restful_resource 0.8.9 → 0.8.10

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: 91803ab9a7afb00f09ddc7272c06e3330f3418fd
4
- data.tar.gz: 8ed0b0c0a0b914b940cdb35cc3c486107909e0b2
3
+ metadata.gz: 36cc91bc3981c424860da9b4d0796b4a6ecd39fa
4
+ data.tar.gz: 36335e18b3355ef43378f4bf40063e13cef20d49
5
5
  SHA512:
6
- metadata.gz: aada22cf8f429ea81558f25f73b8e9bc4420929e1156e21f57a4d01fa1af45c47a528e25a76eb774c96a708e31da85459c738067ca3ef5e7d5af3b41570f2a8f
7
- data.tar.gz: 797b5200c5f2a7d9ece83ddb0aadccffe8b13414b5eebeaf5d5401f564467cc0bf0fd681be34e6623443f22ef8600aa5c174f9e9fe6d8498764331f869af3cff
6
+ metadata.gz: 3f4140e5cdec22eacdbea78d34d92547929c4b261f41ee2c34de587f235cbf55a4957b1d4d93c9437bda5289f25b9f856d4fdff5d362bcea3bf0dace9de2f901
7
+ data.tar.gz: 79c6dd84751fb90dabecfa78218e12b879e6a1a7b99dfc207f674cc2663626c2a75c8a3219e8163886d3fb7c4b8abfc5364cf7a008fbe0194b45b2867d082b2e
@@ -2,20 +2,16 @@ module RestfulResource
2
2
  class Base < OpenObject
3
3
  extend RestfulResource::Associations
4
4
 
5
- def self.http=(http)
6
- @http = http
7
- end
5
+ def self.configure(base_url: nil, username: nil, password: nil)
6
+ @base_url = URI.parse(base_url)
8
7
 
9
- def self.http
10
- @http ||= RestfulResource::HttpClient.new(authorization: superclass.base_authorization)
11
- end
8
+ auth = nil
12
9
 
13
- def self.http_authorization(user, password)
14
- @base_authorization = RestfulResource::Authorization.http_authorization(user, password)
15
- end
10
+ if (username.present? && password.present?)
11
+ auth = RestfulResource::Authorization.http_authorization(username, password)
12
+ end
16
13
 
17
- def self.base_url=(url)
18
- @base_url = URI.parse(url)
14
+ @http = RestfulResource::HttpClient.new(authorization: auth)
19
15
  end
20
16
 
21
17
  def self.resource_path(url)
@@ -23,24 +19,24 @@ module RestfulResource
23
19
  end
24
20
 
25
21
  def self.find(id, params={})
26
- response = superclass.http.get(member_url(id, params))
22
+ response = http.get(member_url(id, params))
27
23
  self.new(parse_json(response.body))
28
24
  end
29
25
 
30
26
  def self.where(params={})
31
- response = superclass.http.get(collection_url(params))
27
+ response = http.get(collection_url(params))
32
28
  self.paginate_response(response)
33
29
  end
34
30
 
35
31
  def self.get(params={})
36
- response = superclass.http.get(collection_url(params))
32
+ response = http.get(collection_url(params))
37
33
  RestfulResource::OpenObject.new(parse_json(response.body))
38
34
  end
39
35
 
40
36
  def self.put(id, data: {}, **params)
41
37
  url = member_url(id, params)
42
38
 
43
- response = superclass.http.put(url, data: data)
39
+ response = http.put(url, data: data)
44
40
  self.new(parse_json(response.body))
45
41
  end
46
42
 
@@ -63,14 +59,17 @@ module RestfulResource
63
59
  end
64
60
 
65
61
  protected
66
- def self.base_url
67
- raise BaseUrlMissing.new if @base_url.nil?
68
-
69
- @base_url
62
+ def self.http
63
+ @http || superclass.http
70
64
  end
71
65
 
72
- def self.base_authorization
73
- @base_authorization
66
+ def self.base_url
67
+ result = @base_url
68
+ if result.nil? && superclass.respond_to?(:base_url)
69
+ result = superclass.base_url
70
+ end
71
+ raise "Base url missing" if result.nil?
72
+ result
74
73
  end
75
74
 
76
75
  private
@@ -79,12 +78,12 @@ module RestfulResource
79
78
  end
80
79
 
81
80
  def self.member_url(id, params)
82
- url = merge_url_paths(superclass.base_url, @resource_path, id, @action_prefix)
81
+ url = merge_url_paths(base_url, @resource_path, id, @action_prefix)
83
82
  replace_parameters(url, params)
84
83
  end
85
84
 
86
85
  def self.collection_url(params)
87
- url = merge_url_paths(superclass.base_url, @resource_path, @action_prefix)
86
+ url = merge_url_paths(base_url, @resource_path, @action_prefix)
88
87
  replace_parameters(url, params)
89
88
  end
90
89
 
@@ -1,3 +1,3 @@
1
1
  module RestfulResource
2
- VERSION = "0.8.9"
2
+ VERSION = "0.8.10"
3
3
  end
@@ -1,6 +1,6 @@
1
1
  require_relative '../spec_helper'
2
2
 
3
- describe "http client" do
3
+ describe RestfulResource::Base, 'authorization' do
4
4
  before :each do
5
5
  class FirstClient < RestfulResource::Base
6
6
  end
@@ -8,6 +8,8 @@ describe "http client" do
8
8
  class SecondClient < RestfulResource::Base
9
9
  end
10
10
 
11
+ class NotConfiguredClient < RestfulResource::Base; end
12
+
11
13
  class FirstTest < FirstClient
12
14
  resource_path 'test'
13
15
  end
@@ -16,31 +18,39 @@ describe "http client" do
16
18
  resource_path 'test'
17
19
  end
18
20
 
19
- FirstClient.http = nil
20
- FirstClient.base_url = 'http://api.carwow.co.uk/api/first'
21
- SecondClient.http = nil
22
- SecondClient.base_url = 'http://api.carwow.co.uk/api/second'
23
- SecondClient.http_authorization('test_user', 'test_pass')
21
+ FirstClient.configure(base_url: 'http://api.carwow.co.uk/api/first')
22
+ SecondClient.configure(base_url: 'http://api.carwow.co.uk/api/second',
23
+ username: 'test_user',
24
+ password: 'test_pass')
24
25
  end
25
26
 
26
27
  it "should use two different http instances" do
27
- expect(FirstTest.http).not_to equal(SecondTest.http)
28
+ expect(FirstTest.send(:http)).not_to equal(SecondTest.send(:http))
28
29
  end
29
30
 
30
31
  it 'should have http auth on SecondClient when initialised first' do
31
- SecondTest.http
32
- FirstTest.http
32
+ SecondTest.send(:http)
33
+ FirstTest.send(:http)
33
34
 
34
- authorization = SecondTest.http.instance_variable_get :@authorization
35
+ authorization = SecondTest.send(:http).instance_variable_get :@authorization
35
36
  expect(authorization).to be_truthy
36
37
  end
37
38
 
38
39
  it 'should have http auth on SecondTest when initialised second' do
39
- FirstTest.http
40
- SecondTest.http
40
+ FirstTest.send(:http)
41
+ SecondTest.send(:http)
41
42
 
42
- authorization = SecondTest.http.instance_variable_get :@authorization
43
+ authorization = SecondTest.send(:http).instance_variable_get :@authorization
43
44
  expect(authorization).to be_truthy
44
45
  end
46
+
47
+ it 'should have same http auth on superclass' do
48
+ expect(SecondTest.send(:http)).to equal(SecondClient.send(:http))
49
+ end
50
+
51
+ it 'should raise exception if base_url is not set' do
52
+ expect { NotConfiguredClient.send(:base_url) }.to raise_error 'Base url missing'
53
+ end
54
+
45
55
  end
46
56
 
@@ -3,8 +3,8 @@ require_relative '../spec_helper'
3
3
  describe RestfulResource::Base do
4
4
  before :each do
5
5
  @mock_http = double("mock_http")
6
- RestfulResource::Base.http = @mock_http
7
- RestfulResource::Base.base_url = "http://api.carwow.co.uk/"
6
+ allow(RestfulResource::Base).to receive(:http).and_return(@mock_http)
7
+ RestfulResource::Base.configure(base_url: 'http://api.carwow.co.uk/')
8
8
  end
9
9
 
10
10
  it "should act as an openobject" do
@@ -77,11 +77,9 @@ describe RestfulResource::Base do
77
77
 
78
78
  describe "#base_url" do
79
79
  it "should be different for each subclass of Base" do
80
- BaseA.base_url = "http://a.carwow.co.uk"
81
- BaseA.http = @mock_http
80
+ BaseA.configure(base_url: "http://a.carwow.co.uk")
82
81
 
83
- BaseB.base_url = "http://b.carwow.co.uk"
84
- BaseB.http = @mock_http
82
+ BaseB.configure(base_url: "http://b.carwow.co.uk")
85
83
 
86
84
  expect_get('http://a.carwow.co.uk/testa/1', RestfulResource::Response.new())
87
85
  expect_get('http://b.carwow.co.uk/testb/2', RestfulResource::Response.new())
@@ -3,8 +3,8 @@ require_relative '../spec_helper'
3
3
  describe RestfulResource::RailsValidations do
4
4
  before :each do
5
5
  @mock_http = double("mock_http")
6
- RestfulResource::Base.http = @mock_http
7
- RestfulResource::Base.base_url = "http://api.carwow.co.uk/"
6
+ RestfulResource::Base.configure(base_url: "http://api.carwow.co.uk/")
7
+ allow(RestfulResource::Base).to receive(:http).and_return(@mock_http)
8
8
  end
9
9
 
10
10
  context "#put without errors" do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: restful_resource
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.9
4
+ version: 0.8.10
5
5
  platform: ruby
6
6
  authors:
7
7
  - David Santoro