oauth-client 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,3 @@
1
+
2
+ *.gem
3
+ *.gemspec
data/README.textile ADDED
@@ -0,0 +1,46 @@
1
+ h1. Generic OAuth Client for Ruby, based on moomerman/twitter_oauth
2
+
3
+ *NOTE*: This is a very early version and has not been tested, give it some time :)
4
+
5
+ This is a core OAuth Client, extracting core elements from the an awesome moomerman/twitter_oauth gem and reorganizing them to give you tools to build your own client for an API of choice. This gem can be used to jump-start your new OAuth client.
6
+
7
+ Example:
8
+ <pre><code>require 'oauth_client'
9
+ class TwitterOAuth < OAuthClient::Client
10
+ site 'http://twitter.com'
11
+ end</code></pre>
12
+
13
+ That's all you need to have a working OAuth client which you can use to authorize with an OAuth server. Of course, it won't do anything just yet :) You need to implement specific methods for the API you're working with.
14
+
15
+ You have a few helper methods to ease the trouble. OAuthClient#get and OAuthClient#post return the unparsed response from the server. It includes an adapter for JSON, which will parse the response prior to returning it. To use it, call OAuthClient#json.get or OAuthClient#json.post.
16
+
17
+ See this example from moomerman/twitter_oauth:
18
+ <pre><code>module TwitterOauth
19
+ class Client
20
+ [...]
21
+ def user(page=1)
22
+ oauth_response = access_token.get("/statuses/user_timeline.json?page=#{page}")
23
+ JSON.parse(oauth_response.body)
24
+ end
25
+ [...]
26
+ end
27
+ end
28
+ </code></pre>
29
+
30
+ with OAuthClient, it would look like this:
31
+
32
+ <pre><code>require 'oauth_client'
33
+ class TwitterOAuth < OAuthClient::Client
34
+ site 'http://twitter.com'
35
+
36
+ def user(page=1)
37
+ json.get("/statuses/user_timeline.json?page=#{page}")
38
+ end
39
+ end</code></pre>
40
+
41
+ h2. Authors
42
+
43
+ This gem has been built by hacking and modifing the twitter_oauth gem by Richard Taylor (http://github.com/moomerman). The core client authorization structure is taken from there.
44
+
45
+ Banged together by Marcin Bunsch (http://github.com/marcinbunsch).
46
+
data/Rakefile ADDED
@@ -0,0 +1,17 @@
1
+ begin
2
+ require 'jeweler'
3
+ Jeweler::Tasks.new do |gem|
4
+ gem.name = "oauth-client"
5
+ gem.summary = %q{Generic OAuth Client for Ruby, based on moomerman/twitter_oauth}
6
+ gem.description = %q{Generic OAuth Client for Ruby, based on moomerman/twitter_oauth}
7
+ gem.email = "marcel@northdocks.com"
8
+ gem.homepage = "http://github.com/sirlantis/oauth-client"
9
+ gem.authors = ["Marcin Bunch, Marcel Jackwerth, Richard Taylor"]
10
+
11
+ gem.add_dependency('oauth', '>= 0.3.1')
12
+ gem.add_dependency('json', '>= 1.1.2')
13
+ end
14
+ Jeweler::GemcutterTasks.new
15
+ rescue LoadError
16
+ puts "Jeweler not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
17
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,15 @@
1
+ # This is a Tripit API client demo built on top of OAuthClient
2
+ require 'oauth_client'
3
+ class TripitOAuth < OAuthClient::Client
4
+ site 'https://api.tripit.com'
5
+
6
+ def info
7
+ json.get('/v1/get/profile?format=json')
8
+ end
9
+
10
+ def trips
11
+ json.get('/v1/list/trip/traveler/true?format=json')
12
+ end
13
+
14
+ end
15
+
@@ -0,0 +1,23 @@
1
+ # This is a Twitter API client demo built on top of OAuthClient
2
+ require 'oauth_client'
3
+ class TwitterOAuth < OAuthClient::Client
4
+ site 'http://twitter.com'
5
+
6
+ def show(username)
7
+ json.get("/users/show/#{username}.json")
8
+ end
9
+
10
+ def friends(username)
11
+ json.get("/friends/ids/#{username}.json")
12
+ end
13
+
14
+ def user(page=1)
15
+ json.get("/statuses/user_timeline.json?page=#{page}")
16
+ end
17
+
18
+ def update(message)
19
+ json.post('/statuses/update.json', :status => message)
20
+ end
21
+
22
+ end
23
+
@@ -0,0 +1,9 @@
1
+
2
+ $:.unshift(File.dirname(__FILE__))
3
+ require 'rubygems'
4
+ require 'oauth'
5
+ require 'oauth_client/client'
6
+ require 'oauth_client/exceptions'
7
+
8
+ module OAuthClient
9
+ end
@@ -0,0 +1,26 @@
1
+ require 'json'
2
+ module OAuthClient
3
+ module Adapters
4
+ # Json Adapter for OAuthClient
5
+ class Json
6
+ attr_accessor :client
7
+
8
+ # on creation, the adapter must be supplied with the client
9
+ def initialize(client)
10
+ self.client = client
11
+ end
12
+
13
+ # make a GET request and parse JSON response
14
+ def get(url)
15
+ oauth_response = self.client.get(url)
16
+ JSON.parse(oauth_response.body)
17
+ end
18
+
19
+ # make a GET request and parse JSON response
20
+ def post(url, params = {})
21
+ oauth_response = self.client.post(url, params)
22
+ JSON.parse(oauth_response.body)
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,26 @@
1
+ require 'nokogiri'
2
+ module OAuthClient
3
+ module Adapters
4
+ # Xml Adapter for OAuthClient
5
+ class Xml
6
+ attr_accessor :client
7
+
8
+ # on creation, the adapter must be supplied with the client
9
+ def initialize(client)
10
+ self.client = client
11
+ end
12
+
13
+ # make a GET request and parse Xml response
14
+ def get(url)
15
+ oauth_response = self.client.get(url)
16
+ Nokogiri::XML::Document.parse(oauth_response.body)
17
+ end
18
+
19
+ # make a GET request and parse Xml response
20
+ def post(url, params = {})
21
+ oauth_response = self.client.post(url, params)
22
+ Nokogiri::XML::Document.parse(oauth_response.body)
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,110 @@
1
+ module OAuthClient
2
+ class Client
3
+
4
+ # class method for setting/getting the base uri for API
5
+ def self.site(site = nil)
6
+ @@site = site if site
7
+ @@site
8
+ end
9
+
10
+ # class method for setting/getting the http method for API
11
+ def self.http_method(http_method = nil)
12
+ @@http_method = http_method if http_method
13
+ @@http_method
14
+ end
15
+
16
+ # class method for setting/getting the http method for API
17
+ def self.scheme(scheme = nil)
18
+ @@scheme = scheme if scheme
19
+ @@scheme
20
+ end
21
+
22
+ def self.request_scheme(scheme = nil)
23
+ @@request_scheme = scheme if scheme
24
+ @@request_scheme
25
+ end
26
+
27
+ # constructor
28
+ def initialize(options = {})
29
+ @consumer_key = options[:consumer_key]
30
+ @consumer_secret = options[:consumer_secret]
31
+ @token = options[:token] || ""
32
+ @secret = options[:secret]
33
+ @adapters = {}
34
+ end
35
+
36
+ # authorization
37
+ def authorize(token, secret, options = {})
38
+ request_token = OAuth::RequestToken.new(
39
+ consumer, token, secret
40
+ )
41
+ @access_token = request_token.get_access_token(options)
42
+ @token = @access_token.token
43
+ @secret = @access_token.secret
44
+ @access_token
45
+ end
46
+
47
+ # get the request token
48
+ def request_token(options = {})
49
+ consumer(:scheme => (@@request_scheme || :header)).get_request_token(options)
50
+ end
51
+
52
+ # make a GET request and return raw response
53
+ def get(url)
54
+ raise OAuthUnauthorized if !access_token
55
+ access_token.get(url)
56
+ end
57
+
58
+ # make a POST request and return raw response
59
+ def post(url, params = {})
60
+ raise OAuthUnauthorized if !access_token
61
+ access_token.post(url, params)
62
+ end
63
+
64
+ # json adapter, allowing json.get and json.post methods
65
+ def json
66
+ return @adapters[:json] if @adapters[:json]
67
+ require 'oauth_client/adapters/json'
68
+ @adapters[:json] = OAuthClient::Adapters::Json.new(self)
69
+ end
70
+
71
+ # xml adapter, allowing xml.get and xml.post methods
72
+ def xml
73
+ return @adapters[:xml] if @adapters[:xml]
74
+ require 'oauth_client/adapters/xml'
75
+ @adapters[:xml] = OAuthClient::Adapters::Xml.new(self)
76
+ end
77
+
78
+ private
79
+
80
+ # get the consumer object, with site specified by class variable
81
+ def consumer(options = nil)
82
+ if @consumer and !options
83
+ return @consumer
84
+ end
85
+
86
+ consumer_options = {
87
+ :site=> @@site,
88
+ :http_method => @@http_method || :post,
89
+ :scheme => @@scheme || :header
90
+ }
91
+ consumer_options.merge(options) if options
92
+
93
+ consumer = OAuth::Consumer.new(
94
+ @consumer_key,
95
+ @consumer_secret,
96
+ consumer_options
97
+ )
98
+
99
+ @consumer = consumer unless options
100
+ consumer
101
+ end
102
+
103
+ # get access token used for API access
104
+ def access_token
105
+ @access_token ||= OAuth::AccessToken.new(consumer, @token, @secret)
106
+ end
107
+
108
+ end
109
+
110
+ end
@@ -0,0 +1,5 @@
1
+ module OAuthClient
2
+ module Exceptions
3
+ class OAuthUnauthorized < Exception; end
4
+ end
5
+ end
metadata ADDED
@@ -0,0 +1,84 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: oauth-client
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Marcin Bunch, Marcel Jackwerth, Richard Taylor
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-11-08 00:00:00 +01:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: oauth
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 0.3.1
24
+ version:
25
+ - !ruby/object:Gem::Dependency
26
+ name: json
27
+ type: :runtime
28
+ version_requirement:
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 1.1.2
34
+ version:
35
+ description: Generic OAuth Client for Ruby, based on moomerman/twitter_oauth
36
+ email: marcel@northdocks.com
37
+ executables: []
38
+
39
+ extensions: []
40
+
41
+ extra_rdoc_files:
42
+ - README.textile
43
+ files:
44
+ - .gitignore
45
+ - README.textile
46
+ - Rakefile
47
+ - VERSION
48
+ - demos/tripit_oauth.rb
49
+ - demos/twitter_oauth.rb
50
+ - lib/oauth_client.rb
51
+ - lib/oauth_client/adapters/json.rb
52
+ - lib/oauth_client/adapters/xml.rb
53
+ - lib/oauth_client/client.rb
54
+ - lib/oauth_client/exceptions.rb
55
+ has_rdoc: true
56
+ homepage: http://github.com/sirlantis/oauth-client
57
+ licenses: []
58
+
59
+ post_install_message:
60
+ rdoc_options:
61
+ - --charset=UTF-8
62
+ require_paths:
63
+ - lib
64
+ required_ruby_version: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: "0"
69
+ version:
70
+ required_rubygems_version: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ version: "0"
75
+ version:
76
+ requirements: []
77
+
78
+ rubyforge_project:
79
+ rubygems_version: 1.3.5
80
+ signing_key:
81
+ specification_version: 3
82
+ summary: Generic OAuth Client for Ruby, based on moomerman/twitter_oauth
83
+ test_files: []
84
+