spyke 1.0.1 → 1.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +15 -6
- data/lib/spyke/version.rb +1 -1
- data/spyke.gemspec +1 -1
- data/test/associations_test.rb +10 -10
- data/test/attributes_test.rb +2 -2
- data/test/custom_request_test.rb +3 -3
- data/test/orm_test.rb +9 -9
- data/test/relation_test.rb +1 -1
- data/test/support/api.rb +13 -1
- metadata +22 -16
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 351feb9aa7e6aa5486ac90bfee67db3088e1bba3
|
4
|
+
data.tar.gz: dce508b409ac67d45c30746a5608d3e0911d8475
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: cec924b0be7293242a30fd358de54aaa9c4d8357fccdc3dd5e1ba3b408f993d4b0d20fa9f81b57ff49fb8cbac7c43620a14d7ba0b5a35a7af505ecde76ac209b
|
7
|
+
data.tar.gz: 5507ca04b4f70ca3a2c5d6b624029c23409414ae14adcfbfd15e55576c6c6c670a0c59ea3817f2e2d77d693adc54ba337c17926840e9008d48e6c38c17459958
|
data/README.md
CHANGED
@@ -1,8 +1,14 @@
|
|
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="20%" />
|
5
|
+
<br/>
|
6
|
+
Interact with remote <strong>REST services</strong> in an <strong>ActiveRecord-like</strong> manner.
|
7
|
+
</p>
|
4
8
|
|
5
|
-
|
9
|
+
---
|
10
|
+
|
11
|
+
Spyke basically ~~rips off~~ takes inspiration :innocent: from [Her](https://github.com/remiprev/her), a gem which we sadly had to abandon as it showed significant performance problems and maintenance seemed to had gone stale.
|
6
12
|
|
7
13
|
We therefore made Spyke which adds a few fixes/features that we needed for our projects:
|
8
14
|
|
@@ -37,12 +43,15 @@ class JSONParser < Faraday::Response::Middleware
|
|
37
43
|
data: json[:result],
|
38
44
|
metadata: json[:metadata]
|
39
45
|
}
|
46
|
+
rescue MultiJson::ParseError => exception
|
47
|
+
{ error: exception.cause }
|
40
48
|
end
|
41
49
|
end
|
42
50
|
|
43
51
|
Spyke::Config.connection = Faraday.new(url: 'http://api.com') do |c|
|
44
|
-
c.
|
45
|
-
c.use
|
52
|
+
c.request :json
|
53
|
+
c.use JSONParser
|
54
|
+
c.use Faraday.default_adapter
|
46
55
|
end
|
47
56
|
```
|
48
57
|
|
@@ -66,7 +75,7 @@ You can specify custom URIs on both the class and association level:
|
|
66
75
|
```ruby
|
67
76
|
class User < Spyke::Base
|
68
77
|
uri '/v1/users/:id'
|
69
|
-
|
78
|
+
|
70
79
|
has_one :image, uri: nil
|
71
80
|
has_many :posts, uri: '/posts/for_user/:user_id'
|
72
81
|
end
|
@@ -82,5 +91,5 @@ Post.find(4) # => GET http://api.com/posts/4
|
|
82
91
|
|
83
92
|
## Contributing
|
84
93
|
|
85
|
-
If possible please take a look at the tests marked "wishlisted"!
|
94
|
+
If possible please take a look at the tests marked "wishlisted"!
|
86
95
|
These are features/fixes we want to implement but haven't gotten around to doing yet :)
|
data/lib/spyke/version.rb
CHANGED
data/spyke.gemspec
CHANGED
@@ -21,10 +21,10 @@ Gem::Specification.new do |spec|
|
|
21
21
|
spec.add_dependency 'activesupport', '>= 3.0.0', '< 5.0'
|
22
22
|
spec.add_dependency 'activemodel', '>= 3.0.0', '< 5.0'
|
23
23
|
spec.add_dependency 'faraday', '>= 0.8.0', '< 2.0'
|
24
|
+
spec.add_dependency "faraday_middleware", '>= 0.9.1', '< 2.0'
|
24
25
|
spec.add_dependency 'uri_template', '>= 0.7.0', '< 2.0'
|
25
26
|
|
26
27
|
spec.add_development_dependency "bundler", "~> 1.6"
|
27
|
-
spec.add_development_dependency "faraday_middleware"
|
28
28
|
spec.add_development_dependency "minitest"
|
29
29
|
spec.add_development_dependency "minitest-line"
|
30
30
|
spec.add_development_dependency "minitest-reporters"
|
data/test/associations_test.rb
CHANGED
@@ -26,7 +26,7 @@ module Spyke
|
|
26
26
|
end
|
27
27
|
|
28
28
|
def test_embedded_associations
|
29
|
-
stub_request(:get, 'http://sushi.com/recipes/1').to_return_json(
|
29
|
+
stub_request(:get, 'http://sushi.com/recipes/1').to_return_json(result: { groups: [{ id: 1, name: 'Fish' }] })
|
30
30
|
|
31
31
|
recipe = Recipe.find(1)
|
32
32
|
|
@@ -34,7 +34,7 @@ module Spyke
|
|
34
34
|
end
|
35
35
|
|
36
36
|
def test_nested_embedded_associtations
|
37
|
-
json = {
|
37
|
+
json = { result: { groups: [{ ingredients: [{ id: 1, name: 'Fish' }] }, { ingredients: [] }] } }
|
38
38
|
stub_request(:get, 'http://sushi.com/recipes/1').to_return_json(json)
|
39
39
|
|
40
40
|
recipe = Recipe.find(1)
|
@@ -43,7 +43,7 @@ module Spyke
|
|
43
43
|
end
|
44
44
|
|
45
45
|
def test_singular_associtations
|
46
|
-
stub_request(:get, 'http://sushi.com/recipes/1').to_return_json(
|
46
|
+
stub_request(:get, 'http://sushi.com/recipes/1').to_return_json(result: { image: { url: 'bob.jpg' } })
|
47
47
|
|
48
48
|
recipe = Recipe.find(1)
|
49
49
|
|
@@ -51,7 +51,7 @@ module Spyke
|
|
51
51
|
end
|
52
52
|
|
53
53
|
def test_unloaded_has_many_association
|
54
|
-
endpoint = stub_request(:get, 'http://sushi.com/recipes/1/groups?public=true').to_return_json(
|
54
|
+
endpoint = stub_request(:get, 'http://sushi.com/recipes/1/groups?public=true').to_return_json(result: [{ id: 1 }])
|
55
55
|
|
56
56
|
groups = Recipe.new(id: 1).groups.where(public: true).to_a
|
57
57
|
|
@@ -60,7 +60,7 @@ module Spyke
|
|
60
60
|
end
|
61
61
|
|
62
62
|
def test_unloaded_has_one_association
|
63
|
-
endpoint = stub_request(:get, 'http://sushi.com/recipes/1/image').to_return_json(
|
63
|
+
endpoint = stub_request(:get, 'http://sushi.com/recipes/1/image').to_return_json(result: { url: 'bob.jpg' })
|
64
64
|
|
65
65
|
image = Recipe.new(id: 1).image
|
66
66
|
|
@@ -69,7 +69,7 @@ module Spyke
|
|
69
69
|
end
|
70
70
|
|
71
71
|
def test_array_like_behavior
|
72
|
-
stub_request(:get, 'http://sushi.com/recipes/1/groups').to_return_json(
|
72
|
+
stub_request(:get, 'http://sushi.com/recipes/1/groups').to_return_json(result: [{ name: 'Fish' }, { name: 'Fruit' }, { name: 'Bread' }])
|
73
73
|
|
74
74
|
recipe = Recipe.new(id: 1)
|
75
75
|
|
@@ -138,7 +138,7 @@ module Spyke
|
|
138
138
|
end
|
139
139
|
|
140
140
|
def test_converting_association_to_ids
|
141
|
-
stub_request(:get, 'http://sushi.com/users/1/recipes').to_return_json(
|
141
|
+
stub_request(:get, 'http://sushi.com/users/1/recipes').to_return_json(result: [{ id: 2 }])
|
142
142
|
user = User.new(id: 1)
|
143
143
|
assert_equal [2], user.recipe_ids
|
144
144
|
end
|
@@ -151,7 +151,7 @@ module Spyke
|
|
151
151
|
end
|
152
152
|
|
153
153
|
def test_custom_class_name
|
154
|
-
stub_request(:get, 'http://sushi.com/recipes/1').to_return_json(
|
154
|
+
stub_request(:get, 'http://sushi.com/recipes/1').to_return_json(result: { background_image: { url: 'bob.jpg' } })
|
155
155
|
|
156
156
|
recipe = Recipe.find(1)
|
157
157
|
|
@@ -180,7 +180,7 @@ module Spyke
|
|
180
180
|
end
|
181
181
|
|
182
182
|
def test_create_association
|
183
|
-
endpoint = stub_request(:post, 'http://sushi.com/recipes/1/groups').with(body: { group: { title: 'Topping' } }).to_return_json(
|
183
|
+
endpoint = stub_request(:post, 'http://sushi.com/recipes/1/groups').with(body: { group: { title: 'Topping' } }).to_return_json(result: { title: 'Topping', id: 1, recipe_id: 1 })
|
184
184
|
|
185
185
|
recipe = Recipe.new(id: 1)
|
186
186
|
group = recipe.groups.create(title: 'Topping')
|
@@ -209,7 +209,7 @@ module Spyke
|
|
209
209
|
end
|
210
210
|
|
211
211
|
def test_save_association
|
212
|
-
endpoint = stub_request(:post, 'http://sushi.com/recipes/1/groups').with(body: { group: { title: 'Topping' } }).to_return_json(
|
212
|
+
endpoint = stub_request(:post, 'http://sushi.com/recipes/1/groups').with(body: { group: { title: 'Topping' } }).to_return_json(result: { title: 'Topping', recipe_id: 1 })
|
213
213
|
|
214
214
|
group = Recipe.new(id: 1).groups.build(title: 'Topping')
|
215
215
|
group.save
|
data/test/attributes_test.rb
CHANGED
@@ -3,7 +3,7 @@ require 'test_helper'
|
|
3
3
|
module Spyke
|
4
4
|
class AttributesTest < MiniTest::Test
|
5
5
|
def test_predicate_methods
|
6
|
-
stub_request(:get, 'http://sushi.com/recipes/1').to_return_json(
|
6
|
+
stub_request(:get, 'http://sushi.com/recipes/1').to_return_json(result: { id: 1, title: 'Sushi' })
|
7
7
|
|
8
8
|
recipe = Recipe.find(1)
|
9
9
|
|
@@ -12,7 +12,7 @@ module Spyke
|
|
12
12
|
end
|
13
13
|
|
14
14
|
def test_respond_to
|
15
|
-
stub_request(:get, 'http://sushi.com/recipes/1').to_return_json(
|
15
|
+
stub_request(:get, 'http://sushi.com/recipes/1').to_return_json(result: { id: 1, serves: 3 })
|
16
16
|
|
17
17
|
recipe = Recipe.find(1)
|
18
18
|
|
data/test/custom_request_test.rb
CHANGED
@@ -3,7 +3,7 @@ require 'test_helper'
|
|
3
3
|
module Spyke
|
4
4
|
class CustomRequestTest < MiniTest::Test
|
5
5
|
def test_custom_get_request_from_class
|
6
|
-
endpoint = stub_request(:get, 'http://sushi.com/recipes/recent').to_return_json(
|
6
|
+
endpoint = stub_request(:get, 'http://sushi.com/recipes/recent').to_return_json(result: [{ id: 1, title: 'Bread' }])
|
7
7
|
assert_equal %w{ Bread }, Recipe.get('/recipes/recent').map(&:title)
|
8
8
|
assert_requested endpoint
|
9
9
|
end
|
@@ -23,7 +23,7 @@ module Spyke
|
|
23
23
|
end
|
24
24
|
|
25
25
|
def test_custom_get_request_from_class
|
26
|
-
endpoint = stub_request(:get, 'http://sushi.com/recipes/recent').to_return_json(
|
26
|
+
endpoint = stub_request(:get, 'http://sushi.com/recipes/recent').to_return_json(result: [{ id: 1, title: 'Bread' }])
|
27
27
|
assert_equal %w{ Bread }, Recipe.get('/recipes/recent').map(&:title)
|
28
28
|
assert_requested endpoint
|
29
29
|
end
|
@@ -35,7 +35,7 @@ module Spyke
|
|
35
35
|
end
|
36
36
|
|
37
37
|
def test_custom_put_request_from_instance
|
38
|
-
endpoint = stub_request(:put, 'http://sushi.com/recipes/1/publish').to_return_json(
|
38
|
+
endpoint = stub_request(:put, 'http://sushi.com/recipes/1/publish').to_return_json(result: { id: 1, status: 'published' })
|
39
39
|
recipe = Recipe.new(id: 1, status: 'unpublished')
|
40
40
|
recipe.put('/recipes/:id/publish')
|
41
41
|
|
data/test/orm_test.rb
CHANGED
@@ -3,8 +3,8 @@ require 'test_helper'
|
|
3
3
|
module Spyke
|
4
4
|
class OrmTest < MiniTest::Test
|
5
5
|
def test_find
|
6
|
-
stub_request(:get, 'http://sushi.com/recipes/1').to_return_json(
|
7
|
-
stub_request(:get, 'http://sushi.com/users/1').to_return_json(
|
6
|
+
stub_request(:get, 'http://sushi.com/recipes/1').to_return_json(result: { id: 1, title: 'Sushi' })
|
7
|
+
stub_request(:get, 'http://sushi.com/users/1').to_return_json(result: { id: 1, name: 'Bob' })
|
8
8
|
|
9
9
|
recipe = Recipe.find(1)
|
10
10
|
user = User.find(1)
|
@@ -15,18 +15,18 @@ module Spyke
|
|
15
15
|
end
|
16
16
|
|
17
17
|
def test_reload
|
18
|
-
stub_request(:get, 'http://sushi.com/recipes/1').to_return_json(
|
18
|
+
stub_request(:get, 'http://sushi.com/recipes/1').to_return_json(result: { id: 1, title: 'Sushi' })
|
19
19
|
|
20
20
|
recipe = Recipe.find(1)
|
21
21
|
assert_equal 'Sushi', recipe.title
|
22
22
|
|
23
|
-
stub_request(:get, 'http://sushi.com/recipes/1').to_return_json(
|
23
|
+
stub_request(:get, 'http://sushi.com/recipes/1').to_return_json(result: { id: 1, title: 'Sashimi' })
|
24
24
|
recipe.reload
|
25
25
|
assert_equal 'Sashimi', recipe.title
|
26
26
|
end
|
27
27
|
|
28
28
|
def test_find_with_slug
|
29
|
-
endpoint = stub_request(:get, 'http://sushi.com/recipes/1').to_return_json(
|
29
|
+
endpoint = stub_request(:get, 'http://sushi.com/recipes/1').to_return_json(result: { id: 1 })
|
30
30
|
Recipe.find('1-delicious-soup')
|
31
31
|
assert_requested endpoint
|
32
32
|
end
|
@@ -40,7 +40,7 @@ module Spyke
|
|
40
40
|
end
|
41
41
|
|
42
42
|
def test_save_new_record
|
43
|
-
endpoint = stub_request(:post, 'http://sushi.com/recipes').with(body: { recipe: { title: 'Sushi' } }).to_return_json(
|
43
|
+
endpoint = stub_request(:post, 'http://sushi.com/recipes').with(body: { recipe: { title: 'Sushi' } }).to_return_json(result: { id: 1, title: 'Sushi (created)' })
|
44
44
|
|
45
45
|
recipe = Recipe.new(title: 'Sushi')
|
46
46
|
recipe.save
|
@@ -51,7 +51,7 @@ module Spyke
|
|
51
51
|
|
52
52
|
def test_save_persisted_record
|
53
53
|
stub_request(:put, /.*/)
|
54
|
-
endpoint = stub_request(:put, 'http://sushi.com/recipes/1').with(body: { recipe: { title: 'Sushi' } }).to_return_json(
|
54
|
+
endpoint = stub_request(:put, 'http://sushi.com/recipes/1').with(body: { recipe: { title: 'Sushi' } }).to_return_json(result: { id: 1, title: 'Sushi (saved)' })
|
55
55
|
|
56
56
|
recipe = Recipe.new(id: 1, title: 'Sashimi')
|
57
57
|
recipe.title = 'Sushi'
|
@@ -62,7 +62,7 @@ module Spyke
|
|
62
62
|
end
|
63
63
|
|
64
64
|
def test_create
|
65
|
-
endpoint = stub_request(:post, 'http://sushi.com/recipes').with(body: { recipe: { title: 'Sushi' } }).to_return_json(
|
65
|
+
endpoint = stub_request(:post, 'http://sushi.com/recipes').with(body: { recipe: { title: 'Sushi' } }).to_return_json(result: { id: 1, title: 'Sushi' })
|
66
66
|
|
67
67
|
recipe = Recipe.create(title: 'Sushi')
|
68
68
|
|
@@ -71,7 +71,7 @@ module Spyke
|
|
71
71
|
end
|
72
72
|
|
73
73
|
def test_find_using_custom_uri_template
|
74
|
-
endpoint = stub_request(:get, 'http://sushi.com/images/photos/1').to_return_json(
|
74
|
+
endpoint = stub_request(:get, 'http://sushi.com/images/photos/1').to_return_json(result: { id: 1 })
|
75
75
|
Photo.find(1)
|
76
76
|
assert_requested endpoint
|
77
77
|
end
|
data/test/relation_test.rb
CHANGED
@@ -3,7 +3,7 @@ require 'test_helper'
|
|
3
3
|
module Spyke
|
4
4
|
class RelationTest < MiniTest::Test
|
5
5
|
def test_all
|
6
|
-
stub_request(:get, 'http://sushi.com/recipes').to_return_json(
|
6
|
+
stub_request(:get, 'http://sushi.com/recipes').to_return_json(result: [{ id: 1, title: 'Sushi' }, { id: 2, title: 'Nigiri' }], metadata: 'meta')
|
7
7
|
|
8
8
|
recipes = Recipe.all
|
9
9
|
|
data/test/support/api.rb
CHANGED
@@ -1,6 +1,18 @@
|
|
1
1
|
# Set up dummy api
|
2
|
+
class JSONParser < Faraday::Response::Middleware
|
3
|
+
def parse(body)
|
4
|
+
json = MultiJson.load(body, symbolize_keys: true)
|
5
|
+
{
|
6
|
+
data: json[:result],
|
7
|
+
metadata: json[:metadata]
|
8
|
+
}
|
9
|
+
rescue MultiJson::ParseError => exception
|
10
|
+
{ error: exception.cause }
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
2
14
|
Spyke::Config.connection = Faraday.new(url: 'http://sushi.com') do |faraday|
|
3
15
|
faraday.request :json
|
4
|
-
faraday.
|
16
|
+
faraday.use JSONParser
|
5
17
|
faraday.adapter Faraday.default_adapter
|
6
18
|
end
|
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: 1.0.
|
4
|
+
version: 1.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jens Balvig
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-11-
|
11
|
+
date: 2014-11-25 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -71,12 +71,12 @@ dependencies:
|
|
71
71
|
- !ruby/object:Gem::Version
|
72
72
|
version: '2.0'
|
73
73
|
- !ruby/object:Gem::Dependency
|
74
|
-
name:
|
74
|
+
name: faraday_middleware
|
75
75
|
requirement: !ruby/object:Gem::Requirement
|
76
76
|
requirements:
|
77
77
|
- - ">="
|
78
78
|
- !ruby/object:Gem::Version
|
79
|
-
version: 0.
|
79
|
+
version: 0.9.1
|
80
80
|
- - "<"
|
81
81
|
- !ruby/object:Gem::Version
|
82
82
|
version: '2.0'
|
@@ -86,38 +86,44 @@ dependencies:
|
|
86
86
|
requirements:
|
87
87
|
- - ">="
|
88
88
|
- !ruby/object:Gem::Version
|
89
|
-
version: 0.
|
89
|
+
version: 0.9.1
|
90
90
|
- - "<"
|
91
91
|
- !ruby/object:Gem::Version
|
92
92
|
version: '2.0'
|
93
93
|
- !ruby/object:Gem::Dependency
|
94
|
-
name:
|
94
|
+
name: uri_template
|
95
95
|
requirement: !ruby/object:Gem::Requirement
|
96
96
|
requirements:
|
97
|
-
- - "
|
97
|
+
- - ">="
|
98
98
|
- !ruby/object:Gem::Version
|
99
|
-
version:
|
100
|
-
|
99
|
+
version: 0.7.0
|
100
|
+
- - "<"
|
101
|
+
- !ruby/object:Gem::Version
|
102
|
+
version: '2.0'
|
103
|
+
type: :runtime
|
101
104
|
prerelease: false
|
102
105
|
version_requirements: !ruby/object:Gem::Requirement
|
103
106
|
requirements:
|
104
|
-
- - "
|
107
|
+
- - ">="
|
105
108
|
- !ruby/object:Gem::Version
|
106
|
-
version:
|
109
|
+
version: 0.7.0
|
110
|
+
- - "<"
|
111
|
+
- !ruby/object:Gem::Version
|
112
|
+
version: '2.0'
|
107
113
|
- !ruby/object:Gem::Dependency
|
108
|
-
name:
|
114
|
+
name: bundler
|
109
115
|
requirement: !ruby/object:Gem::Requirement
|
110
116
|
requirements:
|
111
|
-
- - "
|
117
|
+
- - "~>"
|
112
118
|
- !ruby/object:Gem::Version
|
113
|
-
version: '
|
119
|
+
version: '1.6'
|
114
120
|
type: :development
|
115
121
|
prerelease: false
|
116
122
|
version_requirements: !ruby/object:Gem::Requirement
|
117
123
|
requirements:
|
118
|
-
- - "
|
124
|
+
- - "~>"
|
119
125
|
- !ruby/object:Gem::Version
|
120
|
-
version: '
|
126
|
+
version: '1.6'
|
121
127
|
- !ruby/object:Gem::Dependency
|
122
128
|
name: minitest
|
123
129
|
requirement: !ruby/object:Gem::Requirement
|