marcinbunsch-oauth_client 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/README.textile +37 -0
  2. data/lib/oauth_client.rb +80 -0
  3. metadata +74 -0
@@ -0,0 +1,37 @@
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. 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
10
+ site 'http://twitter.com'
11
+ end</code></pre>
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.
14
+
15
+ You have a few helper methods to ease the trouble. OAuthClient#get and OAuthClient#post return the unparsed response from the server, whereas OAuthClient#get_json and OAuthClient#post_json parse JSON and return the result
16
+
17
+ See this example from moomerman/twitter_oauth:
18
+ <pre><code>module TwitterOauth
19
+ class Client
20
+ def user(page=1)
21
+ oauth_response = access_token.get("/statuses/user_timeline.json?page=#{page}")
22
+ JSON.parse(oauth_response.body)
23
+ end
24
+ end
25
+ end
26
+ </code></pre>
27
+
28
+ with OAuthClient, it would look like this:
29
+
30
+ <pre><code>require 'oauth_client'
31
+ class TwitterOAuth < OAuthClient
32
+ site 'http://twitter.com'
33
+
34
+ def user(page=1)
35
+ get_json("/statuses/user_timeline.json?page=#{page}")
36
+ end
37
+ end</code></pre>
@@ -0,0 +1,80 @@
1
+ require 'rubygems'
2
+ require 'oauth'
3
+ require 'json'
4
+
5
+ class OAuthClient
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
80
+ end
metadata ADDED
@@ -0,0 +1,74 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: marcinbunsch-oauth_client
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Marcin Bunsch, Richard Taylor
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-06-09 00:00:00 -07: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: marcin@applicake.com
37
+ executables: []
38
+
39
+ extensions: []
40
+
41
+ extra_rdoc_files: []
42
+
43
+ files:
44
+ - README.textile
45
+ - lib/oauth_client.rb
46
+ has_rdoc: false
47
+ homepage: http://github.com/marcinbunsch/oauth_client
48
+ post_install_message:
49
+ rdoc_options:
50
+ - --inline-source
51
+ - --charset=UTF-8
52
+ require_paths:
53
+ - lib
54
+ required_ruby_version: !ruby/object:Gem::Requirement
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ version: "0"
59
+ version:
60
+ required_rubygems_version: !ruby/object:Gem::Requirement
61
+ requirements:
62
+ - - ">="
63
+ - !ruby/object:Gem::Version
64
+ version: "0"
65
+ version:
66
+ requirements: []
67
+
68
+ rubyforge_project:
69
+ rubygems_version: 1.2.0
70
+ signing_key:
71
+ specification_version: 2
72
+ summary: Generic OAuth Client for Ruby, based on moomerman/twitter_oauth.
73
+ test_files: []
74
+