rapidash 0.1.2 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -3,23 +3,24 @@ require 'hashie'
3
3
 
4
4
  module Rapidash
5
5
  class Response
6
-
7
6
  class << self
8
7
  def new(response)
9
8
  return nil unless response.body
9
+ return nil if response.body.empty?
10
+ return nil if response.body == "null"
10
11
  type = response.headers["content-type"]
11
- body = JSON.parse(response.body)
12
- if body.kind_of?(Hash)
13
- return Hashie::Mash.new(body)
14
- elsif body.kind_of?(Array)
15
- output = []
16
- body.each do |el|
17
- output << Hashie::Mash.new(el)
18
- end
19
- return output
12
+ body = JSON.parse(response.body)
13
+ if body.kind_of?(Hash)
14
+ return Hashie::Mash.new(body)
15
+ elsif body.kind_of?(Array)
16
+ output = []
17
+ body.each do |el|
18
+ output << Hashie::Mash.new(el)
20
19
  end
20
+ return output
21
+ end
21
22
  rescue JSON::ParserError => e
22
- raise ParseError.new("Failed to parse content for type: #{response.headers["content-type"]}")
23
+ raise ParseError.new("Failed to parse content for type: #{type}")
23
24
  end
24
25
  end
25
26
 
@@ -1,6 +1,5 @@
1
1
  module Rapidash
2
2
  module TestClient
3
-
4
3
  attr_accessor :responses
5
4
 
6
5
  def initialize(options = {})
@@ -1,6 +1,5 @@
1
1
  module Rapidash
2
2
  module Urlable
3
-
4
3
  def self.included(base)
5
4
  base.extend(ClassMethods)
6
5
  end
@@ -14,6 +13,5 @@ module Rapidash
14
13
  end
15
14
  end
16
15
  end
17
-
18
16
  end
19
17
  end
@@ -1,3 +1,3 @@
1
1
  module Rapidash
2
- VERSION = "0.1.2"
2
+ VERSION = "0.2.0"
3
3
  end
@@ -21,10 +21,12 @@ Gem::Specification.new do |spec|
21
21
  spec.add_development_dependency "bundler", "~> 1.0"
22
22
  spec.add_development_dependency "rake"
23
23
  spec.add_development_dependency "rspec", "~> 2.8"
24
+ spec.add_development_dependency "simplecov", "~> 0.7"
24
25
 
25
26
  spec.add_dependency 'json'
26
27
  spec.add_dependency 'faraday', '~> 0.8'
27
28
  spec.add_dependency "oauth2", "~>0.6"
28
29
  spec.add_dependency "hashie", "~>1.2"
30
+ spec.add_dependency "activesupport"
29
31
 
30
32
  end
@@ -26,11 +26,11 @@ describe Rapidash::Base do
26
26
  describe ".initialize" do
27
27
 
28
28
  it "should assume a default based on the class name" do
29
- Base.new.instance_variable_get(:@url).should eql("base")
29
+ Base.new.instance_variable_get(:@url).should eql("bases")
30
30
  end
31
31
 
32
32
  it "should ignore any modules when infering the URL" do
33
- Rapidash::Resource.new.instance_variable_get(:@url).should eql("resource")
33
+ Rapidash::Resource.new.instance_variable_get(:@url).should eql("resources")
34
34
  end
35
35
 
36
36
  it "should override the URL if set" do
@@ -60,7 +60,7 @@ describe Rapidash::Base do
60
60
  subject.create!(post)
61
61
  subject.instance_variable_get(:@options).should eql({
62
62
  :method => :post,
63
- :body => post.to_json
63
+ :body => post
64
64
  })
65
65
  end
66
66
 
@@ -70,7 +70,7 @@ describe Rapidash::Base do
70
70
  subject.create!(no_root)
71
71
  subject.instance_variable_get(:@options).should eql({
72
72
  :method => :post,
73
- :body => post.to_json
73
+ :body => post
74
74
  })
75
75
  end
76
76
  end
@@ -81,7 +81,7 @@ describe Rapidash::Base do
81
81
  subject.update!(post)
82
82
  subject.instance_variable_get(:@options).should eql({
83
83
  :method => :put,
84
- :body => post.to_json
84
+ :body => post
85
85
  })
86
86
  end
87
87
 
@@ -91,7 +91,7 @@ describe Rapidash::Base do
91
91
  subject.update!(post)
92
92
  subject.instance_variable_get(:@options).should eql({
93
93
  :method => :patch,
94
- :body => post.to_json
94
+ :body => post
95
95
  })
96
96
  end
97
97
 
@@ -101,7 +101,7 @@ describe Rapidash::Base do
101
101
  subject.update!(no_root)
102
102
  subject.instance_variable_get(:@options).should eql({
103
103
  :method => :patch,
104
- :body => post.to_json
104
+ :body => post
105
105
  })
106
106
  end
107
107
  end
@@ -142,4 +142,16 @@ describe Rapidash::Base do
142
142
  end
143
143
  end
144
144
 
145
+ describe ".resource_url" do
146
+ it "should return the class name as a url if none is specified" do
147
+ subject.send(:resource_url).should eql("basetesters")
148
+ end
149
+
150
+ it "should return the previous url if set" do
151
+ subject.options = {:url => "people"}
152
+ subject.send(:resource_url).should eql("people")
153
+ end
154
+ end
155
+
156
+
145
157
  end
@@ -4,16 +4,103 @@ class Client < Rapidash::Client
4
4
  method :test
5
5
  end
6
6
 
7
+
8
+ class OAuthClientTester < Rapidash::Client
9
+ method :oauth
10
+ end
11
+
12
+ class HTTPClientTester < Rapidash::Client
13
+ method :http
14
+ end
15
+
16
+ class HTTPClientPatchTester < HTTPClientTester
17
+ use_patch
18
+ end
19
+
20
+ class HTTPClientExtensionTester < HTTPClientTester
21
+ extension :js
22
+ end
23
+
24
+ class HTTPClientErrorTester < HTTPClientTester
25
+ raise_errors
26
+ end
27
+
28
+ class TestClientTester
29
+ method :test
30
+ end
31
+
7
32
  describe Rapidash::Client do
8
33
 
9
34
  let!(:subject) { Client.new }
10
35
 
36
+ describe "#method" do
37
+ it "should include the HTTPClient" do
38
+ client = HTTPClientTester.new
39
+ client.class.ancestors.should include(Rapidash::HTTPClient)
40
+ end
41
+
42
+ it "should include the OAuthClient" do
43
+ client = OAuthClientTester.new({:uid => "foo", :secret => "bar", :site => "baz"})
44
+ client.class.ancestors.should include(Rapidash::OAuthClient)
45
+ end
46
+
47
+ it "should include the OAuthClient" do
48
+ client = TestClientTester.new
49
+ client.class.ancestors.should include(Rapidash::TestClient)
50
+ end
51
+
52
+ it "should raise an error on anything else" do
53
+ expect {
54
+ class InvalidClientTester < Rapidash::Client
55
+ method :invalid
56
+ end
57
+ }.to raise_error(Rapidash::ConfigurationError)
58
+ end
59
+ end
60
+
61
+ describe "#use_patch" do
62
+ it "should set the patch variable to true" do
63
+ HTTPClientPatchTester.new.class.instance_variable_get(:@patch).should eql(true)
64
+ end
65
+ end
66
+
67
+ describe "#extension" do
68
+ it "should set the url_extension variable" do
69
+ HTTPClientExtensionTester.new.class.instance_variable_get(:@extension).should eql(:js)
70
+ end
71
+ end
72
+
73
+ describe "#raise_errors" do
74
+ it "should set the raise_error variable" do
75
+ HTTPClientErrorTester.new.class.instance_variable_get(:@raise_error).should eql(true)
76
+ end
77
+ end
78
+
79
+
11
80
  it "should raise an error when instantiated" do
12
81
  expect {
13
82
  Rapidash::Client.new
14
83
  }.to raise_error(Rapidash::ConfigurationError)
15
84
  end
16
85
 
86
+ describe ".site=" do
87
+ it "should clear the connection variable after set new site" do
88
+ subject.instance_variable_get(:@connection).should eql(nil)
89
+ subject.site = "foo"
90
+ subject.instance_variable_set(:@connection, "Not nil")
91
+
92
+ subject.site = "bar"
93
+ subject.instance_variable_get(:@connection).should eql(nil)
94
+ end
95
+
96
+ it "should set the site variable" do
97
+ subject.instance_variable_get(:@site).should eql(nil)
98
+ subject.site = "foo"
99
+ subject.instance_variable_get(:@site).should eql("foo")
100
+ end
101
+ end
102
+
103
+
17
104
  describe ".get" do
18
105
  it "should call request" do
19
106
  subject.should_receive(:request).with(:get, "foo", {})
@@ -49,6 +136,18 @@ describe Rapidash::Client do
49
136
  end
50
137
  end
51
138
 
139
+ describe ".normalize_url" do
140
+ it "should use the instance extension if set" do
141
+ subject.extension = :json
142
+ subject.normalize_url("users").should eql("users.json")
143
+ end
52
144
 
145
+ it "should use the class extension if set" do
146
+ HTTPClientExtensionTester.new.normalize_url("users").should eql("users.js")
147
+ end
53
148
 
149
+ it "should return the url if no extension if set" do
150
+ subject.normalize_url("users").should eql("users")
151
+ end
152
+ end
54
153
  end
@@ -1,21 +1,16 @@
1
1
  require "spec_helper"
2
2
 
3
- class HTTPTester
3
+ class HTTPTester < Rapidash::Client
4
4
  include Rapidash::HTTPClient
5
5
  end
6
6
 
7
7
  class HTTPSiteTester < HTTPTester
8
- site "http://mysite.com/"
9
- end
10
-
11
- class HTTPExtensionTester < HTTPTester
12
- site "http://mysite.com/"
13
- def self.url_extension
14
- :json
8
+ class << self
9
+ attr_accessor :site
15
10
  end
16
11
  end
17
12
 
18
- class HTTPErrorTester < HTTPTester
13
+ class HTTPErrorTester < HTTPSiteTester
19
14
  def self.raise_error
20
15
  true
21
16
  end
@@ -25,41 +20,51 @@ describe Rapidash::HTTPClient do
25
20
 
26
21
  let!(:subject) { HTTPTester.new }
27
22
 
28
- describe ".site=" do
29
- it "should clear the connection variable" do
30
- subject.instance_variable_get(:@connection).should eql(nil)
31
- subject.connection
32
- subject.instance_variable_get(:@connection).class.should eql(Faraday::Connection)
33
- subject.site = "foo"
34
- subject.instance_variable_get(:@connection).should eql(nil)
35
- end
36
-
37
- it "should set the site variable" do
38
- subject.instance_variable_get(:@site).should eql(nil)
39
- subject.site = "foo"
40
- subject.instance_variable_get(:@site).should eql("foo")
41
- end
42
- end
43
-
44
23
  describe ".connection" do
45
24
  it "should create a Faraday object" do
25
+ subject.site = "http://example.com/"
46
26
  subject.connection.class.should eql(Faraday::Connection)
47
27
  end
48
28
 
29
+ it "should raise Configuration error if site nil" do
30
+ expect {
31
+ subject.connection
32
+ }.to raise_error(Rapidash::ConfigurationError)
33
+ end
34
+
49
35
  it "should use the site variable if set" do
50
36
  Faraday.should_receive(:new).with("http://example.com/")
51
37
  subject.site = "http://example.com/"
52
38
  subject.connection
53
39
  end
40
+ end
54
41
 
55
- it "should use the class URL if one is defined" do
56
- subject = HTTPSiteTester.new
57
- Faraday.should_receive(:new).with("http://mysite.com/")
58
- subject.connection
42
+ describe ".request" do
43
+
44
+ before(:each) do
45
+ subject.site = "http://example.com"
46
+ end
47
+
48
+ describe "authorization" do
49
+ let!(:options) { { :login => "login", :password => "password" } }
50
+ let!(:subject) { HTTPTester.new(options) }
51
+
52
+ it "should authorize with login and password" do
53
+ subject.should_receive(:process_response).with("response", :get, {})
54
+ subject.connection.should_receive(:basic_auth).with(options[:login], options[:password])
55
+ subject.connection.stub_chain('app.call').and_return("response")
56
+ subject.request(:get, "foo")
57
+ end
58
+ end
59
+
60
+ it "should call response" do
61
+ subject.should_receive(:process_response).with("response", :get, {})
62
+ subject.connection.should_receive(:run_request).with(:get, "http://example.com/foo", nil, nil).and_return("response")
63
+ subject.request(:get, "foo")
59
64
  end
60
65
  end
61
66
 
62
- describe ".request" do
67
+ describe ".process_response" do
63
68
 
64
69
  let!(:valid_response) { OpenStruct.new(:status => "200")}
65
70
  let!(:redirect_response) { OpenStruct.new(:status => "301", :headers => {"location" => "http://example.com/redirect"})}
@@ -70,52 +75,32 @@ describe Rapidash::HTTPClient do
70
75
  end
71
76
 
72
77
  describe "valid response" do
73
-
74
78
  before(:each) do
75
79
  Rapidash::Response.should_receive(:new).and_return("response")
76
80
  end
77
81
 
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
82
  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")
83
+ response = subject.process_response(valid_response, :get, {})
94
84
  response.should eql("response")
95
85
  end
96
86
 
97
87
  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")
88
+ subject.should_receive(:request).with(:get, "http://example.com/redirect", anything).and_return(subject.process_response(valid_response, :get, {}))
89
+ response = subject.process_response(redirect_response, :get, {})
101
90
  response.should eql("response")
102
91
  end
103
-
104
92
  end
105
93
 
106
94
  describe "error response" do
107
-
108
95
  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
96
+ response = subject.process_response(error_response, :get, {})
97
+ response.should be_nil
112
98
  end
113
99
 
114
100
  it "should raise an error if the option is set" do
115
101
  subject = HTTPErrorTester.new
116
- subject.connection.should_receive(:run_request).with(:get, anything, nil, nil).and_return(error_response)
117
102
  expect {
118
- response = subject.request(:get, "error")
103
+ response = subject.process_response(error_response, :get, {})
119
104
  }.to raise_error(Rapidash::ResponseError)
120
105
  end
121
106
 
@@ -2,10 +2,10 @@ require 'spec_helper'
2
2
 
3
3
  module Integration
4
4
 
5
- class Repos < Rapidash::Base
5
+ class Repo < Rapidash::Base
6
6
  end
7
7
 
8
- class Users < Rapidash::Base
8
+ class User < Rapidash::Base
9
9
  url "members"
10
10
  resource :repos
11
11
  end
@@ -4,12 +4,6 @@ class OAuthTester
4
4
  include Rapidash::OAuthClient
5
5
  end
6
6
 
7
- class OAuthExtensionTester < OAuthTester
8
- def self.url_extension
9
- :json
10
- end
11
- end
12
-
13
7
  class OAuthErrorTester < OAuthTester
14
8
  def self.raise_error
15
9
  true
@@ -31,16 +25,25 @@ describe Rapidash::OAuthClient do
31
25
  }
32
26
  }
33
27
 
34
- let(:subject) { OAuthTester.new(options) }
28
+ let(:subject) { OAuthTester.new(options) }
35
29
 
30
+ describe ".initialize" do
36
31
 
37
- describe ".site" do
38
- it "should be example.com" do
39
- subject.site.should eql("http://example.com")
32
+ it "should not raise an error with the correct options" do
33
+ expect {
34
+ OAuthTester.new(options)
35
+ }.to_not raise_error(Rapidash::ConfigurationError)
36
+ end
37
+
38
+ it "should raise an error if the correct options are not set" do
39
+ expect {
40
+ OAuthTester.new({})
41
+ }.to raise_error(Rapidash::ConfigurationError)
40
42
  end
41
43
  end
42
44
 
43
- describe ".access_token_from_code" do
45
+
46
+ describe ".access_token_from_code" do
44
47
  it "should call localhost for the access token" do
45
48
  auth_code = mock
46
49
  client = mock
@@ -70,25 +73,11 @@ describe Rapidash::OAuthClient do
70
73
  describe "object returned from API call" do
71
74
 
72
75
  before(:each) do
73
- subject.extension = :json
74
76
  subject.stub(:oauth_access_token).and_return(request)
77
+ subject.stub(:normalize_url).with("me").and_return("me")
75
78
  request.stub(:get)
76
79
  end
77
80
 
78
- it "should add an extension to the url if one is set" do
79
- request.should_receive(:get).with("http://example.com/me.json", anything)
80
- subject.request(:get, "me")
81
- end
82
-
83
- it "should use the class extension if one is set" do
84
- subject = OAuthExtensionTester.new(options)
85
- subject.stub(:oauth_access_token).and_return(request)
86
- request.stub(:get)
87
- request.should_receive(:get).with("http://example.com/me.json", anything)
88
- subject.request(:get, "me")
89
- end
90
-
91
-
92
81
  it "should return a Hashie::Mash" do
93
82
  subject.request(:get, "me").class.should eql(Hashie::Mash)
94
83
  end
@@ -98,14 +87,25 @@ describe Rapidash::OAuthClient do
98
87
  describe "when errors are set" do
99
88
  it "should call oauth_access_token.send with errors set" do
100
89
  subject = OAuthErrorTester.new(options)
90
+ subject.stub(:normalize_url).and_return("error")
101
91
  subject.stub(:oauth_access_token).and_return(request)
102
92
  request.should_receive(:send).with(:get, "http://example.com/error", {:raise_errors => true})
103
93
  subject.request(:get, "error")
104
94
  end
105
95
 
106
96
  end
107
- end
97
+
98
+ describe "when a body is set" do
99
+ it "should call oauth_access_token.send with errors set" do
100
+ subject.stub(:normalize_url).and_return("users")
101
+ subject.stub(:oauth_access_token).and_return(request)
102
+ request.should_receive(:send).with(:get, "http://example.com/users", {:body => {"foo" => "bar"}.to_json, :raise_errors => false})
103
+ subject.request(:get, "error", :body => {"foo" => "bar"})
104
+ end
105
+
106
+ end
108
107
 
109
108
 
109
+ end
110
110
  end
111
111