spyke 5.4.2 → 6.1.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
  SHA256:
3
- metadata.gz: a67a8bfd8ac985172e534108b72f8a03a206daeb7fbd33a04c94f22b45e0126c
4
- data.tar.gz: 360d138ed27015204ad9a7b9be20e068b214cb1e1c4dfcea9e23da08a68f02cb
3
+ metadata.gz: d8a623ec5e619581883a58962e9961e7bc04f889e7c4dfdfd5712d2e7e82953b
4
+ data.tar.gz: e9730e060fbcd6cfb3bf4a718a779d9de490878da0728d1643fbb18f2004968f
5
5
  SHA512:
6
- metadata.gz: 8aac5b80d13d31fc9ecf437f0437dc98c59de493591ebc4307882570bea75e934e208ae991f881ab45b628e4cd177857d63dbe8546d66e6faa79c32cfe7c320a
7
- data.tar.gz: e7857930a37776d02a75493597d6859268b8955fcad572993f5666598177e352b5b5d0ca8200ab65411839ec857afe9782d7ec59d7431943a1a3e4a9b859d294
6
+ metadata.gz: ec2cacb0215d0d780484fb184e5e06aab71cc06e5b7312c41cbf337830e3ad5e1167a00fd255c0354d4293409763e03d076a53362654512f4da19fa925980485
7
+ data.tar.gz: 80607d11d9501f9b6f69cfbf311a099130c0904e37879b58a30df4794f758f131360b6c19eadd9d3086e5b8c284c4eed7399219f2edf82f8a260625c992a7997
data/README.md CHANGED
@@ -131,8 +131,6 @@ Post.find(4) # => GET http://api.com/posts/4
131
131
  Custom request methods and the `with` scope methods allow you to
132
132
  perform requests for non-REST actions:
133
133
 
134
- The `.with` scope:
135
-
136
134
  ```ruby
137
135
  Post.with('posts/recent') # => GET http://api.com/posts/recent
138
136
  Post.with(:recent) # => GET http://api.com/posts/recent
@@ -172,7 +170,7 @@ Spyke expects errors to be formatted in the same way as the
172
170
  [ActiveModel::Errors details hash](https://cowbell-labs.com/2015-01-22-active-model-errors-details.html), ie:
173
171
 
174
172
  ```ruby
175
- { title: [{ error: 'blank'}, { error: 'too_short', count: 10 }]}
173
+ { title: [{ error: 'blank'}, { error: 'too_short', count: 10 }] }
176
174
  ```
177
175
 
178
176
  If the API you're using returns errors in a different format you can
@@ -29,7 +29,7 @@ module Spyke
29
29
  candidates << type_name
30
30
 
31
31
  candidates.each do |candidate|
32
- constant = ActiveSupport::Dependencies.safe_constantize(candidate)
32
+ constant = candidate.safe_constantize
33
33
  return constant if candidate == constant.to_s
34
34
  end
35
35
  raise NameError.new("uninitialized constant #{candidates.first}", candidates.first)
data/lib/spyke/http.rb CHANGED
@@ -41,20 +41,28 @@ module Spyke
41
41
  @uri ||= uri_template || default_uri
42
42
  end
43
43
 
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
44
+ private
45
+
46
+ def send_request(method, path, params)
47
+ connection.send(method) do |request|
48
+ if method == :get
49
+ path, params = merge_query_params(path, params)
50
+ request.url path, params
51
+ else
52
+ request.url path.to_s
53
+ request.body = params
54
+ end
51
55
  end
56
+ rescue Faraday::ConnectionFailed, Faraday::TimeoutError
57
+ raise ConnectionError
52
58
  end
53
- rescue Faraday::ConnectionFailed, Faraday::TimeoutError
54
- raise ConnectionError
55
- end
56
59
 
57
- private
60
+ def merge_query_params(path, params)
61
+ parsed_uri = Addressable::URI.parse(path.to_s)
62
+ path = parsed_uri.path
63
+ params = params.merge(parsed_uri.query_values || {})
64
+ [path, params]
65
+ end
58
66
 
59
67
  def scoped_request(method)
60
68
  uri = new.uri
@@ -5,7 +5,7 @@ module Spyke
5
5
  include Enumerable
6
6
 
7
7
  attr_reader :klass
8
- attr_accessor :params
8
+ attr_reader :params
9
9
  delegate :to_ary, :[], :any?, :empty?, :last, :size, :metadata, to: :find_some
10
10
 
11
11
  def initialize(klass, options = {})
@@ -21,6 +21,10 @@ module Spyke
21
21
  relation
22
22
  end
23
23
 
24
+ def params=(params)
25
+ @params = params.symbolize_keys
26
+ end
27
+
24
28
  def with(uri)
25
29
  if uri.is_a? Symbol
26
30
  @options[:uri] = File.join @options[:uri], uri.to_s
data/lib/spyke/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Spyke
2
- VERSION = '5.4.2'
2
+ VERSION = '6.1.1'
3
3
  end
@@ -241,6 +241,15 @@ module Spyke
241
241
  assert_requested endpoint
242
242
  end
243
243
 
244
+
245
+ def test_using_where_with_custom_uri_including_query_params
246
+ endpoint = stub_request(:get, 'http://sushi.com/featured_ingredients?filter[group_id]=1&filter[status]=published')
247
+
248
+ Group.new(id: 1).featured_ingredients.where(filter: { status: 'published' }).to_a
249
+
250
+ assert_requested endpoint
251
+ end
252
+
244
253
  def test_path_inferred_from_name
245
254
  endpoint = stub_request(:get, 'http://sushi.com/recipes/1/gallery_images')
246
255
  Recipe.new(id: 1).gallery_images.to_a
data/test/path_test.rb CHANGED
@@ -21,13 +21,13 @@ module Spyke
21
21
  end
22
22
 
23
23
  def test_required_variables
24
- assert_raises Spyke::InvalidPathError, 'Missing required variables: user_id in /users/:user_id/recipes/(:id)' do
24
+ assert_raises InvalidPathError, 'Missing required variables: user_id in /users/:user_id/recipes/(:id)' do
25
25
  Path.new('/users/:user_id/recipes/(:id)', id: 2).to_s
26
26
  end
27
27
  end
28
28
 
29
29
  def test_mix_of_required_and_unrequired_variables
30
- assert_raises Spyke::InvalidPathError, 'Missing required variables: part2, part4' do
30
+ assert_raises InvalidPathError, 'Missing required variables: part2, part4' do
31
31
  Path.new('/1/profiles(/:part1)/:part2(/:part3)/:part4.xml').to_s
32
32
  end
33
33
  end
data/test/scopes_test.rb CHANGED
@@ -61,6 +61,14 @@ module Spyke
61
61
  assert_requested endpoint
62
62
  end
63
63
 
64
+ def test_where_with_stringified_keys
65
+ endpoint = stub_request(:get, 'http://sushi.com/recipes/1/image')
66
+
67
+ RecipeImage.where("recipe_id" => 1).to_a
68
+
69
+ assert_requested endpoint
70
+ end
71
+
64
72
  def test_chainable_class_method
65
73
  endpoint = stub_request(:get, 'http://sushi.com/recipes?status=published&per_page=3')
66
74
 
@@ -79,6 +79,7 @@ end
79
79
 
80
80
  class Group < Spyke::Base
81
81
  has_many :ingredients, uri: nil
82
+ has_many :featured_ingredients, uri: 'featured_ingredients?filter[group_id]=:group_id', class_name: "Ingredient"
82
83
  accepts_nested_attributes_for :ingredients
83
84
 
84
85
  def self.build_default
@@ -119,10 +120,10 @@ end
119
120
  class OtherRecipe < OtherApi
120
121
  uri 'recipes/(:id)'
121
122
 
122
- def self.send_request(method, path, params)
123
+ def self.request(method, path, params)
123
124
  super
124
125
  rescue Spyke::ConnectionError
125
- Recipe.send_request(method, path, params)
126
+ Recipe.request(method, path, params)
126
127
  end
127
128
  end
128
129
 
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.2
4
+ version: 6.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jens Balvig
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-03-15 00:00:00.000000000 Z
11
+ date: 2021-12-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -318,7 +318,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
318
318
  - !ruby/object:Gem::Version
319
319
  version: '0'
320
320
  requirements: []
321
- rubygems_version: 3.0.3
321
+ rubygems_version: 3.2.3
322
322
  signing_key:
323
323
  specification_version: 4
324
324
  summary: Interact with REST services in an ActiveRecord-like manner