marcinbunsch-oauth_client 0.1.1 → 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README.textile +16 -7
- data/lib/oauth_client.rb +3 -76
- data/lib/oauth_client/adapters/json.rb +26 -0
- data/lib/oauth_client/client.rb +77 -0
- data/lib/oauth_client/exceptions.rb +5 -0
- metadata +5 -2
data/README.textile
CHANGED
@@ -2,25 +2,27 @@ h1. Generic OAuth Client for Ruby, based on moomerman/twitter_oauth
|
|
2
2
|
|
3
3
|
*NOTE*: This is a very early version and has not been tested, give it some time :)
|
4
4
|
|
5
|
-
This is a core OAuth Client, extracting core elements from the an awesome moomerman/twitter_oauth gem. This gem can be used to jump-start your new OAuth client.
|
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
6
|
|
7
7
|
Example:
|
8
8
|
<pre><code>require 'oauth_client'
|
9
|
-
class TwitterOAuth < OAuthClient
|
9
|
+
class TwitterOAuth < OAuthClient::Client
|
10
10
|
site 'http://twitter.com'
|
11
11
|
end</code></pre>
|
12
12
|
|
13
|
-
That's all you need to have a working OAuth client. Of course, it won't do anything just yet :) You need to implement specific methods for the API you're working with.
|
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
14
|
|
15
|
-
You have a few helper methods to ease the trouble. OAuthClient#get and OAuthClient#post return the unparsed response from the server
|
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
16
|
|
17
17
|
See this example from moomerman/twitter_oauth:
|
18
18
|
<pre><code>module TwitterOauth
|
19
19
|
class Client
|
20
|
+
[...]
|
20
21
|
def user(page=1)
|
21
22
|
oauth_response = access_token.get("/statuses/user_timeline.json?page=#{page}")
|
22
23
|
JSON.parse(oauth_response.body)
|
23
24
|
end
|
25
|
+
[...]
|
24
26
|
end
|
25
27
|
end
|
26
28
|
</code></pre>
|
@@ -28,10 +30,17 @@ end
|
|
28
30
|
with OAuthClient, it would look like this:
|
29
31
|
|
30
32
|
<pre><code>require 'oauth_client'
|
31
|
-
class TwitterOAuth < OAuthClient
|
33
|
+
class TwitterOAuth < OAuthClient::Client
|
32
34
|
site 'http://twitter.com'
|
33
35
|
|
34
36
|
def user(page=1)
|
35
|
-
|
37
|
+
json.get("/statuses/user_timeline.json?page=#{page}")
|
36
38
|
end
|
37
|
-
end</code></pre>
|
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/lib/oauth_client.rb
CHANGED
@@ -1,80 +1,7 @@
|
|
1
1
|
require 'rubygems'
|
2
2
|
require 'oauth'
|
3
|
-
require '
|
3
|
+
require 'oauth_client/client'
|
4
|
+
require 'oauth_client/exceptions'
|
4
5
|
|
5
|
-
|
6
|
-
|
7
|
-
# base site
|
8
|
-
class << self
|
9
|
-
attr_accessor :site
|
10
|
-
end
|
11
|
-
|
12
|
-
def self.site(site = nil)
|
13
|
-
@site = site if site
|
14
|
-
@site
|
15
|
-
end
|
16
|
-
|
17
|
-
def initialize(options = {})
|
18
|
-
@consumer_key = options[:consumer_key]
|
19
|
-
@consumer_secret = options[:consumer_secret]
|
20
|
-
@token = options[:token]
|
21
|
-
@secret = options[:secret]
|
22
|
-
end
|
23
|
-
|
24
|
-
# authorization
|
25
|
-
def authorize(token, secret)
|
26
|
-
request_token = OAuth::RequestToken.new(
|
27
|
-
consumer, token, secret
|
28
|
-
)
|
29
|
-
@access_token = request_token.get_access_token
|
30
|
-
@token = @access_token.token
|
31
|
-
@secret = @access_token.secret
|
32
|
-
@access_token
|
33
|
-
end
|
34
|
-
|
35
|
-
def authentication_request_token
|
36
|
-
consumer.options[:authorize_path] = '/oauth/authenticate'
|
37
|
-
request_token
|
38
|
-
end
|
39
|
-
|
40
|
-
def request_token
|
41
|
-
consumer.get_request_token
|
42
|
-
end
|
43
|
-
|
44
|
-
# request helpers
|
45
|
-
def get(url)
|
46
|
-
raise OAuthUnauthorized if !access_token
|
47
|
-
access_token.get(url)
|
48
|
-
end
|
49
|
-
|
50
|
-
def post(url, params = {})
|
51
|
-
raise OAuthUnauthorized if !access_token
|
52
|
-
access_token.post(url, params)
|
53
|
-
end
|
54
|
-
|
55
|
-
def get_json(url)
|
56
|
-
oauth_response = get(url)
|
57
|
-
JSON.parse(oauth_response.body)
|
58
|
-
end
|
59
|
-
|
60
|
-
def post_json(url, params = {})
|
61
|
-
oauth_response = get(url, params)
|
62
|
-
JSON.parse(oauth_response.body)
|
63
|
-
end
|
64
|
-
|
65
|
-
private
|
66
|
-
def consumer
|
67
|
-
@consumer ||= OAuth::Consumer.new(
|
68
|
-
@consumer_key,
|
69
|
-
@consumer_secret,
|
70
|
-
{ :site=> self.class.site }
|
71
|
-
)
|
72
|
-
end
|
73
|
-
|
74
|
-
def access_token
|
75
|
-
@access_token ||= OAuth::AccessToken.new(consumer, @token, @secret)
|
76
|
-
end
|
77
|
-
end
|
78
|
-
|
79
|
-
class OAuthUnauthorized < Exception
|
6
|
+
module OAuthClient
|
80
7
|
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,77 @@
|
|
1
|
+
module OAuthClient
|
2
|
+
class Client
|
3
|
+
|
4
|
+
# base uri for the API
|
5
|
+
class << self
|
6
|
+
attr_accessor :site
|
7
|
+
end
|
8
|
+
|
9
|
+
# class method for setting/getting the base uri for API
|
10
|
+
def self.site(site = nil)
|
11
|
+
@site = site if site
|
12
|
+
@site
|
13
|
+
end
|
14
|
+
|
15
|
+
# constructor
|
16
|
+
def initialize(options = {})
|
17
|
+
@consumer_key = options[:consumer_key]
|
18
|
+
@consumer_secret = options[:consumer_secret]
|
19
|
+
@token = options[:token]
|
20
|
+
@secret = options[:secret]
|
21
|
+
@adapters = {}
|
22
|
+
end
|
23
|
+
|
24
|
+
# authorization
|
25
|
+
def authorize(token, secret)
|
26
|
+
request_token = OAuth::RequestToken.new(
|
27
|
+
consumer, token, secret
|
28
|
+
)
|
29
|
+
@access_token = request_token.get_access_token
|
30
|
+
@token = @access_token.token
|
31
|
+
@secret = @access_token.secret
|
32
|
+
@access_token
|
33
|
+
end
|
34
|
+
|
35
|
+
# get the request token
|
36
|
+
def request_token
|
37
|
+
consumer.get_request_token
|
38
|
+
end
|
39
|
+
|
40
|
+
# make a GET request and return raw response
|
41
|
+
def get(url)
|
42
|
+
raise OAuthUnauthorized if !access_token
|
43
|
+
access_token.get(url)
|
44
|
+
end
|
45
|
+
|
46
|
+
# make a POST request and return raw response
|
47
|
+
def post(url, params = {})
|
48
|
+
raise OAuthUnauthorized if !access_token
|
49
|
+
access_token.post(url, params)
|
50
|
+
end
|
51
|
+
|
52
|
+
# json adapter, allowing json.get and json.post methods
|
53
|
+
def json
|
54
|
+
return @adapters[:json] if @adapters[:json]
|
55
|
+
require 'oauth_client/adapters/json'
|
56
|
+
@adapters[:json] = OAuthClient::Adapters::Json.new(self)
|
57
|
+
end
|
58
|
+
|
59
|
+
private
|
60
|
+
|
61
|
+
# get the consumer object, with site specified by class variable
|
62
|
+
def consumer
|
63
|
+
@consumer ||= OAuth::Consumer.new(
|
64
|
+
@consumer_key,
|
65
|
+
@consumer_secret,
|
66
|
+
{ :site=> self.class.site }
|
67
|
+
)
|
68
|
+
end
|
69
|
+
|
70
|
+
# get access token used for API access
|
71
|
+
def access_token
|
72
|
+
@access_token ||= OAuth::AccessToken.new(consumer, @token, @secret)
|
73
|
+
end
|
74
|
+
|
75
|
+
end
|
76
|
+
|
77
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: marcinbunsch-oauth_client
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Marcin Bunsch, Richard Taylor
|
@@ -43,7 +43,10 @@ extra_rdoc_files: []
|
|
43
43
|
files:
|
44
44
|
- README.textile
|
45
45
|
- lib/oauth_client.rb
|
46
|
-
|
46
|
+
- lib/oauth_client/client.rb
|
47
|
+
- lib/oauth_client/exceptions.rb
|
48
|
+
- lib/oauth_client/adapters/json.rb
|
49
|
+
has_rdoc: true
|
47
50
|
homepage: http://github.com/marcinbunsch/oauth_client
|
48
51
|
post_install_message:
|
49
52
|
rdoc_options:
|