Class: SocialFeedAgregator::GoogleplusReader

Inherits:
BaseReader
  • Object
show all
Defined in:
lib/social_feed_agregator/googleplus_reader.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ GoogleplusReader

Returns a new instance of GoogleplusReader.



9
10
11
12
13
14
15
# File 'lib/social_feed_agregator/googleplus_reader.rb', line 9

def initialize(options={})
  super(options)
  options.replace(SFA.default_options.merge(options))

  @user_id = options[:googleplus_user_id]
  @api_key = options[:googleplus_api_key]
end

Instance Attribute Details

#api_keyObject

Returns the value of attribute api_key.



7
8
9
# File 'lib/social_feed_agregator/googleplus_reader.rb', line 7

def api_key
  @api_key
end

#user_idObject

Returns the value of attribute user_id.



7
8
9
# File 'lib/social_feed_agregator/googleplus_reader.rb', line 7

def user_id
  @user_id
end

Instance Method Details

#get_feeds(options = {}) ⇒ Object



17
18
19
20
21
22
23
24
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
# File 'lib/social_feed_agregator/googleplus_reader.rb', line 17

def get_feeds(options={}) 
  super(options)
  @user_id = options[:user_id] if options[:user_id]
  count = options[:count] || 25

  from_date = options[:from_date] || DateTime.new(1970,1,1) 
  
  feeds, i, count_per_request, items = [], 0, 100, 0

  opts =  {count: count < count_per_request ? count : count_per_request}

  parts = (count.to_f / count_per_request).ceil

  url = "https://www.googleapis.com/plus/v1/people/#{@user_id}/activities/public?key=#{@api_key}&maxResults=#{opts[:count]}"      

  begin
    i+=1
    next_query = ""
    data = JSON.parse( RestClient.get("#{url}#{next_query}") )

    data['items'].each do |post|    
      items+=1
      break if items > count          

      # Break if the date is less
      if DateTime.parse(post["published"]) <= from_date
        i = parts
        break
      end

      feed = fill_feed post

      block_given? ? yield(feed) : feeds << feed        
    end       
    next_query = "&pageToken=#{data['nextPageToken']}"
    
  end while (data['items'].count > 0 ) && (i < parts)      
end