Class: GoogleReaderApi::Api

Inherits:
Object
  • Object
show all
Defined in:
lib/google-reader-api/api.rb

Constant Summary collapse

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

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Api

specify either the :email and :password or the :auth token you got in the past

:email

the user’s email address for login purposes

:password

the user’s password for login purposes

:auth

the auth token you got from a previous authentication request if you provide this you do not need to provide the email and password



20
21
22
23
24
25
26
27
28
# File 'lib/google-reader-api/api.rb', line 20

def initialize(options)
  if options[:auth]
    @auth = options[:auth]
    @auth_type = options[:auth_type] || :client_login
  else
    request_auth(options[:email],options[:password])
  end
  @cache = GoogleReaderApi::Cache.new(2)
end

Instance Method Details

#argument_string(args) ⇒ Object

returns the argumentstring based on the hash it is given



86
87
88
# File 'lib/google-reader-api/api.rb', line 86

def argument_string(args)
  args.to_a.map { |v| v.join '=' }.join('&')
end

#authObject



90
91
92
# File 'lib/google-reader-api/api.rb', line 90

def auth
  @auth
end

#cached_unread_countObject



42
43
44
# File 'lib/google-reader-api/api.rb', line 42

def cached_unread_count
  @cache['unread-count'] ||= get_link 'api/0/unread-count', :output => :json
end

do a get request to the link args is a hash of values that should be used in the request



32
33
34
35
# File 'lib/google-reader-api/api.rb', line 32

def get_link(link,args={})
  link = BASE_URL + link
  get_request(link,args)
end

#get_request(url, args) ⇒ Object

the url as a string and the args as a hash e.g. :allcomments => true etc…



57
58
59
60
61
62
63
64
65
# File 'lib/google-reader-api/api.rb', line 57

def get_request(url,args)
  uri = URI.parse url

  # ck is the current unix timestamp
  args[:ck] = Time.now.to_i unless args[:ck]

  req = Net::HTTP::Get.new("#{uri.path}?#{argument_string(args)}")
  request(uri,req)
end

#post_link(link, args = {}) ⇒ Object



37
38
39
40
# File 'lib/google-reader-api/api.rb', line 37

def post_link(link,args={})
  link = BASE_URL + link
  post_request(link,args)
end

#post_request(url, args) ⇒ Object

url as a string the post data as a hash



48
49
50
51
52
53
# File 'lib/google-reader-api/api.rb', line 48

def post_request(url,args)
  uri = URI.parse(url)
  req = Net::HTTP::Post.new(uri.path)
  req.set_form_data(args)
  request(uri,req)
end

#request(uri, request) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/google-reader-api/api.rb', line 67

def request(uri,request)
  # add the cookie to the http header
  if @auth_type == :bearer
    request.add_field('Authorization',"Bearer #{auth}")
  else
    request.add_field('Authorization',"GoogleLogin auth=#{auth}")
  end
  res = Net::HTTP.start(uri.host,uri.port) do |http|
    http.request(request)
  end
  # TODO: use better exception
  if res.code != '200'
    p res.body
    raise "something went wrong"
  end
  res.body
end

#request_auth(email, password) ⇒ Object



94
95
96
97
98
# File 'lib/google-reader-api/api.rb', line 94

def request_auth(email,password)
   = GoogleLogin::ClientLogin.new :service => 'reader', :source => 'nudded-greader-0.1'
  .authenticate email, password
  @auth = .auth
end