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
# 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



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

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

#expire!Object

Expires the cache



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

#feedsObject



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

#inspectObject



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

Returns:

  • (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

#tagsObject



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

#tokenObject



75
76
77
# File 'lib/greader/client.rb', line 75

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

#unread_countObject



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

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