rapidash 0.0.5 → 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b7cf2002fd2e632f1c5150d3d3a5d858d58ec09c
4
- data.tar.gz: 6de1e547b1243cce13a8440a41ac28113e191843
3
+ metadata.gz: 25d2edf71b1e0e81782f4d56e4926b963c57e053
4
+ data.tar.gz: 6982fb798abb4b49ba0ce54f72bca5c3086cb56b
5
5
  SHA512:
6
- metadata.gz: b64aa96d960b5a4bb1e2e50bd7c16c262e40fe57548745d1410385c3ce25f8b3374dc2673da012c0d9c5038c21e0e3746eb3fe4405a8c30b1a8b308d2215cd64
7
- data.tar.gz: 9e1d5838e548568064e63bfe8ff2c3cc173d308c7a4f5c9c79615cb7fdf29fedeb837c51c352ec0d5813643bba8c4598b9e1e2e6eb467ba9dc9c3b253798c777
6
+ metadata.gz: c769c2bcb2074211878609785ca58e2fae9840861f6a6cb6636760b6b22dd97b65e0330a9c110724e1f247e5f2d35fe96395056d23239fe1d9325b987863866c
7
+ data.tar.gz: 7bbc9e3833e52d99c723a43f088f2b3abe51d720f9366993f845aac55df7c9d2f05c4406f71c26f4ce856ed35a270c9acf944719fa55642b69a300adbe52dea5
data/README.md CHANGED
@@ -74,7 +74,7 @@ The main thing a client must do is define a method, `oauth` and `http` are curre
74
74
  ```ruby
75
75
  class Client < Rapidash::Client
76
76
  method :oauth
77
- resource :users
77
+ resource :users, :repos #An array can be passed through
78
78
  use_patch # This will use PATCH when updating instead of POST
79
79
  extension :json #Append the extension fo the urls
80
80
  end
@@ -0,0 +1,64 @@
1
+ require 'rapidash'
2
+
3
+ class Repos < Rapidash::Base
4
+ def repo!(owner, repo)
5
+ self.url += "/#{owner}/#{repo}"
6
+ call!
7
+ end
8
+ end
9
+
10
+ class Users < Rapidash::Base
11
+ resource :repos
12
+ end
13
+
14
+ class Emojis < Rapidash::Base
15
+ end
16
+
17
+ class Events < Rapidash::Base
18
+ end
19
+
20
+ class Gists < Rapidash::Base
21
+
22
+ def public!
23
+ self.url += "/public"
24
+ call!
25
+ end
26
+
27
+ end
28
+
29
+ class Organisations < Rapidash::Base
30
+ url "orgs"
31
+ end
32
+
33
+ class Limit < Rapidash::Base
34
+ url "rate_limit"
35
+ end
36
+
37
+
38
+ class Client < Rapidash::Client
39
+ method :http
40
+ site "https://api.github.com/"
41
+ resource :users, :repos, :emojis, :events, :gists, :organisations, :limit
42
+ end
43
+
44
+ client = Client.new
45
+
46
+ p client.limit!.rate.remaining
47
+
48
+ p client.gists.public!
49
+
50
+ client.users("Gazler").repos!.each do |repo|
51
+ p repo.name
52
+ end
53
+
54
+ client.emojis!.each do |emoji|
55
+ p emoji
56
+ end
57
+
58
+ client.events!.each do |event|
59
+ p event
60
+ end
61
+
62
+ p client.organisations!("powershift")
63
+
64
+ p client.repos.repo!("Gazler", "rapidash")
@@ -7,7 +7,7 @@ module Rapidash
7
7
 
8
8
  module ClassMethods
9
9
 
10
- attr_accessor :patch, :url_extension
10
+ attr_accessor :patch, :url_extension, :raise_error
11
11
 
12
12
  def method(method)
13
13
  case method
@@ -26,6 +26,10 @@ module Rapidash
26
26
  def extension(extension)
27
27
  @url_extension = extension
28
28
  end
29
+
30
+ def raise_errors
31
+ @raise_error = true
32
+ end
29
33
  end
30
34
  end
31
35
  end
@@ -1,4 +1,5 @@
1
1
  module Rapidash
2
2
  class ParseError < StandardError; end
3
3
  class ConfigurationError < StandardError; end
4
+ class ResponseError < StandardError; end
4
5
  end
@@ -33,6 +33,10 @@ module Rapidash
33
33
 
34
34
  # "foo"[0] does not work in 1.8.7, "foo"[0,1] is required
35
35
  case response.status.to_s[0,1]
36
+ when "5", "4"
37
+ error = ResponseError.new(response)
38
+ raise error if self.class.respond_to?(:raise_error) && self.class.raise_error
39
+ return nil
36
40
  #Handle redirects
37
41
  when "3"
38
42
  request(verb, response.headers["location"], options)
@@ -23,17 +23,9 @@ module Rapidash
23
23
  elsif self.class.respond_to?(:url_extension) && self.class.url_extension
24
24
  url = "#{url}.#{(self.class.url_extension)}"
25
25
  end
26
+ options[:raise_errors] = self.class.respond_to?(:raise_error) && self.class.raise_error
26
27
  response = oauth_access_token.send(verb.to_sym, "#{site}/#{url}", options)
27
- body = JSON.parse(response.body)
28
- if body.kind_of?(Hash)
29
- return Hashie::Mash.new(body)
30
- elsif body.kind_of?(Array)
31
- output = []
32
- body.each do |el|
33
- output << Hashie::Mash.new(el)
34
- end
35
- return output
36
- end
28
+ return Response.new(response)
37
29
  end
38
30
 
39
31
  def access_token_from_code(code, url)
@@ -7,40 +7,40 @@ module Rapidash
7
7
 
8
8
  module ClassMethods
9
9
 
10
- def resource(name)
10
+ def resource(*names)
11
11
  mod = self.to_s.split("::")[0...-1]
12
12
  if mod.empty?
13
13
  mod = Kernel
14
14
  else
15
15
  mod = Kernel.const_get(mod.join("::"))
16
16
  end
17
- klass = mod.const_get(name.to_s.capitalize)
18
17
 
19
- def get_client(me)
20
- client = me
21
- if me.respond_to?(:client)
22
- client = me.client
23
- end
24
- client
25
- end
26
18
 
27
- mod = self
28
19
 
29
- define_method(name) do |*args|
30
- if self.respond_to?(:url)
31
- options = {:previous_url => self.url}
32
- if args[args.length].is_a?(Hash)
33
- args[args.length].merge!(options)
34
- else
35
- args << options
20
+ names.each do |name|
21
+ klass = mod.const_get(name.to_s.capitalize)
22
+
23
+ define_method(name) do |*args|
24
+ if self.respond_to?(:url)
25
+ options = {:previous_url => self.url}
26
+ if args[args.length].is_a?(Hash)
27
+ args[args.length].merge!(options)
28
+ else
29
+ args << options
30
+ end
36
31
  end
32
+ client = self
33
+ client = self.client if self.respond_to?(:client)
34
+ klass.new(client, *args)
35
+ end
36
+
37
+ define_method("#{name}!".to_sym) do |*args|
38
+ self.send(name, *args).call!
37
39
  end
38
- klass.new(mod.get_client(self), *args)
39
- end
40
- define_method("#{name}!".to_sym) do |*args|
41
- self.send(name, *args).call!
42
40
  end
43
41
  end
42
+
44
43
  end
44
+
45
45
  end
46
46
  end
@@ -1,3 +1,3 @@
1
1
  module Rapidash
2
- VERSION = "0.0.5"
2
+ VERSION = "0.0.6"
3
3
  end
@@ -18,6 +18,11 @@ class HTTPClientExtensionTester < HTTPClientTester
18
18
  extension :json
19
19
  end
20
20
 
21
+ class HTTPClientErrorTester < HTTPClientTester
22
+ raise_errors
23
+ end
24
+
25
+
21
26
  class TestClientTester
22
27
  include Rapidash::Clientable
23
28
  method :test
@@ -74,4 +79,10 @@ describe Rapidash::Clientable do
74
79
  end
75
80
  end
76
81
 
82
+ describe "#raise_errors" do
83
+ it "should set the raise_error variable" do
84
+ HTTPClientErrorTester.new.class.instance_variable_get(:@raise_error).should eql(true)
85
+ end
86
+ end
87
+
77
88
  end
@@ -15,6 +15,12 @@ class HTTPExtensionTester < HTTPTester
15
15
  end
16
16
  end
17
17
 
18
+ class HTTPErrorTester < HTTPTester
19
+ def self.raise_error
20
+ true
21
+ end
22
+ end
23
+
18
24
  describe Rapidash::HTTPClient do
19
25
 
20
26
  let!(:subject) { HTTPTester.new }
@@ -57,37 +63,62 @@ describe Rapidash::HTTPClient do
57
63
 
58
64
  let!(:valid_response) { OpenStruct.new(:status => "200")}
59
65
  let!(:redirect_response) { OpenStruct.new(:status => "301", :headers => {"location" => "http://example.com/redirect"})}
66
+ let!(:error_response) { OpenStruct.new(:status => "404")}
60
67
 
61
68
  before(:each) do
62
69
  subject.site = "http://example.com"
63
- Rapidash::Response.should_receive(:new).and_return("response")
64
70
  end
65
71
 
66
- it "should add an extension if one is set" do
67
- subject.extension = :json
68
- subject.connection.should_receive(:run_request).with(:get, "http://example.com/foo.json", nil, nil).and_return(valid_response)
69
- subject.request(:get, "foo")
70
- end
72
+ describe "valid response" do
73
+
74
+ before(:each) do
75
+ Rapidash::Response.should_receive(:new).and_return("response")
76
+ end
77
+
78
+ it "should add an extension if one is set" do
79
+ subject.extension = :json
80
+ subject.connection.should_receive(:run_request).with(:get, "http://example.com/foo.json", nil, nil).and_return(valid_response)
81
+ subject.request(:get, "foo")
82
+ end
83
+
84
+ it "should use the class extension if one is set" do
85
+ subject = HTTPExtensionTester.new
86
+ subject.connection.should_receive(:run_request).with(:get, "http://mysite.com/foo.json", nil, nil).and_return(valid_response)
87
+ subject.request(:get, "foo")
88
+ end
89
+
90
+
91
+ it "should return a response object" do
92
+ subject.connection.should_receive(:run_request).with(:get, "http://example.com/foo", nil, nil).and_return(valid_response)
93
+ response = subject.request(:get, "foo")
94
+ response.should eql("response")
95
+ end
96
+
97
+ it "should perform a redirect" do
98
+ subject.connection.should_receive(:run_request).with(:get, "http://example.com/foo", nil, nil).and_return(redirect_response)
99
+ subject.connection.should_receive(:run_request).with(:get, "http://example.com/redirect", nil, nil).and_return(valid_response)
100
+ response = subject.request(:get, "foo")
101
+ response.should eql("response")
102
+ end
71
103
 
72
- it "should use the class extension if one is set" do
73
- subject = HTTPExtensionTester.new
74
- subject.connection.should_receive(:run_request).with(:get, "http://mysite.com/foo.json", nil, nil).and_return(valid_response)
75
- subject.request(:get, "foo")
76
104
  end
77
105
 
106
+ describe "error response" do
107
+
108
+ it "should not raise an error by default" do
109
+ subject.connection.should_receive(:run_request).with(:get, "http://example.com/error", nil, nil).and_return(error_response)
110
+ response = subject.request(:get, "error")
111
+ response.should be_nil
112
+ end
113
+
114
+ it "should raise an error if the option is set" do
115
+ subject = HTTPErrorTester.new
116
+ subject.connection.should_receive(:run_request).with(:get, anything, nil, nil).and_return(error_response)
117
+ expect {
118
+ response = subject.request(:get, "error")
119
+ }.to raise_error(Rapidash::ResponseError)
120
+ end
78
121
 
79
- it "should return a response object" do
80
- subject.connection.should_receive(:run_request).with(:get, "http://example.com/foo", nil, nil).and_return(valid_response)
81
- response = subject.request(:get, "foo")
82
- response.should eql("response")
83
- end
84
-
85
- it "should perform a redirect" do
86
- subject.connection.should_receive(:run_request).with(:get, "http://example.com/foo", nil, nil).and_return(redirect_response)
87
- subject.connection.should_receive(:run_request).with(:get, "http://example.com/redirect", nil, nil).and_return(valid_response)
88
- response = subject.request(:get, "foo")
89
- response.should eql("response")
90
122
  end
91
123
  end
92
-
93
124
  end
@@ -10,16 +10,26 @@ class OAuthExtensionTester < OAuthTester
10
10
  end
11
11
  end
12
12
 
13
+ class OAuthErrorTester < OAuthTester
14
+ def self.raise_error
15
+ true
16
+ end
17
+ end
18
+
13
19
  describe Rapidash::OAuthClient do
14
20
 
15
- let(:options) do
21
+ before(:each) do
22
+ Rapidash::Response.stub(:new).and_return(Hashie::Mash.new)
23
+ end
24
+
25
+ let(:options) {
16
26
  {
17
27
  :uid => "foo",
18
28
  :secret => "bar",
19
29
  :access_token => "baz",
20
30
  :site => "http://example.com"
21
31
  }
22
- end
32
+ }
23
33
 
24
34
  let(:subject) { OAuthTester.new(options) }
25
35
 
@@ -59,13 +69,10 @@ describe Rapidash::OAuthClient do
59
69
 
60
70
  describe "object returned from API call" do
61
71
 
62
- let(:body) { {:foo => "bar"}.to_json }
63
- let(:response) { OpenStruct.new(:body => body) }
64
-
65
72
  before(:each) do
66
73
  subject.extension = :json
67
74
  subject.stub(:oauth_access_token).and_return(request)
68
- request.stub(:get).and_return(response)
75
+ request.stub(:get)
69
76
  end
70
77
 
71
78
  it "should add an extension to the url if one is set" do
@@ -76,7 +83,7 @@ describe Rapidash::OAuthClient do
76
83
  it "should use the class extension if one is set" do
77
84
  subject = OAuthExtensionTester.new(options)
78
85
  subject.stub(:oauth_access_token).and_return(request)
79
- request.stub(:get).and_return(response)
86
+ request.stub(:get)
80
87
  request.should_receive(:get).with("http://example.com/me.json", anything)
81
88
  subject.request(:get, "me")
82
89
  end
@@ -86,31 +93,16 @@ describe Rapidash::OAuthClient do
86
93
  subject.request(:get, "me").class.should eql(Hashie::Mash)
87
94
  end
88
95
 
89
- it "should return a traversable object" do
90
- subject.request(:get, "me").foo.should eql("bar")
91
- end
92
-
93
96
  end
94
97
 
95
- describe "array returned from API call" do
96
-
97
- let(:body) { [{:foo => "bar"}, {:baz => "bra"}].to_json }
98
- let(:response) { OpenStruct.new(:body => body) }
99
-
100
- before(:each) do
98
+ describe "when errors are set" do
99
+ it "should call oauth_access_token.send with errors set" do
100
+ subject = OAuthErrorTester.new(options)
101
101
  subject.stub(:oauth_access_token).and_return(request)
102
- request.stub(:get).and_return(response)
103
- end
104
- it "should return an array" do
105
- subject.request(:get, "me").class.should eql(Array)
102
+ request.should_receive(:send).with(:get, "http://example.com/error", {:raise_errors => true})
103
+ subject.request(:get, "error")
106
104
  end
107
-
108
- it "should return a traversable object" do
109
- response = subject.request(:get, "me")
110
- response[0].foo.should eql("bar")
111
- response[1].baz.should eql("bra")
112
- end
113
-
105
+
114
106
  end
115
107
  end
116
108
 
@@ -28,6 +28,11 @@ class Rapidash::ClientTester
28
28
  resource :users
29
29
  end
30
30
 
31
+ class Rapidash::MultiResourceTester
32
+ include Rapidash::Resourceable
33
+ resource :users, :repos
34
+ end
35
+
31
36
  class ClientTester
32
37
  include Rapidash::Resourceable
33
38
  resource :users
@@ -50,6 +55,11 @@ describe Rapidash::Resourceable do
50
55
  it "should add a bang method with the name of the argument" do
51
56
  Rapidash::ClientTester.new.methods.map { |m| m.to_sym }.should include(:users!)
52
57
  end
58
+
59
+ it "should add a method for each resource is an array is passed" do
60
+ methods = Rapidash::MultiResourceTester.new.methods.map { |m| m.to_sym }
61
+ (methods & [:users, :users!, :repos, :repos!]).length.should eql(4)
62
+ end
53
63
  end
54
64
 
55
65
  describe ".users" do
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rapidash
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gary 'Gazler' Rennie
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-03-10 00:00:00.000000000 Z
11
+ date: 2013-03-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -122,6 +122,7 @@ files:
122
122
  - LICENSE.txt
123
123
  - README.md
124
124
  - Rakefile
125
+ - examples/github.rb
125
126
  - lib/rapidash.rb
126
127
  - lib/rapidash/base.rb
127
128
  - lib/rapidash/client.rb