Class: GReader::Client
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
@client.tags
@client.tags @client.expire!
@client.tags
Internal low-level usage
@client.api_get 'subscription/list' @client.get 'http://foo'
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
#auth ⇒ Object
Returns the value of attribute auth.
39
40
41
|
# File 'lib/greader/client.rb', line 39
def auth
@auth
end
|
#email ⇒ Object
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.
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_name ⇒ Object
115
|
# File 'lib/greader/client.rb', line 115
def client_name() "greader.rb/#{GReader.version}"; end
|
#expire! ⇒ Object
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
|
#feeds ⇒ Object
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
|
#inspect ⇒ Object
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
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
|
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
|
#token ⇒ Object
77
78
79
|
# File 'lib/greader/client.rb', line 77
def token
@token ||= api_get 'token'
end
|
#unread_count ⇒ Object
111
112
113
|
# File 'lib/greader/client.rb', line 111
def unread_count
json_get 'unread-count', 'all' => 'all'
end
|