spyke 1.6.0 → 1.7.0

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: d8c05691c5a62ac3b717844d0c3f87890ab7c902
4
- data.tar.gz: 7edc244c8975b0ec3c5f1887e53866fafb8dde04
3
+ metadata.gz: b1e4e0a09a6e130d78cdfc11ad4c46baee78c332
4
+ data.tar.gz: ef161ce8bdf5013e36e41e5054ac461d58c84b88
5
5
  SHA512:
6
- metadata.gz: 05a0f43e35a3411bfab96a7825ad75a9721d3177fc90d1c6b26a29c9d68e43e5476448c14bbba8972ac4d7458efd64a89a04acac929340551d0378f64717b5cb
7
- data.tar.gz: e1a53e75a003f0a7dba3c0ecf5151a7b40477b97ab5cc85ddce8e1535ca875ba79c00a04ab679dd3d1904d70049ddf29163a9c2cac6c22ddbd6fffd21ca8a991
6
+ metadata.gz: bf368cf28410559c6fdc38f3f14c24b28effe957d05249aced614adfcbcb39869dc6092c329c46c1bc79ce30200167e481db4c1a1925005c21d6072596d5a3e5
7
+ data.tar.gz: be1d98fa2a3fab4ef8e19cee15d179a4a937a8108bfe5f789a6406aa9e2c9652bd754e256eb25ecbbb59dea79d40585a3d29351d505e335a65eb14d04e2d6c2a
data/README.md CHANGED
@@ -58,7 +58,7 @@ end
58
58
  Spyke::Config.connection = Faraday.new(url: 'http://api.com') do |c|
59
59
  c.request :json
60
60
  c.use JSONParser
61
- c.use Faraday.default_adapter
61
+ c.adapter Faraday.default_adapter
62
62
  end
63
63
  ```
64
64
 
@@ -104,10 +104,10 @@ You can specify custom URIs on both the class and association level:
104
104
 
105
105
  ```ruby
106
106
  class User < Spyke::Base
107
- uri '/v1/users/:id'
107
+ uri '/v1/users/(:id)' # id optional, both /v1/users and /v1/users/4 are usable
108
108
 
109
109
  has_one :image, uri: nil
110
- has_many :posts, uri: '/posts/for_user/:user_id'
110
+ has_many :posts, uri: '/posts/for_user/:user_id' # user_id is required
111
111
  end
112
112
 
113
113
  class Post < Spyke::Base
@@ -3,7 +3,7 @@ module Spyke
3
3
  class HasMany < Association
4
4
  def initialize(*args)
5
5
  super
6
- @options.reverse_merge!(uri: "/#{parent.class.model_name.plural}/:#{foreign_key}/#{klass.model_name.plural}/:id")
6
+ @options.reverse_merge!(uri: "/#{parent.class.model_name.plural}/:#{foreign_key}/#{klass.model_name.plural}/(:id)")
7
7
  @params[foreign_key] = parent.id
8
8
  end
9
9
 
data/lib/spyke/http.rb CHANGED
@@ -50,7 +50,7 @@ module Spyke
50
50
  Collection.new Array(result.data).map { |record| new(record) }, result.metadata
51
51
  end
52
52
 
53
- def uri(uri_template = "/#{model_name.plural}/:id")
53
+ def uri(uri_template = "/#{model_name.plural}/(:id)")
54
54
  @uri ||= uri_template
55
55
  end
56
56
 
data/lib/spyke/path.rb CHANGED
@@ -1,10 +1,12 @@
1
1
  require 'uri_template'
2
2
 
3
3
  module Spyke
4
+ class InvalidPathError < StandardError; end
4
5
  class Path
5
- def initialize(uri_template, params = {})
6
- @uri_template = URITemplate.new(:colon, uri_template)
7
- @params = params
6
+
7
+ def initialize(pattern, params = {})
8
+ @pattern = pattern
9
+ @params = params.symbolize_keys
8
10
  end
9
11
 
10
12
  def join(other_path)
@@ -16,13 +18,36 @@ module Spyke
16
18
  end
17
19
 
18
20
  def variables
19
- @variables ||= @uri_template.variables.map(&:to_sym)
21
+ @variables ||= uri_template.variables.map(&:to_sym)
20
22
  end
21
23
 
22
24
  private
23
25
 
26
+ def uri_template
27
+ @uri_template ||= URITemplate.new(:colon, pattern_with_rfc_style_parens)
28
+ end
29
+
30
+ def pattern_with_rfc_style_parens
31
+ @pattern.gsub('(', '{').gsub(')', '}')
32
+ end
33
+
24
34
  def path
25
- @uri_template.expand(@params).chomp('/')
35
+ validate_required_params!
36
+ uri_template.expand(@params).chomp('/')
37
+ end
38
+
39
+ def validate_required_params!
40
+ if missing_required_params.any?
41
+ raise Spyke::InvalidPathError, "Missing required params: #{missing_required_params.join(', ')} in #{@pattern}. Mark optional params with parens eg: (:param)"
42
+ end
43
+ end
44
+
45
+ def missing_required_params
46
+ required_params - @params.keys
47
+ end
48
+
49
+ def required_params
50
+ @pattern.scan(/\/:(\w+)/).flatten.map(&:to_sym)
26
51
  end
27
52
  end
28
53
  end
data/lib/spyke/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Spyke
2
- VERSION = '1.6.0'
2
+ VERSION = '1.7.0'
3
3
  end
data/test/path_test.rb CHANGED
@@ -3,15 +3,15 @@ require 'test_helper'
3
3
  module Spyke
4
4
  class PathTest < MiniTest::Test
5
5
  def test_collection_path
6
- assert_equal '/recipes', Path.new('/recipes/:id').to_s
6
+ assert_equal '/recipes', Path.new('/recipes/(:id)').to_s
7
7
  end
8
8
 
9
9
  def test_resource_path
10
- assert_equal '/recipes/2', Path.new('/recipes/:id', id: 2).to_s
10
+ assert_equal '/recipes/2', Path.new('/recipes/(:id)', id: 2).to_s
11
11
  end
12
12
 
13
13
  def test_nested_collection_path
14
- path = Path.new('/users/:user_id/recipes/:id', user_id: 1, status: 'published')
14
+ path = Path.new('/users/:user_id/recipes/(:id)', user_id: 1, status: 'published')
15
15
  assert_equal [:user_id, :id], path.variables
16
16
  assert_equal '/users/1/recipes', path.to_s
17
17
  end
@@ -19,5 +19,11 @@ module Spyke
19
19
  def test_nested_resource_path
20
20
  assert_equal '/users/1/recipes/2', Path.new('/users/:user_id/recipes/:id', user_id: 1, id: 2).to_s
21
21
  end
22
+
23
+ def test_required_params
24
+ assert_raises Spyke::InvalidPathError, 'Missing required params: user_id in /users/:user_id/recipes/(:id)' do
25
+ Path.new('/users/:user_id/recipes/(:id)', id: 2).to_s
26
+ end
27
+ end
22
28
  end
23
29
  end
@@ -116,5 +116,13 @@ module Spyke
116
116
  Recipe.published.to_a
117
117
  assert_requested endpoint_2, times: 1
118
118
  end
119
+
120
+ def test_namespaced_model
121
+ tip_endpoint = stub_request(:get, 'http://sushi.com/tips/1').to_return_json(result: { id: 1 })
122
+ likes_endpoint = stub_request(:get, 'http://sushi.com/tips/1/likes')
123
+ Cookbook::Tip.find(1).likes.first
124
+ assert_requested tip_endpoint
125
+ assert_requested likes_endpoint
126
+ end
119
127
  end
120
128
  end
@@ -59,7 +59,7 @@ class Group < Spyke::Base
59
59
  end
60
60
 
61
61
  class Ingredient < Spyke::Base
62
- uri '/recipes/:recipe_id/ingredients/:id'
62
+ uri '/recipes/:recipe_id/ingredients/(:id)'
63
63
  end
64
64
 
65
65
  class User < Spyke::Base
@@ -67,7 +67,7 @@ class User < Spyke::Base
67
67
  end
68
68
 
69
69
  class Photo < Spyke::Base
70
- uri '/images/photos/:id'
70
+ uri '/images/photos/(:id)'
71
71
  end
72
72
 
73
73
  class Comment < Spyke::Base
@@ -87,3 +87,13 @@ class Search
87
87
  recipes.metadata[:suggestions]
88
88
  end
89
89
  end
90
+
91
+ module Cookbook
92
+ class Tip < Spyke::Base
93
+ uri '/tips/(:id)'
94
+ has_many :likes, class_name: 'Cookbook::Like', uri: '/tips/:cookbook_tip_id/likes/(:id)'
95
+ end
96
+
97
+ class Like < Spyke::Base
98
+ end
99
+ 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.6.0
4
+ version: 1.7.0
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-01-16 00:00:00.000000000 Z
11
+ date: 2015-01-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport