abstract_api_wrapper 1.3.0 → 1.3.1

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d44dac1f1b099e9dade84a4fd871547a1778c855
4
- data.tar.gz: ccf0fbf273198e999b19796f65e23f16f4899fc9
3
+ metadata.gz: 9cc995d436e78cb0be33beb51fa674d0f59baa8c
4
+ data.tar.gz: 2f65f4c68daef1550a7c8c986e17de4b9c333bbc
5
5
  SHA512:
6
- metadata.gz: 9594e279953f13a993239b5bf7b0ec10df2bcf4d75a8fdb91e024752b70c89282313d6444809b540d612f2bcea1d2f04e9b2d8e6ccfb5386bae21ab41f477bf5
7
- data.tar.gz: f41d40d4cb674ebc1160c8b542d1557c7bacc18003ec91b7b607644bd03d7e94df32596f39de1b98dd9811596b09917d173018fdd271bf62d7ec1a56780eb956
6
+ metadata.gz: 64fd33e5276da4c8cae1f98dfa4b97647af04c0389386ac7417a388fc7e8dd09b8c58baa285f8ddb6406236a17cb7e74cef45592093de9a239c7e573447d1f65
7
+ data.tar.gz: a2bebcf9e5c72acad74c6c4340288d82f55582e9889403e66e4b7213ee046e8011e193b93f146857eba5fd1747debfcea611c975d5ce4512d6e94ed4c2fee4b9
@@ -19,7 +19,7 @@ class AbstractApiWrapper
19
19
  end
20
20
 
21
21
  class Request
22
- attr_accessor :path, :filters, :chain
22
+ attr_accessor :path, :filters, :chain, :params
23
23
 
24
24
  METHODS_MAP = {
25
25
  'all' => 'get',
@@ -65,8 +65,8 @@ class AbstractApiWrapper
65
65
  @params = Hashie::Mash.new(params.first || {})
66
66
  end
67
67
  when 'destroy'
68
- if chain[-2] == 'find'
69
- path.push(params.first.to_s)
68
+ if chain[-2] != 'find'
69
+ raise NoResourceGiven.new('Missing resource id to destroy')
70
70
  end
71
71
  else
72
72
  path.push(method_name)
@@ -106,6 +106,8 @@ class AbstractApiWrapper
106
106
 
107
107
  "#{@client.base_url}/#{path.join('/')}?#{query_string}"
108
108
  end
109
+
110
+ class NoResourceGiven < StandardError; end
109
111
  end
110
112
 
111
113
  class Response
@@ -150,7 +152,7 @@ class AbstractApiWrapper
150
152
  end
151
153
 
152
154
  def headers
153
- @headers ||= @request.headers
155
+ @headers ||= Hashie::Mash.new(@request.headers)
154
156
  end
155
157
 
156
158
  def pagination
@@ -1,3 +1,3 @@
1
1
  class AbstractApiWrapper
2
- VERSION = '1.3.0'
2
+ VERSION = '1.3.1'
3
3
  end
@@ -36,6 +36,22 @@ describe 'AbstractApiWrapper' do
36
36
  expect(user.object_id).to eq(project.object_id)
37
37
  end
38
38
 
39
+ it 'should set a Collection instance for an array response' do
40
+ stub_request(:get, "#{TEST_OPTIONS[:base_url]}/users?apiver=v3")
41
+ .to_return(status: 200, body: '[]')
42
+
43
+ users = @client.users.all.run
44
+ expect(users).to be_kind_of(AbstractApiWrapper::Response::Collection)
45
+ end
46
+
47
+ it 'should set a Resource instance for an object response' do
48
+ stub_request(:get, "#{TEST_OPTIONS[:base_url]}/users/1?apiver=v3")
49
+ .to_return(status: 200, body: '{}')
50
+
51
+ user = @client.users.find(1).run
52
+ expect(user).to be_kind_of(AbstractApiWrapper::Response::Resource)
53
+ end
54
+
39
55
  it 'should create a path as array' do
40
56
  request = @client.users.find(1)
41
57
  expect(request.path).to be_kind_of(Array)
@@ -49,10 +65,45 @@ describe 'AbstractApiWrapper' do
49
65
  expect(request.filters).to eq({ 'active' => true })
50
66
  end
51
67
 
52
- it 'should set the path chain' do
68
+ it 'should set the path chain to get a collection' do
53
69
  request = @client.users.find(1).projects.all(active: true)
54
- expect(request.chain).to be_kind_of(Array)
55
70
  expect(request.chain).to eq(['users', 'find', 'projects', 'all'])
71
+ expect(request.path).to eq(['users', '1', 'projects'])
72
+ expect(request.filters).to eq({'active' => true})
73
+ end
74
+
75
+ it 'should set the path chain to make a head request' do
76
+ request = @client.users.find(1).projects.all(active: true).head
77
+ expect(request.chain).to eq(['users', 'find', 'projects', 'all', 'head'])
78
+ expect(request.path).to eq(['users', '1', 'projects'])
79
+ expect(request.filters).to eq({'active' => true})
80
+ end
81
+
82
+ it 'should set the path to create resource' do
83
+ request = @client.users.create(name: 'Jon Snow')
84
+ expect(request.chain).to eq(['users', 'create'])
85
+ expect(request.path).to eq(['users'])
86
+ expect(request.params).to eq({ 'name' => 'Jon Snow' })
87
+ end
88
+
89
+ it 'should set the path to update a resource' do
90
+ request = @client.users.find(1).update(name: 'Jon Targaryen')
91
+ expect(request.chain).to eq(['users', 'find', 'update'])
92
+ expect(request.path).to eq(['users', '1'])
93
+ expect(request.params).to eq({ 'name' => 'Jon Targaryen' })
94
+ end
95
+
96
+ it 'should set the path to delete a resoure' do
97
+ request = @client.users.find(1).destroy
98
+ expect(request.chain).to eq(['users', 'find', 'destroy'])
99
+ expect(request.path).to eq(['users', '1'])
100
+ expect(request.params).to eq({})
101
+ end
102
+
103
+ it 'should raise an error if no resoure to destroy is given' do
104
+ expect {
105
+ request = @client.users.destroy
106
+ }.to raise_error(AbstractApiWrapper::Request::NoResourceGiven)
56
107
  end
57
108
  end
58
109
 
@@ -100,9 +151,24 @@ describe 'AbstractApiWrapper' do
100
151
  end
101
152
  end
102
153
 
103
- # TODO: Collection tests
104
- # describe 'Collection' do
105
- # end
154
+ describe 'Collection' do
155
+ before(:each) do
156
+ @items = [
157
+ { id: 1, full_name: 'Jon Snow', email: 'jon@winterfell.com' },
158
+ { id: 1, full_name: 'Jon Snow', email: 'tyrion@lannisport.com' },
159
+ ]
160
+ end
161
+
162
+ it 'should create a new Collection instance' do
163
+ request = Faraday.get(TEST_OPTIONS[:base_url])
164
+ collection = AbstractApiWrapper::Response::Collection.new(@items, request)
165
+
166
+ expect(collection).to be_kind_of(Array)
167
+ expect(collection.size).to be(2)
168
+ expect(collection.headers).to be_kind_of(Hashie::Mash)
169
+ expect(collection.pagination).to be_kind_of(Hashie::Mash)
170
+ end
171
+ end
106
172
  end
107
173
 
108
174
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: abstract_api_wrapper
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.0
4
+ version: 1.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Juan Puelpan
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-12-10 00:00:00.000000000 Z
11
+ date: 2016-12-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: json