sayso-api-client 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (5) hide show
  1. data/Gemfile +7 -0
  2. data/README.rdoc +36 -0
  3. data/lib/sayso.rb +68 -0
  4. data/sayso-api-client.gemspec +18 -0
  5. metadata +91 -0
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source "http://rubygems.org"
2
+ source 'http://gemcutter.org'
3
+ source 'http://gems.github.com'
4
+
5
+ gem 'oauth'
6
+ gem 'crack'
7
+ gem 'activesupport'
data/README.rdoc ADDED
@@ -0,0 +1,36 @@
1
+ = SaySo API Client
2
+
3
+ Ruby gem to use the full SaySo API.
4
+ Currently the SaySo API is closed and for partner use only. To request an API
5
+ key please contact us at team@truvolabs.com.
6
+
7
+ For more info see:
8
+
9
+ * <b>The origin of this code:</b> https://github.com/truvolabs/sayso-api-client
10
+ * <b>SaySo:</b> https://www.sayso.com
11
+
12
+ Please read the full API documentation before using!
13
+
14
+ == Install
15
+
16
+ Use the gem.
17
+
18
+ gem install sayso-api-client
19
+
20
+ == Usage
21
+
22
+ require 'sayso'
23
+ sayso = Sayso.new(:consumer_key => 'your_key', :consumer_secret => 'your_secret')
24
+ sayso.get("/places/search?what=restaurant&where=antwerpen")
25
+ sayso.get("/places/search", :what => 'restaurant', :where => 'antwerpen')
26
+ sayso.authorize_url # go here (=> "http://api.sayso.com/api1/oauth/authorize?oauth_token=some_token_hash")
27
+ sayso.authorize_access_token('verifier')
28
+ sayso.get("/users/current")
29
+
30
+ == TODO
31
+
32
+ * Tests
33
+
34
+ Please feel free to contribute and send me a pull request via Github!
35
+
36
+ Copyright (c) 2011 Joost Hietbrink, released under the MIT license
data/lib/sayso.rb ADDED
@@ -0,0 +1,68 @@
1
+ require 'oauth'
2
+ require 'crack'
3
+ require 'active_support/all'
4
+ #
5
+ # Example usage:
6
+ # sayso = Sayso.new(:consumer_key => 'your_key', :consumer_secret => 'your_secret')
7
+ # sayso.get("/places/search?what=restaurant&where=antwerpen")
8
+ # sayso.get("/places/search", :what => 'restaurant', :where => 'antwerpen')
9
+ # sayso.authorize_url # go here (=> "http://api.sayso.com/api1/oauth/authorize?oauth_token=some_token_hash")
10
+ # sayso.authorize_access_token('verifier')
11
+ # sayso.get("/users/current")
12
+ #
13
+ class Sayso
14
+
15
+ attr_accessor :version, :consumer_key, :consumer_secret, :base_url, :callback
16
+ attr_reader :access_token, :request_token, :response
17
+
18
+ def initialize(options = {})
19
+ options = {:version => 1, :base_url => 'http://api.sayso.com', :callback => nil}.merge(options)
20
+ options.each do |attr, value|
21
+ instance_variable_set(:"@#{attr}", value)
22
+ end
23
+ self.initialize_access_token
24
+ end
25
+
26
+ def initialize_access_token
27
+ return @access_token unless @access_token.nil?
28
+ @consumer = OAuth::Consumer.new(self.consumer_key, self.consumer_secret, {
29
+ :site => self.base_url,
30
+ :request_token_path => "/api#{self.version}/oauth/request_token",
31
+ :authorize_path => "/api#{self.version}/oauth/authorize",
32
+ :access_token_path => "/api#{self.version}/oauth/access_token" })
33
+ @request_token = @consumer.get_request_token(:oauth_callback => "http://localhost:3000")
34
+ # Just make it using the request token (and a user that has given access ?!)
35
+ @access_token = OAuth::AccessToken.new(@consumer, @request_token.token, @request_token.secret)
36
+ end
37
+
38
+ # Returns the url where to send the user to authenticate himself.
39
+ # After this call the authorize_access_token method to authenticate
40
+ # the session and be able to use write access and current_user methods.
41
+ def authorize_url
42
+ @request_token.authorize_url
43
+ end
44
+
45
+ # Go to the given url (see authorize_url) and you get redirected to, eg:
46
+ # http://localhost:3000/?oauth_token=DGJb7aPa1XrY82a8hmJVp6IbF0qLZ9Je0pO4B7qF&oauth_verifier=iWhKZfIjPDjozBRhSDoA
47
+ # Call this method with the correct oauth_verifier as argument.
48
+ def authorize_access_token(oauth_verifier)
49
+ @access_token = @request_token.get_access_token(:oauth_verifier => oauth_verifier)
50
+ end
51
+
52
+ # Gets from the Sayso API and returns the parsed XML.
53
+ # Access the unparsed response using the response method.
54
+ # Examples:
55
+ # get("/places/search?what=restaurant&where=antwerpen")
56
+ def get(path, params = {})
57
+ path = "/api#{self.version}#{path}"
58
+ path += "?#{params.to_query}" unless params.blank?
59
+ @response = @access_token.get(path)
60
+ result = Crack::XML.parse(@response.body)
61
+ HashWithIndifferentAccess.new(result)
62
+ end
63
+
64
+ # TODO
65
+ # def post/put
66
+ # end
67
+
68
+ end
@@ -0,0 +1,18 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = %q{sayso-api-client}
5
+ spec.version = '0.1.0'
6
+ spec.platform = Gem::Platform::RUBY
7
+ spec.description = spec.summary = 'Ruby gem to use the SaySo API (v1).'
8
+ spec.authors = ['Joost Hietbrink']
9
+ spec.email = 'joost@truvolabs.com'
10
+ spec.homepage = %q{http://www.sayso.com}
11
+
12
+ spec.add_dependency('oauth')
13
+ spec.add_dependency('crack')
14
+ spec.add_dependency('activesupport')
15
+
16
+ spec.files = `git ls-files`.split("\n")
17
+ spec.require_paths = ['lib']
18
+ end
metadata ADDED
@@ -0,0 +1,91 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sayso-api-client
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.1.0
6
+ platform: ruby
7
+ authors:
8
+ - Joost Hietbrink
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2011-07-21 00:00:00 +02:00
14
+ default_executable:
15
+ dependencies:
16
+ - !ruby/object:Gem::Dependency
17
+ name: oauth
18
+ prerelease: false
19
+ requirement: &id001 !ruby/object:Gem::Requirement
20
+ none: false
21
+ requirements:
22
+ - - ">="
23
+ - !ruby/object:Gem::Version
24
+ version: "0"
25
+ type: :runtime
26
+ version_requirements: *id001
27
+ - !ruby/object:Gem::Dependency
28
+ name: crack
29
+ prerelease: false
30
+ requirement: &id002 !ruby/object:Gem::Requirement
31
+ none: false
32
+ requirements:
33
+ - - ">="
34
+ - !ruby/object:Gem::Version
35
+ version: "0"
36
+ type: :runtime
37
+ version_requirements: *id002
38
+ - !ruby/object:Gem::Dependency
39
+ name: activesupport
40
+ prerelease: false
41
+ requirement: &id003 !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: "0"
47
+ type: :runtime
48
+ version_requirements: *id003
49
+ description: Ruby gem to use the SaySo API (v1).
50
+ email: joost@truvolabs.com
51
+ executables: []
52
+
53
+ extensions: []
54
+
55
+ extra_rdoc_files: []
56
+
57
+ files:
58
+ - Gemfile
59
+ - README.rdoc
60
+ - lib/sayso.rb
61
+ - sayso-api-client.gemspec
62
+ has_rdoc: true
63
+ homepage: http://www.sayso.com
64
+ licenses: []
65
+
66
+ post_install_message:
67
+ rdoc_options: []
68
+
69
+ require_paths:
70
+ - lib
71
+ required_ruby_version: !ruby/object:Gem::Requirement
72
+ none: false
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ version: "0"
77
+ required_rubygems_version: !ruby/object:Gem::Requirement
78
+ none: false
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: "0"
83
+ requirements: []
84
+
85
+ rubyforge_project:
86
+ rubygems_version: 1.5.0
87
+ signing_key:
88
+ specification_version: 3
89
+ summary: Ruby gem to use the SaySo API (v1).
90
+ test_files: []
91
+