ruby_json_api_client 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (35) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +22 -0
  3. data/Gemfile +4 -0
  4. data/LICENSE.txt +22 -0
  5. data/README.md +25 -0
  6. data/Rakefile +2 -0
  7. data/lib/ruby_json_api_client/adapters/ams_adapter.rb +15 -0
  8. data/lib/ruby_json_api_client/adapters/json_api_adapter.rb +15 -0
  9. data/lib/ruby_json_api_client/adapters/rest_adapter.rb +80 -0
  10. data/lib/ruby_json_api_client/base.rb +98 -0
  11. data/lib/ruby_json_api_client/collection.rb +13 -0
  12. data/lib/ruby_json_api_client/serializers/ams_serializer.rb +187 -0
  13. data/lib/ruby_json_api_client/serializers/json_api_serializer.rb +93 -0
  14. data/lib/ruby_json_api_client/store.rb +171 -0
  15. data/lib/ruby_json_api_client/version.rb +3 -0
  16. data/lib/ruby_json_api_client.rb +14 -0
  17. data/ruby_json_api_client.gemspec +34 -0
  18. data/spec/integration/ams/find_spec.rb +46 -0
  19. data/spec/integration/ams/has_many_links_spec.rb +161 -0
  20. data/spec/integration/ams/has_many_sideload_spec.rb +170 -0
  21. data/spec/integration/ams/has_one_links_spec.rb +57 -0
  22. data/spec/integration/ams/has_one_sideload_spec.rb +87 -0
  23. data/spec/integration/ams/query_spec.rb +65 -0
  24. data/spec/integration/json_api/find_spec.rb +44 -0
  25. data/spec/integration/json_api/query_spec.rb +65 -0
  26. data/spec/spec_helper.rb +7 -0
  27. data/spec/support/classes.rb +26 -0
  28. data/spec/unit/adapters/json_api_spec.rb +4 -0
  29. data/spec/unit/adapters/rest_spec.rb +109 -0
  30. data/spec/unit/base_spec.rb +60 -0
  31. data/spec/unit/collection_spec.rb +17 -0
  32. data/spec/unit/serializers/ams_spec.rb +480 -0
  33. data/spec/unit/serializers/json_api_spec.rb +114 -0
  34. data/spec/unit/store_spec.rb +243 -0
  35. metadata +262 -0
@@ -0,0 +1,243 @@
1
+ require 'spec_helper'
2
+
3
+ describe RubyJsonApiClient::Store do
4
+ class Person < RubyJsonApiClient::Base
5
+ field :firstname
6
+ field :lastname
7
+ end
8
+
9
+ describe :register_adapter do
10
+ context "with name only" do
11
+ subject { RubyJsonApiClient::Store.register_adapter(:json_api) }
12
+ its(:klass) { should eq(RubyJsonApiClient::JsonApiAdapter) }
13
+ its(:options) { should eq({}) }
14
+ end
15
+
16
+ context "with name and options" do
17
+ subject do
18
+ RubyJsonApiClient::Store.register_adapter(:json_api, {
19
+ hostname: 'www.example.com'
20
+ })
21
+ end
22
+
23
+ its(:klass) { should eq(RubyJsonApiClient::JsonApiAdapter) }
24
+ its(:options) { should == { hostname: 'www.example.com' } }
25
+ end
26
+
27
+ context "with name and klass" do
28
+ subject do
29
+ RubyJsonApiClient::Store.register_adapter(:asdf, RubyJsonApiClient::JsonApiAdapter)
30
+ end
31
+
32
+ its(:klass) { should eq(RubyJsonApiClient::JsonApiAdapter) }
33
+ its(:options) { should eq({}) }
34
+ end
35
+
36
+ context "with name, klass, and options" do
37
+ subject do
38
+ RubyJsonApiClient::Store.register_adapter(:asdf, RubyJsonApiClient::JsonApiAdapter, {
39
+ hostname: 'www.example.com'
40
+ })
41
+ end
42
+
43
+ its(:klass) { should eq(RubyJsonApiClient::JsonApiAdapter) }
44
+ its(:options) { should eq({ hostname: 'www.example.com' }) }
45
+ end
46
+ end
47
+
48
+ describe :register_serializer do
49
+ context "with name only" do
50
+ subject { RubyJsonApiClient::Store.register_serializer(:json_api) }
51
+ its(:klass) { should eq(RubyJsonApiClient::JsonApiSerializer) }
52
+ end
53
+
54
+ context "with name and class" do
55
+ subject { RubyJsonApiClient::Store.register_serializer(:asdf, RubyJsonApiClient::JsonApiSerializer) }
56
+ its(:klass) { should eq(RubyJsonApiClient::JsonApiSerializer) }
57
+ end
58
+ end
59
+
60
+ describe :get_adapter do
61
+ context "from a store with no adapters" do
62
+ subject { RubyJsonApiClient::Store.get_adapter(:does_not_exist) }
63
+ it { should be_nil }
64
+ end
65
+
66
+ context "from a store with a registered adapter" do
67
+ before(:each) do
68
+ RubyJsonApiClient::Store.register_adapter(:json_api)
69
+ end
70
+
71
+ subject { RubyJsonApiClient::Store.get_adapter(:json_api) }
72
+ it { should_not be_nil }
73
+ it { should be_instance_of(RubyJsonApiClient::JsonApiAdapter) }
74
+ end
75
+ end
76
+
77
+ describe :get_serializer do
78
+ context "from a store with no serializer" do
79
+ subject { RubyJsonApiClient::Store.get_serializer(:does_not_exist) }
80
+ it { should be_nil }
81
+ end
82
+
83
+ context "from a store with a registered serializer" do
84
+ before(:each) do
85
+ RubyJsonApiClient::Store.register_serializer(:json_api)
86
+ end
87
+
88
+ subject { RubyJsonApiClient::Store.get_serializer(:json_api) }
89
+ it { should_not be_nil }
90
+ it { should be_instance_of(RubyJsonApiClient::JsonApiSerializer) }
91
+ end
92
+ end
93
+
94
+ let(:store) do
95
+ RubyJsonApiClient::Store.new(format: :json_api)
96
+ end
97
+
98
+ describe :initialize do
99
+ before(:each) do
100
+ RubyJsonApiClient::Store.register_adapter(:json_api)
101
+ RubyJsonApiClient::Store.register_serializer(:json_api)
102
+ end
103
+
104
+ context "the default adapter" do
105
+ subject { store.default_adapter }
106
+ it { should be_instance_of(RubyJsonApiClient::JsonApiAdapter) }
107
+ end
108
+
109
+ context "the default serializer" do
110
+ subject { store.default_serializer }
111
+ it { should be_instance_of(RubyJsonApiClient::JsonApiSerializer) }
112
+ its(:store) { should eq(store) }
113
+ end
114
+ end
115
+
116
+ describe :find do
117
+
118
+ let(:person) { Person.new }
119
+
120
+ before(:each) do
121
+ RubyJsonApiClient::Store.register_adapter(:json_api)
122
+ RubyJsonApiClient::Store.register_serializer(:json_api)
123
+
124
+ allow(store.default_adapter).to receive(:find)
125
+ .with(Person, 1)
126
+ .and_return("{}")
127
+
128
+ allow(store.default_serializer).to receive(:extract_single)
129
+ .with(Person, 1, "{}")
130
+ .and_return(person)
131
+ end
132
+
133
+ it "should should find a person" do
134
+ expect(store.find(Person, 1)).to eq(person)
135
+ end
136
+
137
+ it "should set the response inside the model" do
138
+ person = store.find(Person, 1)
139
+ expect(person.__origin__).to eq("{}")
140
+ end
141
+ end
142
+
143
+ describe :query do
144
+ let(:person) { Person.new }
145
+ let(:people) { [person, person] }
146
+ let(:result) { store.query(Person, {}) }
147
+
148
+ before(:each) do
149
+ RubyJsonApiClient::Store.register_adapter(:json_api)
150
+ RubyJsonApiClient::Store.register_serializer(:json_api)
151
+
152
+ allow(store.default_adapter).to receive(:find_many)
153
+ .with(Person, {})
154
+ .and_return("{}")
155
+
156
+ allow(store.default_serializer).to receive(:extract_many)
157
+ .with(Person, "{}")
158
+ .and_return(people)
159
+ end
160
+
161
+ subject { result }
162
+
163
+ it { should have(2).people }
164
+ it { should include(person) }
165
+
166
+ it "should set the response on the collection" do
167
+ expect(result.__origin__).to eq("{}")
168
+ end
169
+ end
170
+
171
+ describe :load_collection do
172
+ before(:each) do
173
+ RubyJsonApiClient::Store.register_adapter(:json_api)
174
+ RubyJsonApiClient::Store.register_serializer(:json_api)
175
+ end
176
+
177
+ let(:serializer) { store.default_serializer }
178
+ let(:adapter) { store.default_adapter }
179
+
180
+ it "should use the adapter and serializer to load the collection" do
181
+ expect(adapter).to receive(:get)
182
+ .with("http://example.com/testings")
183
+ .and_return("[]")
184
+
185
+ expect(serializer).to receive(:extract_many)
186
+ .with(Person, "[]")
187
+
188
+ store.load_collection(Person, "http://example.com/testings")
189
+ end
190
+ end
191
+
192
+ describe :load_single do
193
+ before(:each) do
194
+ RubyJsonApiClient::Store.register_adapter(:json_api)
195
+ RubyJsonApiClient::Store.register_serializer(:json_api)
196
+ end
197
+
198
+ let(:serializer) { store.default_serializer }
199
+ let(:adapter) { store.default_adapter }
200
+
201
+ it "should use the adapter and serializer to load the model" do
202
+ expect(adapter).to receive(:get)
203
+ .with("http://example.com/testings/1")
204
+ .and_return("{}")
205
+
206
+ expect(serializer).to receive(:extract_single)
207
+ .with(Person, 1, "{}")
208
+
209
+ store.load_single(Person, 1, "http://example.com/testings/1")
210
+ end
211
+ end
212
+
213
+ describe :reload do
214
+ let(:person) { Person.new(id: 1) }
215
+
216
+ before(:each) do
217
+ expect(store).to receive(:find)
218
+ .with(Person, 1)
219
+ .and_return(
220
+ Person.new(id: 1, firstname: 'test')
221
+ )
222
+
223
+ store.reload(person)
224
+ end
225
+
226
+ subject { person }
227
+
228
+ it { should eql(person) }
229
+ its(:firstname) { should eq('test') }
230
+ its(:id) { should eq(1) }
231
+ end
232
+
233
+ describe :merge do
234
+ let(:person1) { Person.new(firstname: 'ryan', lastname: 'no') }
235
+ let(:person2) { Person.new(firstname: 'new') }
236
+
237
+ subject { store.merge(person1, person2) }
238
+
239
+ it { should be_instance_of(Person) }
240
+ its(:firstname) { should eq('new') }
241
+ its(:lastname) { should be_nil }
242
+ end
243
+ end
metadata ADDED
@@ -0,0 +1,262 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ruby_json_api_client
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Ryan Toronto
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-08-06 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: json
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 1.8.1
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 1.8.1
27
+ - !ruby/object:Gem::Dependency
28
+ name: faraday
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 0.9.0
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: 0.9.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: addressable
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: 2.3.6
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: 2.3.6
55
+ - !ruby/object:Gem::Dependency
56
+ name: activemodel
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '4.0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '4.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: activesupport
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '4.0'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '4.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: bundler
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '1.6'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '1.6'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rake
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: pry
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ - !ruby/object:Gem::Dependency
126
+ name: rspec
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - "~>"
130
+ - !ruby/object:Gem::Version
131
+ version: 3.0.0
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - "~>"
137
+ - !ruby/object:Gem::Version
138
+ version: 3.0.0
139
+ - !ruby/object:Gem::Dependency
140
+ name: rspec-collection_matchers
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - "~>"
144
+ - !ruby/object:Gem::Version
145
+ version: 1.0.0
146
+ type: :development
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - "~>"
151
+ - !ruby/object:Gem::Version
152
+ version: 1.0.0
153
+ - !ruby/object:Gem::Dependency
154
+ name: rspec-its
155
+ requirement: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - "~>"
158
+ - !ruby/object:Gem::Version
159
+ version: 1.0.1
160
+ type: :development
161
+ prerelease: false
162
+ version_requirements: !ruby/object:Gem::Requirement
163
+ requirements:
164
+ - - "~>"
165
+ - !ruby/object:Gem::Version
166
+ version: 1.0.1
167
+ - !ruby/object:Gem::Dependency
168
+ name: webmock
169
+ requirement: !ruby/object:Gem::Requirement
170
+ requirements:
171
+ - - "~>"
172
+ - !ruby/object:Gem::Version
173
+ version: 1.18.0
174
+ type: :development
175
+ prerelease: false
176
+ version_requirements: !ruby/object:Gem::Requirement
177
+ requirements:
178
+ - - "~>"
179
+ - !ruby/object:Gem::Version
180
+ version: 1.18.0
181
+ description: API client for activemodel instances
182
+ email:
183
+ - ryanto@gmail.com
184
+ executables: []
185
+ extensions: []
186
+ extra_rdoc_files: []
187
+ files:
188
+ - ".gitignore"
189
+ - Gemfile
190
+ - LICENSE.txt
191
+ - README.md
192
+ - Rakefile
193
+ - lib/ruby_json_api_client.rb
194
+ - lib/ruby_json_api_client/adapters/ams_adapter.rb
195
+ - lib/ruby_json_api_client/adapters/json_api_adapter.rb
196
+ - lib/ruby_json_api_client/adapters/rest_adapter.rb
197
+ - lib/ruby_json_api_client/base.rb
198
+ - lib/ruby_json_api_client/collection.rb
199
+ - lib/ruby_json_api_client/serializers/ams_serializer.rb
200
+ - lib/ruby_json_api_client/serializers/json_api_serializer.rb
201
+ - lib/ruby_json_api_client/store.rb
202
+ - lib/ruby_json_api_client/version.rb
203
+ - ruby_json_api_client.gemspec
204
+ - spec/integration/ams/find_spec.rb
205
+ - spec/integration/ams/has_many_links_spec.rb
206
+ - spec/integration/ams/has_many_sideload_spec.rb
207
+ - spec/integration/ams/has_one_links_spec.rb
208
+ - spec/integration/ams/has_one_sideload_spec.rb
209
+ - spec/integration/ams/query_spec.rb
210
+ - spec/integration/json_api/find_spec.rb
211
+ - spec/integration/json_api/query_spec.rb
212
+ - spec/spec_helper.rb
213
+ - spec/support/classes.rb
214
+ - spec/unit/adapters/json_api_spec.rb
215
+ - spec/unit/adapters/rest_spec.rb
216
+ - spec/unit/base_spec.rb
217
+ - spec/unit/collection_spec.rb
218
+ - spec/unit/serializers/ams_spec.rb
219
+ - spec/unit/serializers/json_api_spec.rb
220
+ - spec/unit/store_spec.rb
221
+ homepage: ''
222
+ licenses:
223
+ - MIT
224
+ metadata: {}
225
+ post_install_message:
226
+ rdoc_options: []
227
+ require_paths:
228
+ - lib
229
+ required_ruby_version: !ruby/object:Gem::Requirement
230
+ requirements:
231
+ - - ">="
232
+ - !ruby/object:Gem::Version
233
+ version: '0'
234
+ required_rubygems_version: !ruby/object:Gem::Requirement
235
+ requirements:
236
+ - - ">="
237
+ - !ruby/object:Gem::Version
238
+ version: '0'
239
+ requirements: []
240
+ rubyforge_project:
241
+ rubygems_version: 2.2.2
242
+ signing_key:
243
+ specification_version: 4
244
+ summary: API client for activemodel instances
245
+ test_files:
246
+ - spec/integration/ams/find_spec.rb
247
+ - spec/integration/ams/has_many_links_spec.rb
248
+ - spec/integration/ams/has_many_sideload_spec.rb
249
+ - spec/integration/ams/has_one_links_spec.rb
250
+ - spec/integration/ams/has_one_sideload_spec.rb
251
+ - spec/integration/ams/query_spec.rb
252
+ - spec/integration/json_api/find_spec.rb
253
+ - spec/integration/json_api/query_spec.rb
254
+ - spec/spec_helper.rb
255
+ - spec/support/classes.rb
256
+ - spec/unit/adapters/json_api_spec.rb
257
+ - spec/unit/adapters/rest_spec.rb
258
+ - spec/unit/base_spec.rb
259
+ - spec/unit/collection_spec.rb
260
+ - spec/unit/serializers/ams_spec.rb
261
+ - spec/unit/serializers/json_api_spec.rb
262
+ - spec/unit/store_spec.rb