Class: Sayso

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

Overview

sayso.post(‘/places/cj5C4oyiCr3Ra2aby-7U4a/reviews’, review)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Sayso

Example:

Sayso.new(:consumer_key => 'your_key', :consumer_secret => 'your_secret', :language => 'nl-NL')

The language parameter is optional.



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

def initialize(options = {})
  options = {:version => 1, :base_url => 'http://api.sayso.com', :callback => nil}.merge(options)

  # The language options is set to the get_params.
  @get_params = {}
  @get_params[:language] = options.delete(:language) if options[:language]

  # All other options should be set to the related instance variable.
  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.



20
21
22
# File 'lib/sayso.rb', line 20

def access_token
  @access_token
end

#base_urlObject

Returns the value of attribute base_url.



19
20
21
# File 'lib/sayso.rb', line 19

def base_url
  @base_url
end

#callbackObject

Returns the value of attribute callback.



19
20
21
# File 'lib/sayso.rb', line 19

def callback
  @callback
end

#consumerObject (readonly)

Returns the value of attribute consumer.



20
21
22
# File 'lib/sayso.rb', line 20

def consumer
  @consumer
end

#consumer_keyObject

Returns the value of attribute consumer_key.



19
20
21
# File 'lib/sayso.rb', line 19

def consumer_key
  @consumer_key
end

#consumer_secretObject

Returns the value of attribute consumer_secret.



19
20
21
# File 'lib/sayso.rb', line 19

def consumer_secret
  @consumer_secret
end

#get_paramsObject

Returns the value of attribute get_params.



19
20
21
# File 'lib/sayso.rb', line 19

def get_params
  @get_params
end

#request_tokenObject (readonly)

Returns the value of attribute request_token.



20
21
22
# File 'lib/sayso.rb', line 20

def request_token
  @request_token
end

#responseObject (readonly)

Returns the value of attribute response.



20
21
22
# File 'lib/sayso.rb', line 20

def response
  @response
end

#versionObject

Returns the value of attribute version.



19
20
21
# File 'lib/sayso.rb', line 19

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.



61
62
63
# File 'lib/sayso.rb', line 61

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.



54
55
56
# File 'lib/sayso.rb', line 54

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")
get("/places/search", :what => 'restaurant', :where => 'antwerpen', :base_country => 'BE')


70
71
72
73
74
75
76
# File 'lib/sayso.rb', line 70

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_token(force = false) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
# File 'lib/sayso.rb', line 39

def initialize_access_token(force = false)
  return @access_token if force || !@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 => callback)
  # 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

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

Examples:

post('/places/:place_id/reviews', :rating => 3, :text => "My review text that should be a certain length.")


80
81
82
83
84
85
# File 'lib/sayso.rb', line 80

def post(path, params = {})
  path = "/api#{self.version}#{path}"
  @response = @access_token.post(path, params, { 'Accept'=>'application/xml', 'Content-Type' => 'application/xml' })
  result = Crack::XML.parse(@response.body)
  HashWithIndifferentAccess.new(result)
end