Ruby on Rails 3 and Google Book Search - ruby-on-rails

I'm trying to get started using the Google Data API for Google Book Search in my Ruby on Rails 3 application, and I don't even understand how to get started. What gems do I need? What do I need to do in order to do something simple like searching for books with a title of Foobar?

Following up on the deprecation issue: I've just published GoogleBooks, a Ruby wrapper that enables users to query for books precisely in the manner described.
It's updated to hook into the present-day Google API, so it's not affected by the recent deprecation of the Google Book Search API.

If you're looking to use Google Books to retrieve information about books, you can use their data API: http://code.google.com/apis/books/docs/gdata/developers_guide_protocol.html
Making requests to a URL like http://books.google.com/books/feeds/volumes?q=isbn:9780974514055 will return XML with the book's information. You could use the Nokogiri gem to parse the result ( http://nokogiri.org/ ).
One thing to be aware of is that, to get the full descriptions for books, you need to get the entry instead of just the feed results.
Here's a short example of how you could get a book's information from Google:
require 'open-uri'
require 'nokogiri'
class Book
attr_accessor :title, :description
def self.from_google(title)
book = self.new
entry = Nokogiri::XML(open "http://books.google.com/books/feeds/volumes?q=#{title}").css("entry id").first
xml = Nokogiri::XML(open entry.text) if entry
return book unless xml
book.title = xml.css("entry dc|title").first.text unless xml.css("entry dc|title").empty?
book.description = xml.css("entry dc|description").first.text unless xml.css("entry dc|description").empty?
book
end
end
b = Book.from_google("Ruby")
p b

if you want to use the api, i think you will have to use jruby and their java api. no ruby api exists for the book search, according to this: http://code.google.com/apis/books/docs/gdata/code.html
for connecting with google, try using the gdata gem.
http://code.google.com/apis/gdata/articles/gdata_on_rails.html#SetupRails

Related

How to parse api request using ruby

I am learning how to use Yelp API from this yelp blog and yelp github on Rails. I was able to connect to Yelp service and got a response back, but I don't know what to do with the response that I got back.
Here is what I did, on Rails Console:
2.2.2 :057 > response = client.search('los angeles', {limit: 2})
=> #<Yelp::Response::Search:0x007fff32edc2c0 #region=#<Yelp::Response::Model::Region:0x007fff35ddf6d0 #span=#<Yelp::Response::Model::RegionSpan:0x007fff35ddf450 #latitude_delta=0.04455494999999132, #longitude_delta=0.02209966000000918>, #center=#<Yelp::Response::Model::RegionCenter:0x007fff35ddf5e0 #latitude=34.08390635, #longitude=-118.3184503>>,...
What kind of format is that? this article says that when I make API call to Yelp, it gives ruby object, but I am not sure what data type #<Yelp::Response... is. I guess I was expecting a ruby array/ json format return, like stated in the article:
`
search
response = client.search('San Francisco')
response.businesses
[< Business 1>, < Business 2 >, ...]
response.businesses[0].name
"Kim Makoi, DC"
response.businesses[0].rating
5.0
If I want to select a specific information, say display_address, or neighborhood from the return API, how can I do that? (Here is the end part of the same API request):
...#display_address=["6353 Yucca St", "Hollywood", "Los Angeles, CA 90028"], #geo_accuracy=8.0, #postal_code="90028", #country_code="US", #address=["6353 Yucca St"], #coordinate=#<Yelp::Response::Model::Coordinate:0x007fff35ddf7e8 #latitude=34.10413, #longitude=-118.32834>, #state_code="CA", #neighborhoods=["Hollywood"]>, #deals=nil, #gift_certificates=nil, #reviews=nil>]>
1.
If you were to call the API directly (say, from cURL), you'd get JSON back.
You're using the Yelp gem, though, so it's helpfully converting that JSON into a ruby object for you. If you're interested in the construction of the object, you can take a look at how the gem is doing the conversion on GitHub.
You should be able to interact with that response just like the article states, i.e. results.businesses should give you an array of businesses found.
More concretely, it looks like you can do something like:
results.businesses[0].display_address to get the display address for the first business found
results.businesses[0].neighborhoods is an array of all neighborhoods associated with that same business.

Can we extract multiple books data from google books api using googlebooks ruby gem?

I am trying to parse the books data from google api JSON response
below link gives me books data for multiple books
https://www.googleapis.com/books/v1/volumes?q=you%20can%20win&maxresults=40
Using GoogleBooks ruby gem, i could hardly figure out the way to get data of the first books
Ex:first.book.tittle, first.book.author
Can someone please help me out getting the data of all books in the JSON ?
It should be something like that:
books = GoogleBooks::API.search('Douglas Rockford')
books.each do |book|
puts book.author
end

Rails TMDB API Browse to Find

TMDB.org recently made a change to their API which removes the capability to browse their database.
My Rails app used to use the tmdb-ruby gem to browse the TMDB database, but this gem only worked with v2.0 of the API, which is now defunct.
TMDB.org recommends using this gem, and since it is forked from the gem I previously used, it makes it a bit easier.
My PostgreSQL database is already populated with data imported from TMDB when v2.0 was still extant and when I could use the browse feature.
How can I now use the find feature (ie: #movie = TmdbMovie.find(:title => "Iron Man", :limit => 1) ) to find a random movie, without supplying the title of the Movie.
This is my rake file which worked with the older gem.
I would like to know how to have it work the same way but whilst using the find instead of the browse.
Thanks
I don't think find is what you need in order to get what you want (getting the oldest movies in the database and working its way up to the newest movie). Looking at the TMDb API documentation, it looks like they now have discover that may have replaced the browse that you used to use.
I don't see discover anywhere in Irio's ruby-tmdb fork, but it looks like most of the specific methods they have (like TmdbMovie.find) call a generic method Tmdb.api_call.
You should be able to use the generic method to do something like:
api_return = Tmdb.api_call(
"discover/movie",
{
page: 1,
sort_by: 'release_date.asc',
query: '' # Necessary because Tmdb.api_call throws a nil error if you don't specify a query param value
},
"en"
)
results = api_return["results"]
results.flatten!(1)
results.uniq!
results.delete_if &:nil?
results.map!{|m| TmdbMovie.new(m, true)} # `true` tells TmdbMovie.new to expand results
If this works, you could even fork Irio's fork, implement a TmdbMovie.discover method supporting all the options and handling edge cases like TmdbMovie.find does, and send them a pull request since it just looks like they haven't gotten around to implementing this yet and I'm sure other people would like to have this method as well :)

Search for user query string for address in database

I have a Rails app on a Postgres database and I need to have a search field for the user to enter a string and look up in the database for possible address matches (within a city). In the database I have a column with full addresses.
I cannot make assumptions on the input, so I am thinking that I should first try to directly look up the address on the database somehow (using a LIKE query maybe?), and if that fails, request to a Geocoding API (i.e. Google) to return a well formatted addresses list matching the query and search those in my database.
I would appreciate any guidance on how to do this.
I don't think FTS (full text search) is what you want. You'll have to use an address API that can match addresses.
I've successfully and easily used SmartyStreets for something like this. They have a free account you can use.
http://smartystreets.com
Also if you did want to try going down the FTS route here is a Gist that explains how to do it.
https://gist.github.com/4365593
You may know it already, but postresql has a fulltext search engine integrated so it's a great time to take advantage of it. I suggest watching thats excellent railscast.
Then once implemented :
class Place < AR
def search_db_or_geokit(query)
res = db_search()
if res.empty?
res = geokit_search(query)
else
res
end
end
def geokit_search(query)
# ...
end
def db_search(query)
# ...
end
end
For the geocoding google search api there's probably a good gem out there like geokit

What is the best way to access Google Calendar from ruby?

I'm writing an app for a company that uses Google Calendar internally and would need to use events they already have in their calendar in the app. So I need to get read only access to their calendars from the app (namely I need the events title, start and end dates and attendee emails for all future events).
What is the simplest way to do this in ruby (I would need it to work relatively seamlessly on Heroku)?
I tried using the GCal4Ruby gem which seemed the least outdated of the ones I found but I'm unable to even authenticate through the library (HTTPRequestFailed - Captcha required error) let alone get the info I need.
Clarification: What I'm talking about is the Google Apps version of the calendar, not the one at calendar.google.com.
OK I got the api via GCal4Ruby working. I'm not exactly sure what went wrong the first time. Thanks to Mike and James for their suggestions. This is sample code I used for anyone interested:
require "rubygems"
require "gcal4ruby"
serv = GCal4Ruby::Service.new
serv.authenticate "username#example.com", "password"
events = GCal4Ruby::Event.find serv, {'start-min' => Time.now.utc.xmlschema,
:calendar => 'example-cal%40example.com'}
events.each do |event|
puts event.title
puts event.attendees.join ", "
puts event.start_time
puts event.end_time
puts '-----------------------'
end
You should be able to use the Google Calendar private xml address feature to pull out the needed data.
You could then parse it with hpricot or nokogiri to extract whatever fields you need.

Resources