Class: Sayso
- Inherits:
-
Object
- Object
- Sayso
- Defined in:
- lib/sayso.rb
Overview
sayso.post(‘/places/cj5C4oyiCr3Ra2aby-7U4a/reviews’, review)
Instance Attribute Summary collapse
-
#access_token ⇒ Object
readonly
Returns the value of attribute access_token.
-
#base_url ⇒ Object
Returns the value of attribute base_url.
-
#callback ⇒ Object
Returns the value of attribute callback.
-
#consumer ⇒ Object
readonly
Returns the value of attribute consumer.
-
#consumer_key ⇒ Object
Returns the value of attribute consumer_key.
-
#consumer_secret ⇒ Object
Returns the value of attribute consumer_secret.
-
#get_params ⇒ Object
Returns the value of attribute get_params.
-
#request_token ⇒ Object
readonly
Returns the value of attribute request_token.
-
#response ⇒ Object
readonly
Returns the value of attribute response.
-
#version ⇒ Object
Returns the value of attribute version.
Instance Method Summary collapse
-
#authorize_access_token(oauth_verifier) ⇒ Object
Go to the given url (see authorize_url) and you get redirected to, eg: localhost:3000/?oauth_token=DGJb7aPa1XrY82a8hmJVp6IbF0qLZ9Je0pO4B7qF&oauth_verifier=iWhKZfIjPDjozBRhSDoA Call this method with the correct oauth_verifier as argument.
-
#authorize_url ⇒ Object
Returns the url where to send the user to authenticate himself.
-
#get(path, params = {}) ⇒ Object
Gets from the Sayso API and returns the parsed XML.
-
#initialize(options = {}) ⇒ Sayso
constructor
Example: Sayso.new(:consumer_key => ‘your_key’, :consumer_secret => ‘your_secret’, :language => ‘nl-NL’) The language parameter is optional.
- #initialize_access_token(force = false) ⇒ Object
-
#post(path, params = {}) ⇒ Object
Examples: post(‘/places/:place_id/reviews’, :rating => 3, :text => “My review text that should be a certain length.”).
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( = {}) = {:version => 1, :base_url => 'http://api.sayso.com', :callback => nil}.merge() # The language options is set to the get_params. @get_params = {} @get_params[:language] = .delete(:language) if [:language] # All other options should be set to the related instance variable. .each do |attr, value| instance_variable_set(:"@#{attr}", value) end self.initialize_access_token end |
Instance Attribute Details
#access_token ⇒ Object (readonly)
Returns the value of attribute access_token.
21 22 23 |
# File 'lib/sayso.rb', line 21 def access_token @access_token end |
#base_url ⇒ Object
Returns the value of attribute base_url.
20 21 22 |
# File 'lib/sayso.rb', line 20 def base_url @base_url end |
#callback ⇒ Object
Returns the value of attribute callback.
20 21 22 |
# File 'lib/sayso.rb', line 20 def callback @callback end |
#consumer ⇒ Object (readonly)
Returns the value of attribute consumer.
21 22 23 |
# File 'lib/sayso.rb', line 21 def consumer @consumer end |
#consumer_key ⇒ Object
Returns the value of attribute consumer_key.
20 21 22 |
# File 'lib/sayso.rb', line 20 def consumer_key @consumer_key end |
#consumer_secret ⇒ Object
Returns the value of attribute consumer_secret.
20 21 22 |
# File 'lib/sayso.rb', line 20 def consumer_secret @consumer_secret end |
#get_params ⇒ Object
Returns the value of attribute get_params.
20 21 22 |
# File 'lib/sayso.rb', line 20 def get_params @get_params end |
#request_token ⇒ Object (readonly)
Returns the value of attribute request_token.
21 22 23 |
# File 'lib/sayso.rb', line 21 def request_token @request_token end |
#response ⇒ Object (readonly)
Returns the value of attribute response.
21 22 23 |
# File 'lib/sayso.rb', line 21 def response @response end |
#version ⇒ Object
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 (oauth_verifier) @access_token = @request_token.get_access_token(:oauth_verifier => oauth_verifier) end |
#authorize_url ⇒ Object
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 @request_token. 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')
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 |