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
|
# 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
117
|
# File 'lib/greader/client.rb', line 117
def client_name() "greader.rb/#{GReader.version}"; end
|
#expire! ⇒ Object
80
81
82
83
|
# File 'lib/greader/client.rb', line 80
def expire!
@feeds = nil
@tags = nil
end
|
#feed(what = nil) ⇒ Object
97
98
99
|
# File 'lib/greader/client.rb', line 97
def feed(what=nil)
feeds && @feeds[slug(what)]
end
|
#feeds ⇒ Object
85
86
87
88
89
90
91
92
93
94
95
|
# File 'lib/greader/client.rb', line 85
def feeds
@feeds ||= begin
list = json_get('subscription/list')['subscriptions']
list.inject({}) do |h, item|
feed = Feed.new(self, item)
h[feed.to_param] = feed
h
end
end
@feeds.values.sort
end
|
#get(url, options = {}) ⇒ Object
119
120
121
|
# File 'lib/greader/client.rb', line 119
def get(url, options={})
request :get, url, options.merge('client' => client_name)
end
|
#inspect ⇒ Object
142
143
144
|
# File 'lib/greader/client.rb', line 142
def inspect
"#<#{self.class.name}#{email ? ' '+email.inspect : '' }>"
end
|
#json_get(url, options = {}) ⇒ Object
127
128
129
|
# File 'lib/greader/client.rb', line 127
def json_get(url, options={})
JSON.parse get(url, options.merge('output' => 'json'))
end
|
#logged_in? ⇒ Boolean
71
72
73
|
# File 'lib/greader/client.rb', line 71
def logged_in?
!@auth.nil? or !@oauth_token.nil?
end
|
#post(url, options = {}) ⇒ Object
123
124
125
|
# File 'lib/greader/client.rb', line 123
def post(url, options={})
request :post, url, options.merge('client' => client_name)
end
|
#request(meth, url, options = {}) ⇒ Object
131
132
133
134
135
136
137
138
139
140
|
# File 'lib/greader/client.rb', line 131
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
109
110
111
|
# File 'lib/greader/client.rb', line 109
def tag(what=nil)
tags && @tags[what.gsub('/', '_')]
end
|
101
102
103
104
105
106
107
|
# File 'lib/greader/client.rb', line 101
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
75
76
77
|
# File 'lib/greader/client.rb', line 75
def token
@token ||= api_get 'token'
end
|
#unread_count ⇒ Object
113
114
115
|
# File 'lib/greader/client.rb', line 113
def unread_count
json_get 'unread-count', 'all' => 'all'
end
|