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.



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

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.



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

def access_token
  @access_token
end

#base_urlObject

Returns the value of attribute base_url.



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

def base_url
  @base_url
end

#callbackObject

Returns the value of attribute callback.



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

def callback
  @callback
end

#consumerObject (readonly)

Returns the value of attribute consumer.



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

def consumer
  @consumer
end

#consumer_keyObject

Returns the value of attribute consumer_key.



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

def consumer_key
  @consumer_key
end

#consumer_secretObject

Returns the value of attribute consumer_secret.



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

def consumer_secret
  @consumer_secret
end

#get_paramsObject

Returns the value of attribute get_params.



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

def get_params
  @get_params
end

#request_tokenObject (readonly)

Returns the value of attribute request_token.



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

def request_token
  @request_token
end

#responseObject (readonly)

Returns the value of attribute response.



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

def response
  @response
end

#versionObject

Returns the value of attribute version.



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

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.



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

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.



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

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

Raises:

  • (ArgumentError)


71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/sayso.rb', line 71

def get(path, params = {})
  params = params.with_indifferent_access
  # We should always include a base_country in searches.
  raise ArgumentError, "You should add a :country parameter to a search request to prevent weird/incorrect replies." if path =~ /^\/places\/search/ && !(params.include?(:base_country) || path =~ /(\?|&)base\_country\=/)

  path = "/api#{self.version}#{path}"
  path += "?#{params.to_query}" unless params.blank?
  @response = @access_token.get(path)
  raise StandardError, "Got non 200 HTTP response from SaySo" if not @response.code == '200'
  result = Crack::XML.parse(@response.body)
  HashWithIndifferentAccess.new(result)
end

#initialize_access_token(force = false) ⇒ Object



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

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.")


86
87
88
89
90
91
# File 'lib/sayso.rb', line 86

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