spyke 5.3.0 → 5.4.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.
- checksums.yaml +5 -5
- data/.travis.yml +1 -1
- data/README.md +15 -2
- data/lib/spyke/attribute_assignment.rb +9 -2
- data/lib/spyke/http.rb +13 -13
- data/lib/spyke/version.rb +1 -1
- data/spyke.gemspec +2 -2
- data/test/activemodel_dirty_test.rb +12 -0
- data/test/attributes_test.rb +2 -2
- data/test/config_test.rb +11 -0
- data/test/custom_request_test.rb +8 -1
- data/test/orm_test.rb +8 -0
- data/test/support/fixtures.rb +16 -1
- data/test/test_helper.rb +1 -1
- metadata +10 -20
- data/circle.yml +0 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 7de3203151c0bbf40af24c4ebb7926f60b497e61cd4a7edd3d5b0dcc12f302e5
|
4
|
+
data.tar.gz: 9cea6becd607f2f4a74258a5984055e619206f0df881c56fb444716e6e91b8af
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: db5b33e37fe25bc7f2f56215ca904a37c40541804acaa8338c9b63d23ac10e9e7724f65b6abf4ac98501b354fd1d802cd0bff0483b3a2b64673797d299043750
|
7
|
+
data.tar.gz: 34252e5c0e56a6b7a3fc83343ed955d39f6dc46250da3c8e90817df01609adcb9e09105cc9bb6b96bfd4b8c66bf06d3dbcbc20fbf4ccc7cd11ef016800019c5a
|
data/.travis.yml
CHANGED
data/README.md
CHANGED
@@ -1,12 +1,11 @@
|
|
1
1
|
# Spyke
|
2
2
|
|
3
3
|
<p align="center">
|
4
|
-
<img src="http://upload.wikimedia.org/wikipedia/en/thumb/2/21/Spyke.jpg/392px-Spyke.jpg" width="
|
4
|
+
<img src="http://upload.wikimedia.org/wikipedia/en/thumb/2/21/Spyke.jpg/392px-Spyke.jpg" width="15%" />
|
5
5
|
<br/>
|
6
6
|
Interact with remote <strong>REST services</strong> in an <strong>ActiveRecord-like</strong> manner.
|
7
7
|
<br /><br />
|
8
8
|
<a href="https://rubygems.org/gems/spyke"><img src="https://badge.fury.io/rb/spyke.svg?style=flat" alt="Gem Version"></a>
|
9
|
-
<a href='https://gemnasium.com/balvig/spyke'><img src="https://gemnasium.com/balvig/spyke.svg" /></a>
|
10
9
|
<a href="https://codeclimate.com/github/balvig/spyke"><img src="https://codeclimate.com/github/balvig/spyke/badges/gpa.svg" /></a>
|
11
10
|
<a href='https://coveralls.io/r/balvig/spyke?branch=master'><img src='https://img.shields.io/coveralls/balvig/spyke.svg?style=flat' /></a>
|
12
11
|
<a href="https://travis-ci.org/balvig/spyke"><img src="https://travis-ci.org/balvig/spyke.svg?branch=master" /></a>
|
@@ -31,6 +30,7 @@ Add this line to your application's Gemfile:
|
|
31
30
|
|
32
31
|
```ruby
|
33
32
|
gem 'spyke'
|
33
|
+
gem 'multi_json' # or whatever is needed to parse responses
|
34
34
|
```
|
35
35
|
|
36
36
|
Spyke uses Faraday to handle requests and expects it to parse the response body into a hash in the following format:
|
@@ -153,6 +153,19 @@ Post.request(:post, 'posts/3/log', time: '12:00')
|
|
153
153
|
# => POST http://api.com/posts/3/log - { time: '12:00' }
|
154
154
|
```
|
155
155
|
|
156
|
+
### Custom primary keys
|
157
|
+
|
158
|
+
Custom primary keys can be defined with `self.primary_key = :custom_key`:
|
159
|
+
|
160
|
+
```ruby
|
161
|
+
class User < Spyke::Base
|
162
|
+
self.primary_key = :user_id
|
163
|
+
|
164
|
+
# When using custom URIs the :id parameter also has to be adjusted
|
165
|
+
uri 'people(/:user_id)'
|
166
|
+
end
|
167
|
+
```
|
168
|
+
|
156
169
|
### API-side validations
|
157
170
|
|
158
171
|
Spyke expects errors to be formatted in the same way as the
|
@@ -6,7 +6,6 @@ module Spyke
|
|
6
6
|
extend ActiveSupport::Concern
|
7
7
|
|
8
8
|
included do
|
9
|
-
attr_reader :attributes
|
10
9
|
delegate :[], :[]=, to: :attributes
|
11
10
|
end
|
12
11
|
|
@@ -40,11 +39,19 @@ module Spyke
|
|
40
39
|
yield self if block_given?
|
41
40
|
end
|
42
41
|
|
42
|
+
def attributes
|
43
|
+
@spyke_attributes
|
44
|
+
end
|
45
|
+
|
43
46
|
def attributes=(new_attributes)
|
44
|
-
@
|
47
|
+
@spyke_attributes ||= Attributes.new(scope.params)
|
45
48
|
use_setters(new_attributes) if new_attributes
|
46
49
|
end
|
47
50
|
|
51
|
+
def id?
|
52
|
+
id.present?
|
53
|
+
end
|
54
|
+
|
48
55
|
def id
|
49
56
|
attributes[primary_key]
|
50
57
|
end
|
data/lib/spyke/http.rb
CHANGED
@@ -41,20 +41,20 @@ module Spyke
|
|
41
41
|
@uri ||= uri_template || default_uri
|
42
42
|
end
|
43
43
|
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
request.url path.to_s
|
52
|
-
request.body = params
|
53
|
-
end
|
44
|
+
def send_request(method, path, params)
|
45
|
+
connection.send(method) do |request|
|
46
|
+
if method == :get
|
47
|
+
request.url path.to_s, params
|
48
|
+
else
|
49
|
+
request.url path.to_s
|
50
|
+
request.body = params
|
54
51
|
end
|
55
|
-
rescue Faraday::ConnectionFailed, Faraday::TimeoutError
|
56
|
-
raise ConnectionError
|
57
52
|
end
|
53
|
+
rescue Faraday::ConnectionFailed, Faraday::TimeoutError
|
54
|
+
raise ConnectionError
|
55
|
+
end
|
56
|
+
|
57
|
+
private
|
58
58
|
|
59
59
|
def scoped_request(method)
|
60
60
|
uri = new.uri
|
@@ -79,7 +79,7 @@ module Spyke
|
|
79
79
|
end
|
80
80
|
|
81
81
|
def default_uri
|
82
|
-
"#{model_name.element.pluralize}
|
82
|
+
"#{model_name.element.pluralize}(/:#{primary_key})"
|
83
83
|
end
|
84
84
|
end
|
85
85
|
|
data/lib/spyke/version.rb
CHANGED
data/spyke.gemspec
CHANGED
@@ -18,8 +18,8 @@ Gem::Specification.new do |spec|
|
|
18
18
|
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
19
|
spec.require_paths = ["lib"]
|
20
20
|
|
21
|
-
spec.add_dependency 'activesupport', '>= 4.0.0'
|
22
|
-
spec.add_dependency 'activemodel', '>= 4.0.0'
|
21
|
+
spec.add_dependency 'activesupport', '>= 4.0.0'
|
22
|
+
spec.add_dependency 'activemodel', '>= 4.0.0'
|
23
23
|
spec.add_dependency 'faraday', '>= 0.9.0', '< 2.0'
|
24
24
|
spec.add_dependency 'faraday_middleware', '>= 0.9.1', '< 2.0'
|
25
25
|
spec.add_dependency 'addressable', '>= 2.5.2'
|
@@ -0,0 +1,12 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
module Spyke
|
4
|
+
class ActiveModelDirtyTest < MiniTest::Test
|
5
|
+
def test_attributes_instance_var_compatibility
|
6
|
+
recipe = RecipeWithDirty.new(title: 'Cheeseburger')
|
7
|
+
|
8
|
+
# If @attributes is set on recipe ActiveModel::Dirty will crash
|
9
|
+
assert_equal false, recipe.attribute_changed_in_place?(:title)
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
data/test/attributes_test.rb
CHANGED
@@ -135,9 +135,9 @@ module Spyke
|
|
135
135
|
|
136
136
|
def test_inspect
|
137
137
|
recipe = Recipe.new(id: 2, title: 'Pizza', description: 'Delicious')
|
138
|
-
assert_equal '#<Recipe(recipes
|
138
|
+
assert_equal '#<Recipe(recipes(/:id)) id: 2 title: "Pizza" description: "Delicious">', recipe.inspect
|
139
139
|
recipe = Recipe.new
|
140
|
-
assert_equal '#<Recipe(recipes
|
140
|
+
assert_equal '#<Recipe(recipes(/:id)) id: nil >', recipe.inspect
|
141
141
|
user = Recipe.new.build_user
|
142
142
|
assert_equal '#<User(users/:uuid) id: nil >', user.inspect
|
143
143
|
group = Recipe.new.groups.build
|
data/test/config_test.rb
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
module Spyke
|
4
|
+
class ConfigConnectionWarnTest < MiniTest::Test
|
5
|
+
def test_config_connection_warn
|
6
|
+
assert_output '', "[DEPRECATION] `Spyke::Config.connection=` is deprecated. Please use `Spyke::Base.connection=` instead.\n" do
|
7
|
+
Spyke::Config.connection = Spyke::Base.connection
|
8
|
+
end
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
data/test/custom_request_test.rb
CHANGED
@@ -63,9 +63,16 @@ module Spyke
|
|
63
63
|
end
|
64
64
|
|
65
65
|
def test_multiple_apis
|
66
|
-
endpoint = stub_request(:get, 'http://sashimi.com/
|
66
|
+
endpoint = stub_request(:get, 'http://sashimi.com/recipes')
|
67
67
|
OtherRecipe.all.to_a
|
68
68
|
assert_requested endpoint
|
69
69
|
end
|
70
|
+
|
71
|
+
def test_multiple_apis_with_custom_fallback
|
72
|
+
fallback_endpoint = stub_request(:get, 'http://sushi.com/recipes')
|
73
|
+
primary_endpoint = stub_request(:get, 'http://sashimi.com/recipes').to_timeout
|
74
|
+
OtherRecipe.all.to_a
|
75
|
+
assert_requested fallback_endpoint
|
76
|
+
end
|
70
77
|
end
|
71
78
|
end
|
data/test/orm_test.rb
CHANGED
@@ -206,5 +206,13 @@ module Spyke
|
|
206
206
|
assert_equal 1, user[:uuid]
|
207
207
|
assert_equal 42, user[:id]
|
208
208
|
end
|
209
|
+
|
210
|
+
def test_custom_primary_key_used_for_persistence_check
|
211
|
+
user = User.new
|
212
|
+
refute user.persisted?
|
213
|
+
|
214
|
+
user.uuid = 1
|
215
|
+
assert user.persisted?
|
216
|
+
end
|
209
217
|
end
|
210
218
|
end
|
data/test/support/fixtures.rb
CHANGED
@@ -116,7 +116,15 @@ class OtherApi < Spyke::Base
|
|
116
116
|
end
|
117
117
|
end
|
118
118
|
|
119
|
-
class OtherRecipe < OtherApi
|
119
|
+
class OtherRecipe < OtherApi
|
120
|
+
uri 'recipes/(:id)'
|
121
|
+
|
122
|
+
def self.send_request(method, path, params)
|
123
|
+
super
|
124
|
+
rescue Spyke::ConnectionError
|
125
|
+
Recipe.send_request(method, path, params)
|
126
|
+
end
|
127
|
+
end
|
120
128
|
|
121
129
|
class Search
|
122
130
|
def initialize(query)
|
@@ -152,3 +160,10 @@ module Cookbook
|
|
152
160
|
include_root_in_json :foto
|
153
161
|
end
|
154
162
|
end
|
163
|
+
|
164
|
+
class RecipeWithDirty < Recipe
|
165
|
+
# NOTE: Simply including ActiveModel::Dirty doesn't provide all the dirty
|
166
|
+
# functionality. This is left intentionally incomplete as it's all we need
|
167
|
+
# for testing compatibility.
|
168
|
+
include ActiveModel::Dirty
|
169
|
+
end
|
data/test/test_helper.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: spyke
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 5.
|
4
|
+
version: 5.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jens Balvig
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2020-12-09 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -17,9 +17,6 @@ dependencies:
|
|
17
17
|
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
19
|
version: 4.0.0
|
20
|
-
- - "<"
|
21
|
-
- !ruby/object:Gem::Version
|
22
|
-
version: '6.0'
|
23
20
|
type: :runtime
|
24
21
|
prerelease: false
|
25
22
|
version_requirements: !ruby/object:Gem::Requirement
|
@@ -27,9 +24,6 @@ dependencies:
|
|
27
24
|
- - ">="
|
28
25
|
- !ruby/object:Gem::Version
|
29
26
|
version: 4.0.0
|
30
|
-
- - "<"
|
31
|
-
- !ruby/object:Gem::Version
|
32
|
-
version: '6.0'
|
33
27
|
- !ruby/object:Gem::Dependency
|
34
28
|
name: activemodel
|
35
29
|
requirement: !ruby/object:Gem::Requirement
|
@@ -37,9 +31,6 @@ dependencies:
|
|
37
31
|
- - ">="
|
38
32
|
- !ruby/object:Gem::Version
|
39
33
|
version: 4.0.0
|
40
|
-
- - "<"
|
41
|
-
- !ruby/object:Gem::Version
|
42
|
-
version: '6.0'
|
43
34
|
type: :runtime
|
44
35
|
prerelease: false
|
45
36
|
version_requirements: !ruby/object:Gem::Requirement
|
@@ -47,9 +38,6 @@ dependencies:
|
|
47
38
|
- - ">="
|
48
39
|
- !ruby/object:Gem::Version
|
49
40
|
version: 4.0.0
|
50
|
-
- - "<"
|
51
|
-
- !ruby/object:Gem::Version
|
52
|
-
version: '6.0'
|
53
41
|
- !ruby/object:Gem::Dependency
|
54
42
|
name: faraday
|
55
43
|
requirement: !ruby/object:Gem::Requirement
|
@@ -257,7 +245,6 @@ files:
|
|
257
245
|
- LICENSE.txt
|
258
246
|
- README.md
|
259
247
|
- Rakefile
|
260
|
-
- circle.yml
|
261
248
|
- lib/spyke.rb
|
262
249
|
- lib/spyke/associations.rb
|
263
250
|
- lib/spyke/associations/association.rb
|
@@ -285,9 +272,11 @@ files:
|
|
285
272
|
- lib/spyke/scoping.rb
|
286
273
|
- lib/spyke/version.rb
|
287
274
|
- spyke.gemspec
|
275
|
+
- test/activemodel_dirty_test.rb
|
288
276
|
- test/associations_test.rb
|
289
277
|
- test/attributes_test.rb
|
290
278
|
- test/callbacks_test.rb
|
279
|
+
- test/config_test.rb
|
291
280
|
- test/custom_request_test.rb
|
292
281
|
- test/fallbacks_test.rb
|
293
282
|
- test/orm_test.rb
|
@@ -300,7 +289,7 @@ homepage: https://github.com/balvig/spyke
|
|
300
289
|
licenses:
|
301
290
|
- MIT
|
302
291
|
metadata: {}
|
303
|
-
post_install_message:
|
292
|
+
post_install_message:
|
304
293
|
rdoc_options: []
|
305
294
|
require_paths:
|
306
295
|
- lib
|
@@ -315,15 +304,16 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
315
304
|
- !ruby/object:Gem::Version
|
316
305
|
version: '0'
|
317
306
|
requirements: []
|
318
|
-
|
319
|
-
|
320
|
-
signing_key:
|
307
|
+
rubygems_version: 3.0.3
|
308
|
+
signing_key:
|
321
309
|
specification_version: 4
|
322
310
|
summary: Interact with REST services in an ActiveRecord-like manner
|
323
311
|
test_files:
|
312
|
+
- test/activemodel_dirty_test.rb
|
324
313
|
- test/associations_test.rb
|
325
314
|
- test/attributes_test.rb
|
326
315
|
- test/callbacks_test.rb
|
316
|
+
- test/config_test.rb
|
327
317
|
- test/custom_request_test.rb
|
328
318
|
- test/fallbacks_test.rb
|
329
319
|
- test/orm_test.rb
|
data/circle.yml
DELETED