Class: Sayso

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

Overview

sayso.get(“/users/current”)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Sayso

Returns a new instance of Sayso.



18
19
20
21
22
23
24
# File 'lib/sayso.rb', line 18

def initialize(options = {})
  options = {:version => 1, :base_url => 'http://api.sayso.com', :callback => nil}.merge(options)
  options.each do |attr, value|
    instance_variable_set(:"@#{attr}", value)
  end
  self.initialize_access_token
end

Instance Attribute Details

#access_tokenObject (readonly)

Returns the value of attribute access_token.



16
17
18
# File 'lib/sayso.rb', line 16

def access_token
  @access_token
end

#base_urlObject

Returns the value of attribute base_url.



15
16
17
# File 'lib/sayso.rb', line 15

def base_url
  @base_url
end

#callbackObject

Returns the value of attribute callback.



15
16
17
# File 'lib/sayso.rb', line 15

def callback
  @callback
end

#consumer_keyObject

Returns the value of attribute consumer_key.



15
16
17
# File 'lib/sayso.rb', line 15

def consumer_key
  @consumer_key
end

#consumer_secretObject

Returns the value of attribute consumer_secret.



15
16
17
# File 'lib/sayso.rb', line 15

def consumer_secret
  @consumer_secret
end

#request_tokenObject (readonly)

Returns the value of attribute request_token.



16
17
18
# File 'lib/sayso.rb', line 16

def request_token
  @request_token
end

#responseObject (readonly)

Returns the value of attribute response.



16
17
18
# File 'lib/sayso.rb', line 16

def response
  @response
end

#versionObject

Returns the value of attribute version.



15
16
17
# File 'lib/sayso.rb', line 15

def version
  @version
end

Instance Method Details

#authorize_access_token(oauth_verifier) ⇒ Object

Go to the given url (see authorize_url) and you get redirected to, eg:

http://localhost:3000/?oauth_token=DGJb7aPa1XrY82a8hmJVp6IbF0qLZ9Je0pO4B7qF&oauth_verifier=iWhKZfIjPDjozBRhSDoA

Call this method with the correct oauth_verifier as argument.



48
49
50
# File 'lib/sayso.rb', line 48

def authorize_access_token(oauth_verifier)
  @access_token = @request_token.get_access_token(:oauth_verifier => oauth_verifier)
end

#authorize_urlObject

Returns the url where to send the user to authenticate himself. After this call the authorize_access_token method to authenticate the session and be able to use write access and current_user methods.



41
42
43
# File 'lib/sayso.rb', line 41

def authorize_url
  @request_token.authorize_url
end

#get(path, params = {}) ⇒ Object

Gets from the Sayso API and returns the parsed XML. Access the unparsed response using the response method. Examples:

get("/places/search?what=restaurant&where=antwerpen")


56
57
58
59
60
61
62
# File 'lib/sayso.rb', line 56

def get(path, params = {})
  path = "/api#{self.version}#{path}"
  path += "?#{params.to_query}" unless params.blank?
  @response = @access_token.get(path)
  result = Crack::XML.parse(@response.body)
  HashWithIndifferentAccess.new(result)
end

#initialize_access_tokenObject



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/sayso.rb', line 26

def initialize_access_token
  return @access_token unless @access_token.nil?
  @consumer = OAuth::Consumer.new(self.consumer_key, self.consumer_secret, {
    :site => self.base_url,
    :request_token_path => "/api#{self.version}/oauth/request_token",
    :authorize_path => "/api#{self.version}/oauth/authorize",
    :access_token_path => "/api#{self.version}/oauth/access_token" })
  @request_token = @consumer.get_request_token(:oauth_callback => "http://localhost:3000")
  # Just make it using the request token (and a user that has given access ?!)
  @access_token = OAuth::AccessToken.new(@consumer, @request_token.token, @request_token.secret)
end