spyke 1.8.3 → 1.8.4

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: c511b748bec0a2438f2c05d6cd2e76346b55f387
4
- data.tar.gz: 67f632949d0fd0429bb3576d84e771c9709f74c8
3
+ metadata.gz: 2321a4ffd56617ec8f489fbe3ddb96061c4ece5f
4
+ data.tar.gz: f7f97d6d1b1356db71a0f384e8c5257ffe81f9ef
5
5
  SHA512:
6
- metadata.gz: 1241be3c296ac93e9bfa523ec598f0f550aa122897a34b2a322f378251d9a943b8a7a07818abab823ffcf686c8aa064f3e46c2b824df5ec4471f81a05e28dc2a
7
- data.tar.gz: accfea484c16960b90f02696489ddb98bd02682779c2e6cf4ceae21ee448e430153f07edd32241c36297c93114c94f5780477d314d4ea1a0ff285c06a63ac96a
6
+ metadata.gz: 6d7f5283de00b250dae9734458d8ef06ceb20705b99f809ec321a5d5701520342ec78114b8d1c9902c4bae75b482050c750ac22fef551c6850fee055083bea3a
7
+ data.tar.gz: 61c0cfe2bd04c618512da051ac1c4c35de569b5ba6dc5405580aea0615c2d04377967c4e3c5bf2dd6a0998c1c34284a012eed504169e8399fcb70cb6e307a098
@@ -11,7 +11,7 @@ module Spyke
11
11
  module ClassMethods
12
12
  METHODS.each do |method|
13
13
  define_method(method) do |path, params = {}|
14
- new_or_collection_from_result send("#{method}_raw", path, params)
14
+ new_instance_or_collection_from_result send("#{method}_raw", path, params)
15
15
  end
16
16
 
17
17
  define_method("#{method}_raw") do |path, params = {}|
@@ -19,30 +19,7 @@ module Spyke
19
19
  end
20
20
  end
21
21
 
22
- def request(method, path, params = {})
23
- ActiveSupport::Notifications.instrument('request.spyke', method: method) do |payload|
24
- response = connection.send(method) do |request|
25
- if method == :get
26
- request.url path.to_s, params
27
- else
28
- request.url path.to_s
29
- request.body = params
30
- end
31
- end
32
- payload[:url], payload[:status] = response.env.url, response.status
33
- Result.new_from_response(response)
34
- end
35
- end
36
-
37
- def new_or_collection_from_result(result)
38
- if result.data.is_a?(Array)
39
- new_collection_from_result(result)
40
- else
41
- new_from_result(result)
42
- end
43
- end
44
-
45
- def new_from_result(result)
22
+ def new_instance_from_result(result)
46
23
  new result.data if result.data
47
24
  end
48
25
 
@@ -50,13 +27,46 @@ module Spyke
50
27
  Collection.new Array(result.data).map { |record| new(record) }, result.metadata
51
28
  end
52
29
 
53
- def uri(uri_template = "/#{model_name.plural}/(:id)")
54
- @uri ||= uri_template
30
+ def uri(uri_template = nil)
31
+ @uri ||= uri_template || superclass_uri || default_uri
55
32
  end
56
33
 
57
34
  def connection
58
35
  Config.connection
59
36
  end
37
+
38
+ private
39
+
40
+ def request(method, path, params = {})
41
+ ActiveSupport::Notifications.instrument('request.spyke', method: method) do |payload|
42
+ response = connection.send(method) do |request|
43
+ if method == :get
44
+ request.url path.to_s, params
45
+ else
46
+ request.url path.to_s
47
+ request.body = params
48
+ end
49
+ end
50
+ payload[:url], payload[:status] = response.env.url, response.status
51
+ Result.new_from_response(response)
52
+ end
53
+ end
54
+
55
+ def new_instance_or_collection_from_result(result)
56
+ if result.data.is_a?(Array)
57
+ new_collection_from_result(result)
58
+ else
59
+ new_instance_from_result(result)
60
+ end
61
+ end
62
+
63
+ def superclass_uri
64
+ superclass.uri.dup.freeze if superclass != Base
65
+ end
66
+
67
+ def default_uri
68
+ "/#{model_name.plural}/(:id)"
69
+ end
60
70
  end
61
71
 
62
72
  METHODS.each do |method|
@@ -24,7 +24,7 @@ module Spyke
24
24
  end
25
25
 
26
26
  def find_one
27
- @find_one ||= klass.new_from_result(fetch)
27
+ @find_one ||= klass.new_instance_from_result(fetch)
28
28
  end
29
29
 
30
30
  def find_some
@@ -1,3 +1,3 @@
1
1
  module Spyke
2
- VERSION = '1.8.3'
2
+ VERSION = '1.8.4'
3
3
  end
@@ -19,7 +19,7 @@ module Spyke
19
19
  def test_get_request_with_appended_scope
20
20
  skip 'wishlisted'
21
21
  endpoint = stub_request(:get, 'http://sushi.com/recipes/recent?status=published')
22
- Recipe.get('/recipes/recent').published.fetch
22
+ Recipe.get('/recipes/recent').published.to_a
23
23
  assert_requested endpoint
24
24
  end
25
25
 
@@ -113,12 +113,18 @@ module Spyke
113
113
  assert_requested endpoint
114
114
  end
115
115
 
116
- def test_inheritance_using_custom_method
117
- endpoint = stub_request(:put, 'http://sushi.com/step_images')
116
+ def test_inheritance_passes_on_custom_method_and_uri
117
+ endpoint = stub_request(:put, 'http://sushi.com/images')
118
118
  StepImage.create
119
119
  assert_requested endpoint
120
120
  end
121
121
 
122
+ def test_inheritance_not_overwriting_custom_uri
123
+ endpoint = stub_request(:put, 'http://sushi.com/recipes/1/image')
124
+ RecipeImage.where(recipe_id: 1).create
125
+ assert_requested endpoint
126
+ end
127
+
122
128
  def test_to_params_without_root
123
129
  assert_equal({ 'url' => 'bob.jpg' }, RecipeImage.new(url: 'bob.jpg').to_params)
124
130
  end
@@ -16,3 +16,10 @@ Dir[File.expand_path('../support/**/*.rb', __FILE__)].each { |f| require f }
16
16
 
17
17
  # Pretty colors
18
18
  Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new
19
+
20
+ # Don't raise but report uncaught net connections
21
+ WebMock.allow_net_connect!
22
+ WebMock.stub_request(:any, /.*/).to_return do |request|
23
+ puts "\e[35mUNSTUBBED REQUEST:\e[0m #{request.method.upcase} #{request.uri}"
24
+ { body: nil }
25
+ 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.8.3
4
+ version: 1.8.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jens Balvig
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-02-11 00:00:00.000000000 Z
11
+ date: 2015-02-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport