Class: GReader::Client

Inherits:
Object
  • Object
show all
Includes:
Utilities
Defined in:
lib/greader/client.rb

Overview

A client.

Common usage

Greader.auth is the preferred way.

@client = GReader.auth email: '[email protected]', password: 'password'
@client = GReader.auth email: '[email protected]', access_token: <from oauth>
@client.nil?  # nil if logging in fails

You can also use it like so:

client = Client.new email: '[email protected]', password: 'password'
client.logged_in?

See GReader for more common usage of the Client class.

Caching and expiration

# Caching
@client.tags
@client.tags      # Will be retrieved from cache
@client.expire!
@client.tags      # Will be re-retrieved online

Internal low-level usage

# Making calls
@client.api_get 'subscription/list'   # to /reader/api/0/...
@client.get 'http://foo'              # to arbitrary URL

Constant Summary collapse

AUTH_URL =
"https://www.google.com/accounts/ClientLogin"
API_URL =
"http://www.google.com/reader/api/0/"

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Utilities

#escape, #kv_map, #slug, #strip_tags

Constructor Details

#initialize(options = {}) ⇒ Client

Constructor.

The constructor can be called without args, but you won’t be able to do anything that requires authentication (which is pretty much everything).



48
49
50
51
# File 'lib/greader/client.rb', line 48

def initialize(options={})
  authenticate options  if options[:password]
  @oauth_token = options[:access_token]  if options[:access_token]
end

Instance Attribute Details

#authObject (readonly)

Returns the value of attribute auth.



39
40
41
# File 'lib/greader/client.rb', line 39

def auth
  @auth
end

#emailObject (readonly)

Returns the value of attribute email.



40
41
42
# File 'lib/greader/client.rb', line 40

def email
  @email
end

Instance Method Details

#authenticate(options = {}) ⇒ true

Authenticates to the Google Reader service.

Returns:

  • (true)

    on success



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/greader/client.rb', line 55

def authenticate(options={})
  @email = options[:email]

  response = RestClient.post AUTH_URL,
    'service'  => 'reader',
    'continue' => 'http://www.google.com/',
    'Email'    => options[:email],
    'Passwd'   => options[:password],
    'source'   => client_name

  m = /Auth=(.*)/i.match(response.to_s)
  @auth = m ? m[1] : nil

  true
rescue RestClient::Forbidden
  false
end

#client_nameObject



115
# File 'lib/greader/client.rb', line 115

def client_name() "greader.rb/#{GReader.version}"; end

#expire!Object

Expires the cache



82
83
84
85
# File 'lib/greader/client.rb', line 82

def expire!
  @feeds = nil
  @tags  = nil
end

#feed(what = nil) ⇒ Object



95
96
97
# File 'lib/greader/client.rb', line 95

def feed(what=nil)
  feeds && @feeds[what.to_s.gsub('/', '_')]
end

#feedsObject



87
88
89
90
91
92
93
# File 'lib/greader/client.rb', line 87

def feeds
  @feeds ||= begin
    list = json_get('subscription/list')['subscriptions']
    list.inject({}) { |h, item| feed = Feed.new self, item; h[feed.to_param] = feed; h }
  end
  @feeds.values.sort
end

#get(url, options = {}) ⇒ Object



117
118
119
# File 'lib/greader/client.rb', line 117

def get(url, options={})
  request :get, url, params: options.merge('client' => client_name)
end

#inspectObject



141
142
143
# File 'lib/greader/client.rb', line 141

def inspect
  "#<#{self.class.name}#{email ? ' '+email.inspect : '' }>"
end

#json_get(url, options = {}) ⇒ Object



125
126
127
# File 'lib/greader/client.rb', line 125

def json_get(url, options={})
  JSON.parse get(url, options.merge('output' => 'json'))
end

#logged_in?Boolean

Returns:

  • (Boolean)


73
74
75
# File 'lib/greader/client.rb', line 73

def logged_in?
  !@auth.nil? or !@oauth_token.nil?
end

#post(url, options = {}) ⇒ Object



121
122
123
# File 'lib/greader/client.rb', line 121

def post(url, options={})
  request :post, url, options.merge('client' => client_name)
end

#request(meth, url, options = {}) ⇒ Object



129
130
131
132
133
134
135
136
137
138
139
# File 'lib/greader/client.rb', line 129

def request(meth, url, options={})
  url = API_URL + url

  if @auth
    auth_request meth, url, options
  elsif @oauth_token
    oauth_request meth, url, options
  else
    raise Error, "Not logged in"
  end
end

#tag(what = nil) ⇒ Object



107
108
109
# File 'lib/greader/client.rb', line 107

def tag(what=nil)
  tags && @tags[what.gsub('/', '_')]
end

#tagsObject



99
100
101
102
103
104
105
# File 'lib/greader/client.rb', line 99

def tags
  @tags ||= begin
    list = json_get('tag/list')['tags']
    list.inject({}) { |h, item| tag = Tag.new self, item; h[tag.to_param] = tag; h }
  end
  @tags.values.sort
end

#tokenObject



77
78
79
# File 'lib/greader/client.rb', line 77

def token
  @token ||= api_get 'token'
end

#unread_countObject



111
112
113
# File 'lib/greader/client.rb', line 111

def unread_count
  json_get 'unread-count', 'all' => 'all'
end