Class: Overlord::GoogleFeedRequest

Inherits:
GoogleBase show all
Defined in:
lib/overlord/google_feed_request.rb

Overview

Class Method Summary collapse

Methods inherited from GoogleBase

api_key_id, api_key_id=, build_google_query, get, parse_name, referer, referer=, user_ip, user_ip=

Methods included from JsonParser

#parse_json, #try_active_json, #try_crack, #try_json

Class Method Details

.convert_google_feed_json_to_entries(feed, json) ⇒ Object

Converts json returned from google into an array of entries



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/overlord/google_feed_request.rb', line 54

def self.convert_google_feed_json_to_entries(feed, json)
  if json['responseData']['feed']['entries']
    json['responseData']['feed']['entries'].collect do |entry|
      published_at = DateTime.parse(entry['publishedDate']) rescue DateTime.now - 1.day
      Entry.new(:permalink => entry['link'],
                :author => entry['author'],
                :title => entry['title'],
                :description => entry['contentSnippet'],
                :content => entry['content'],
                :published_at => published_at,
                :tag_list => entry['categories'],
                :direct_link => entry['link'],
                :feed => feed)
    end
  end
end

.convert_google_feed_json_to_feed(feed, json) ⇒ Object

Converts json returned from google into a feed object



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/overlord/google_feed_request.rb', line 41

def self.convert_google_feed_json_to_feed(feed, json)
  if json['responseStatus'] == 200
    if json['responseData']['feed']
      Feed.new( :uri => feed.uri,
                :service_id => feed.service_id,
                :display_uri => json['responseData']['feed']['link'],
                :title => json['responseData']['feed']['title'],
                :service => feed.service)
    end
  end
end

.convert_google_find_feeds_json_to_feeds(google_feeds) ⇒ Object

convert the result of a google query to ‘Feed’ objects



90
91
92
93
94
95
96
97
# File 'lib/overlord/google_feed_request.rb', line 90

def self.convert_google_find_feeds_json_to_feeds(google_feeds)
  google_feeds['responseData']['entries'].collect do |google_feed|
    Feed.new( :uri => google_feed['url'],
              :display_uri => google_feed['link'],
              :title => google_feed['title'],
              :service_id => Service.find_service_by_uri(google_feed['link']).id)
  end
end

.find_feeds(query) ⇒ Object

Search for feeds using google. This will return a collection of ‘Feed’ objects



73
74
75
76
77
78
79
80
81
# File 'lib/overlord/google_feed_request.rb', line 73

def self.find_feeds(query)
  query = query.join('+') if query.is_a?(Array)
  feed_response = find_google_feeds(query)
  if 200 == feed_response['responseStatus']
    convert_google_find_feeds_json_to_feeds(feed_response)
  else
    []
  end
end

.find_google_feeds(query, number_of_items = 5) ⇒ Object

Search for feeds using google. This will return a hash with the results



85
86
87
# File 'lib/overlord/google_feed_request.rb', line 85

def self.find_google_feeds(query, number_of_items = 5)
  get('http://ajax.googleapis.com/ajax/services/feed/find', :query => build_google_query({:q => query}))
end

.load_feed(uri, number_of_items = 5) ⇒ Object

Requests entries for a single feed

uri: Uri of the feed to load number_of_items: Number of items to load from the feed



19
20
21
# File 'lib/overlord/google_feed_request.rb', line 19

def self.load_feed(uri, number_of_items = 5)
  get('http://ajax.googleapis.com/ajax/services/feed/load', :query => build_google_query({:q => uri, :num => number_of_items}))
end

.load_feeds(feeds, number_of_items = 5) ⇒ Object

Requests a set of feeds from Google

feeds: An array of objects with the attributes ‘uri’ and ‘service_id’ defined



26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/overlord/google_feed_request.rb', line 26

def self.load_feeds(feeds, number_of_items = 5)
  feeds = feeds.collect do |feed|
    json = load_feed(feed.uri, number_of_items)
    new_feed = convert_google_feed_json_to_feed(feed, json)
    new_feed.entries << convert_google_feed_json_to_entries(new_feed, json).compact if new_feed
    new_feed
  end
  if feeds
    feeds.compact
  else
    []
  end
end

.lookup_feed(uri) ⇒ Object

Lookup a given feed.



11
12
13
# File 'lib/overlord/google_feed_request.rb', line 11

def self.lookup_feed(uri)
  get('http://ajax.googleapis.com/ajax/services/feed/lookup', :query => build_google_query({:q => uri}))
end