mbleigh-twitter-auth 0.1.3 → 0.1.5

Sign up to get free protection for your applications and to get access to all the features.
data/README.markdown CHANGED
@@ -6,11 +6,9 @@ TwitterAuth aims to provide a complete authentication and API access solution fo
6
6
  Installation
7
7
  ============
8
8
 
9
- **NOTE:** The GemPlugin version of TwitterAuth is currently broken...Rails isn't picking up namespaced models in the Engines. I'm working on a fix, but please use the plugin version until then.
10
-
11
9
  You can include TwitterAuth as a gem in your project like so:
12
10
 
13
- config.gem 'mbleigh-twitter-auth', :source => 'http://gems.github.com'
11
+ config.gem 'twitter-auth', :lib => 'twitter_auth'
14
12
 
15
13
  Or you can install it as a traditional Rails plugin:
16
14
 
data/Rakefile CHANGED
@@ -24,6 +24,7 @@ s.files = FileList["[A-Z]*", "{bin,generators,lib,spec,config,app,rails}/**/*"]
24
24
  s.authors = ["Michael Bleigh"]
25
25
  s.add_dependency('oauth', '>= 0.3.1')
26
26
  s.add_dependency('ezcrypto', '>= 0.7.2')
27
+ s.rubyforge_project = 'twitter-auth'
27
28
  end
28
29
  rescue LoadError
29
30
  puts "Jeweler not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
data/VERSION.yml CHANGED
@@ -1,4 +1,4 @@
1
1
  ---
2
2
  :minor: 1
3
- :patch: 3
3
+ :patch: 5
4
4
  :major: 0
@@ -3,6 +3,8 @@ require 'net/http'
3
3
  module TwitterAuth
4
4
  module Dispatcher
5
5
  class Basic
6
+ include TwitterAuth::Dispatcher::Shared
7
+
6
8
  attr_accessor :user
7
9
 
8
10
  def initialize(user)
@@ -20,9 +22,7 @@ module TwitterAuth
20
22
  http.request(req)
21
23
  }
22
24
 
23
- JSON.parse(response.body)
24
- rescue JSON::ParserError
25
- response.body
25
+ handle_response(response)
26
26
  end
27
27
 
28
28
  def get(path, *arguments)
@@ -3,6 +3,8 @@ require 'oauth'
3
3
  module TwitterAuth
4
4
  module Dispatcher
5
5
  class Oauth < OAuth::AccessToken
6
+ include TwitterAuth::Dispatcher::Shared
7
+
6
8
  attr_accessor :user
7
9
 
8
10
  def initialize(user)
@@ -14,9 +16,8 @@ module TwitterAuth
14
16
  def request(http_method, path, *arguments)
15
17
  path << '.json' unless path.match(/\.(:?xml|json)\z/i)
16
18
  response = super
17
- JSON.parse(response.body)
18
- rescue JSON::ParserError
19
- response.body
19
+
20
+ handle_response(response)
20
21
  end
21
22
  end
22
23
  end
@@ -0,0 +1,28 @@
1
+ module TwitterAuth
2
+ module Dispatcher
3
+ module Shared
4
+ def handle_response(response)
5
+ case response
6
+ when Net::HTTPOK
7
+ begin
8
+ JSON.parse(response.body)
9
+ rescue JSON::ParserError
10
+ response.body
11
+ end
12
+ else
13
+ message = begin
14
+ JSON.parse(response.body)['error']
15
+ rescue JSON::ParserError
16
+ if match = response.body.match(/<error>(.*)<\/error>/)
17
+ match[1]
18
+ else
19
+ 'An error occurred processing your Twitter request.'
20
+ end
21
+ end
22
+
23
+ raise TwitterAuth::Dispatcher::Error, message
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
data/lib/twitter_auth.rb CHANGED
@@ -67,3 +67,7 @@ end
67
67
  require 'twitter_auth/controller_extensions'
68
68
  require 'twitter_auth/cryptify'
69
69
  require 'twitter_auth/dispatcher/oauth'
70
+ require 'twitter_auth/dispatcher/basic'
71
+ require 'twitter_auth/dispatcher/shared'
72
+
73
+ class TwitterAuth::Dispatcher::Error < StandardError; end
@@ -47,6 +47,21 @@ describe TwitterAuth::Dispatcher::Basic do
47
47
  @net.should_receive(:start)
48
48
  lambda{@dispatcher.request(:get, '/fake')}.should raise_error(NoMethodError)
49
49
  end
50
+
51
+ it "should raise a TwitterAuth::Dispatcher::Error if response code isn't 200" do
52
+ FakeWeb.register_uri('https://twitter.com:443/bad_response.json', :string => {'error' => 'bad response'}.to_json, :status => ['401', 'Unauthorized'])
53
+ lambda{@dispatcher.request(:get, '/bad_response')}.should raise_error(TwitterAuth::Dispatcher::Error)
54
+ end
55
+
56
+ it 'should set the error message to the JSON message' do
57
+ FakeWeb.register_uri('https://twitter.com:443/bad_response.json', :string => {'error' => 'bad response'}.to_json, :status => ['401', 'Unauthorized'])
58
+ lambda{@dispatcher.request(:get, '/bad_response')}.should raise_error(TwitterAuth::Dispatcher::Error, 'bad response')
59
+ end
60
+
61
+ it 'should set the error message to the XML message' do
62
+ FakeWeb.register_uri('https://twitter.com:443/bad_response.xml', :string => "<hash>\n<request>/bad_response.xml</request>\n<error>bad response</error>\n</hash>", :status => ['401', 'Unauthorized'])
63
+ lambda{@dispatcher.request(:get, '/bad_response')}.should raise_error(TwitterAuth::Dispatcher::Error, 'bad response')
64
+ end
50
65
  end
51
66
 
52
67
  %w(get post delete put).each do |method|
@@ -45,6 +45,21 @@ describe TwitterAuth::Dispatcher::Oauth do
45
45
  @dispatcher.request(:get, '/fake').should == @dispatcher.request(:get, '/fake.json')
46
46
  end
47
47
 
48
+ it "should raise a TwitterAuth::Dispatcher::Error if response code isn't 200" do
49
+ FakeWeb.register_uri('https://twitter.com:443/bad_response.json', :string => {'error' => 'bad response'}.to_json, :status => ['401', 'Unauthorized'])
50
+ lambda{@dispatcher.request(:get, '/bad_response')}.should raise_error(TwitterAuth::Dispatcher::Error)
51
+ end
52
+
53
+ it 'should set the error message to the JSON message' do
54
+ FakeWeb.register_uri('https://twitter.com:443/bad_response.json', :string => {'error' => 'bad response'}.to_json, :status => ['401', 'Unauthorized'])
55
+ lambda{@dispatcher.request(:get, '/bad_response')}.should raise_error(TwitterAuth::Dispatcher::Error, 'bad response')
56
+ end
57
+
58
+ it 'should set the error message to the XML message' do
59
+ FakeWeb.register_uri('https://twitter.com:443/bad_response.xml', :string => "<hash>\n<request>/bad_response.xml</request>\n<error>bad response</error>\n</hash>", :status => ['401', 'Unauthorized'])
60
+ lambda{@dispatcher.request(:get, '/bad_response')}.should raise_error(TwitterAuth::Dispatcher::Error, 'bad response')
61
+ end
62
+
48
63
  it 'should work with verb methods' do
49
64
  @dispatcher.get('/fake').should == @dispatcher.request(:get, '/fake')
50
65
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mbleigh-twitter-auth
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michael Bleigh
@@ -67,6 +67,7 @@ files:
67
67
  - lib/twitter_auth/dispatcher
68
68
  - lib/twitter_auth/dispatcher/basic.rb
69
69
  - lib/twitter_auth/dispatcher/oauth.rb
70
+ - lib/twitter_auth/dispatcher/shared.rb
70
71
  - lib/twitter_auth.rb
71
72
  - lib/twitteresque
72
73
  - spec/controllers
@@ -130,7 +131,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
130
131
  version:
131
132
  requirements: []
132
133
 
133
- rubyforge_project:
134
+ rubyforge_project: twitter-auth
134
135
  rubygems_version: 1.2.0
135
136
  signing_key:
136
137
  specification_version: 2