Class: Greedy::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/greedy/connection.rb

Constant Summary collapse

BASE_URL =
"http://www.google.com/reader/api/0/"

Instance Method Summary collapse

Constructor Details

#initialize(username, password, appname = "Greedy") ⇒ Connection

Create a new connection to the Google Reader API Example:

Greedy::Connection.new 'username', 'password', 'my aggregator application'


8
9
10
11
12
13
# File 'lib/greedy/connection.rb', line 8

def initialize(username, password, appname = "Greedy")
  @username = username
  @password = password
  @application_name = appname
  connect!
end

Instance Method Details

#connected?Boolean

Determine the status of the connection

Returns:

  • (Boolean)


46
47
48
# File 'lib/greedy/connection.rb', line 46

def connected?
  !@client.nil?
end

#fetch(path, options = {}) ⇒ Object

Issue a GET HTTP request to the Google Reader API Example

connection.fetch "stream/contents/user/-/state/com.google/unread", :c => '976H987BKU'


18
19
20
21
22
23
24
25
26
27
28
# File 'lib/greedy/connection.rb', line 18

def fetch(path, options = {})
  url = [full_path(path), convert_to_querystring(options)].join
  response = @client.get url
  JSON.parse response.body
rescue GData::Client::RequestError => e
  if Greedy::AuthorizationError.gdata_errors.include?(e.class.to_s)
    raise Greedy::AuthorizationError.new(e.message)
  else
    raise Greedy::ServiceError.new(e.message)
  end
end

#post(path, form_data = {}) ⇒ Object

Issue a POST HTTP request to the Google Reader API Example:

connection.fetch "stream/contents/user/-/state/com.google/edit-tag", :form_data => { :async => false, :a => 'broadcast' }


33
34
35
36
37
38
39
40
41
42
43
# File 'lib/greedy/connection.rb', line 33

def post(path, form_data = {})
  uri = [full_path(path), convert_to_querystring(:client => @application_name)].join
  response = @client.post uri, convert_to_post_body(form_data)
  JSON.parse response.body
rescue GData::Client::RequestError => e
  if Greedy::AuthorizationError.gdata_errors.include?(e.class.to_s)
    raise Greedy::AuthorizationError.new(e.message)
  else
    raise Greedy::ServiceError.new(e.message)
  end
end