Class: Google::Reader::Search

Inherits:
Base
  • Object
show all
Defined in:
lib/google/reader/search.rb

Overview

:nodoc:

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Base

get_entries, get_token, parse, parse_json, user_info

Constructor Details

#initialize(q) ⇒ Search

Returns a new instance of Search.



10
11
12
13
# File 'lib/google/reader/search.rb', line 10

def initialize(q)
  self.q = q
  @executed = false
end

Instance Attribute Details

#idsObject

Returns the value of attribute ids.



8
9
10
# File 'lib/google/reader/search.rb', line 8

def ids
  @ids
end

#qObject

Returns the value of attribute q.



8
9
10
# File 'lib/google/reader/search.rb', line 8

def q
  @q
end

Instance Method Details

#executeObject



15
16
17
18
19
20
21
22
23
# File 'lib/google/reader/search.rb', line 15

def execute
  response = self.class.get(SEARCH_IDS_URL, :query_hash => {
    :num => 1000, 
    :output => 'json', 
    :q => q
  })
  self.ids = self.class.parse_json(response)['results'].collect { |r| r['id'] }
  @executed = true
end

#results(o = {}) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/google/reader/search.rb', line 25

def results(o={})
  execute unless @executed
  options = {
    :token => nil,
    :start => 0,
    :num   => 20,
  }.merge(o)
  options[:token] ||= self.class.get_token
  
  if ids.size > 0
    raw_data  = ids[(options[:start]), options[:num]].collect { |id| "i=#{id}" unless id.nil? }.join('&')
    raw_data += "&T=#{options[:token]}"
    json_results = self.class.parse_json(self.class.post(SEARCH_CONTENTS_URL, :raw_data => raw_data))
    results = json_results['items'].inject([]) do |acc, r|
      result = OpenStruct.new(:google_id => r['id'], :title => r['title'], 
                              :published => Time.at(r['published']), :author => r['author'], 
                              :categories => r['categories'])
      if r['alternate'].size > 0
        result.alternate_href = r['alternate'].first['href']
        result.alternate_type = r['alternate'].first['type']
      end
      result.origin_title     = r['origin']['title']
      result.origin_url       = r['origin']['htmlUrl']
      result.origin_stream_id = r['origin']['streamId']
      result.content = r['content'] ? r['content']['content'] : ''
      acc << result
    end
    results.class.class_eval "attr_accessor :total"
    results.total = ids.size
    results
  else
    []
  end
end