Skip to content

Instantly share code, notes, and snippets.

@libbyschuknight
Created August 11, 2015 02:36
Show Gist options
  • Save libbyschuknight/8e552a64075b20b69254 to your computer and use it in GitHub Desktop.
Save libbyschuknight/8e552a64075b20b69254 to your computer and use it in GitHub Desktop.
Google Books API example
@import url(http://fonts.googleapis.com/css?family=Source+Sans+Pro:300,400,600,700);
* {
/*border: 1px solid lightgray;*/
margin: 0 auto;
}
body {
width: 95%;
font-family: 'Source Sans Pro', sans-serif;
margin: 30px;
}
.container {
width: 90%;
}
header {
width: 80%;
}
.row {
border-top: 1px solid lightgray;
padding: 20px;
}
.row img {
margin-top: 10px;
}
.nav, h1, h2, footer {
text-align: center;
}
.nav {
display: inline-block;
margin: 0 auto;
text-align: center;
}
h5 {
font-weight: bold;
}
footer {
border-top: 1px solid lightgray;
margin-top: 30px;
margin-bottom: 30px;
}
class Book
attr_accessor :title, :url, :description, :image, :authors
def initialize(args)
@title = args.fetch(:title)
@url = args.fetch(:url)
@description = args.fetch(:description)
# @image = args.fetch(:image)
@authors = args.fetch(:authors)
end
end
require "httparty"
class BookParser
attr_accessor :data
def initialize(data)
@data = HTTParty.get(data).parsed_response
load_books
end
def load_books
books = @data
book_shelf = []
books["items"].each do | book |
values = {
title: title = book["volumeInfo"]["title"],
url: url = book["volumeInfo"]["canonicalVolumeLink"],
description: description = book["volumeInfo"]["description"],
# image: image_url = book["volumeInfo"]["imageLinks"]["smallThumbnail"],
authors: authors = book["volumeInfo"]["authors"] }
book_shelf << Book.new(values)
end
book_shelf
end
end
get '/' do
# search for surf+new+zealand
@book_parser = BookParser.new("https://www.googleapis.com/books/v1/volumes?q=surf+new+zealand")
puts @book_parser
erb :index
end
<div id='all_books'>
<h2>All books - find when using for 'surf+new zealand'</h2>
<div class='books'>
<ul class="list-unstyled">
<% @book_parser.load_books.each do | book | %>
<div class="row">
<div class="col-md-8">
<li><h4>
<a class='' href='<%= book.url %>' target='_blank' > <%= book.title %> </a>
</h4></li>
<li>
<%= book.description %>
</li>
</div>
<div class="col-md-4">
<li>
<img class='img-responsive center-block img-thumbnail' src = '<%= book.image %>' >
</li>
</div>
</div>
<% end %>
</ul>
</div>
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment