spyke 1.7.2 → 1.8.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: 6bb23a4da9d9143ff6f62b5887a54b0244746be8
4
- data.tar.gz: 926010cb42bae7510da165fa4c94cc69dbe96faa
3
+ metadata.gz: aae9497db570bde9804ea90d14418f739aa9b739
4
+ data.tar.gz: 33ded16c0322550c0694cbc36f6a64ca6b46ce6f
5
5
  SHA512:
6
- metadata.gz: 8a5ca662137935d28cd46ebe4fda3ca44a0158edde6c1d658f1a7195445a565626d393abb571d448564cee4910b75d9a477789b81b521591e56be0edac270267
7
- data.tar.gz: 87ba21f060e68307b1aaf49386d180e2a511a5a5d8b4af9a68bd1f82ad9a585b4bb5760ea17e674d21a60e2e5260e4aaeaaa7702877cf14e0087b2d985fbee82
6
+ metadata.gz: bcaf128e92e898c6036b080afc1a8ae73b328bf17c5dfa2c4480dff9a9e38038a58792aa7fc6beacf3a3bba3f6a7f5d962602ba113e26675939851cfc25e14c6
7
+ data.tar.gz: 2ed6ec2a7c5cb0203d78fde3addde56192f72777a2387695b92944caca91ea70cb68eb0d8646cce4e0d00f1d1b57ad0486cd743b0be34e48a2250044f0999986
data/README.md CHANGED
@@ -22,6 +22,7 @@ We therefore made Spyke which adds a few fixes/features needed for our projects:
22
22
  - Proper support for scopes
23
23
  - Ability to define custom URIs for associations
24
24
  - ActiveRecord-like log output
25
+ - Handling of server side validations
25
26
  - Googlable name! :)
26
27
 
27
28
  ## Configuration
@@ -41,7 +42,7 @@ Spyke uses Faraday to handle requests and expects it to parse the response body
41
42
  So, for example for an API that returns JSON in the following format:
42
43
 
43
44
  ```json
44
- { "result": { "id": 1, "name": "Bob" }, "metadata": {}, "message": "" }
45
+ { "result": { "id": 1, "name": "Bob" }, "extra": {}, "errors": {} }
45
46
  ```
46
47
 
47
48
  ...the simplest possible configuration that could work is something like this:
@@ -54,11 +55,11 @@ class JSONParser < Faraday::Response::Middleware
54
55
  json = MultiJson.load(body, symbolize_keys: true)
55
56
  {
56
57
  data: json[:result],
57
- metadata: json[:metadata],
58
- errors: [json[:message]]
58
+ metadata: json[:extra],
59
+ errors: json[:errors]
59
60
  }
60
61
  rescue MultiJson::ParseError => exception
61
- { errors: [exception.cause] }
62
+ { errors: { base: [ error: exception.message ] } }
62
63
  end
63
64
  end
64
65
 
@@ -139,6 +140,20 @@ Processing by PostsController#index as HTML
139
140
  Completed 200 OK in 75ms (Views: 64.6ms | Spyke: 40.3ms | ActiveRecord: 0ms)
140
141
  ```
141
142
 
143
+ ### Server-side validations
144
+
145
+ Spyke expects errors to be formatted in the same way as the
146
+ ActiveModel::Errors hash, ie:
147
+
148
+ ```ruby
149
+ { title: [{ error: 'blank'}, { error: 'too_short', count: 10 }]}
150
+ ```
151
+
152
+ If the API you're using returns errors in a different format you can
153
+ remap it in Faraday to match the above. Doing this will allow you to
154
+ show errors returned from the server in forms and f.ex using
155
+ `@post.errors.full_messages` just like ActiveRecord.
156
+
142
157
  ## Contributing
143
158
 
144
159
  If possible please take a look at the [tests marked "wishlisted"](https://github.com/balvig/spyke/search?utf8=%E2%9C%93&q=wishlisted)!
data/lib/spyke/base.rb CHANGED
@@ -9,12 +9,7 @@ require 'spyke/scoping'
9
9
  module Spyke
10
10
  class Base
11
11
  # ActiveModel
12
- include ActiveModel::Conversion
13
12
  include ActiveModel::Model
14
- include ActiveModel::Validations
15
- include ActiveModel::Validations::Callbacks
16
- extend ActiveModel::Translation
17
- extend ActiveModel::Callbacks
18
13
 
19
14
  # Spyke
20
15
  include Associations
data/lib/spyke/http.rb CHANGED
@@ -66,7 +66,7 @@ module Spyke
66
66
 
67
67
  result = self.class.send("#{method}_raw", path, params)
68
68
 
69
- result.errors.each { |error| errors.add(:base, error) }
69
+ add_errors_to_model(result.errors)
70
70
  self.attributes = result.data
71
71
  end
72
72
  end
@@ -77,6 +77,15 @@ module Spyke
77
77
 
78
78
  private
79
79
 
80
+ def add_errors_to_model(errors_hash)
81
+ errors_hash.each do |field, field_errors|
82
+ field_errors.each do |attributes|
83
+ error_name = attributes.delete(:error).to_sym
84
+ errors.add(field.to_sym, error_name, attributes.symbolize_keys)
85
+ end
86
+ end
87
+ end
88
+
80
89
  def resolve_path_from_action(action)
81
90
  case action
82
91
  when Symbol then uri.join(action)
data/lib/spyke/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Spyke
2
- VERSION = '1.7.2'
2
+ VERSION = '1.8.0'
3
3
  end
data/test/orm_test.rb CHANGED
@@ -79,13 +79,13 @@ module Spyke
79
79
  assert_requested endpoint
80
80
  end
81
81
 
82
- def test_create_with_server_failure
83
- endpoint = stub_request(:put, 'http://sushi.com/recipes/1').to_return_json(id: 'write_error:400', message: 'Unable to save recipe')
82
+ def test_create_with_server_returning_validation_errors
83
+ endpoint = stub_request(:put, 'http://sushi.com/recipes/1').to_return_json(id: 'write_error:400', errors: { title: [{ error: 'too_short', count: 4 }] })
84
84
 
85
- recipe = Recipe.create(id: 1, title: 'Sushi')
85
+ recipe = Recipe.create(id: 1, title: 'sus')
86
86
 
87
- assert_equal 'Sushi', recipe.title
88
- assert_equal ['Unable to save recipe'], recipe.errors[:base]
87
+ assert_equal 'sus', recipe.title
88
+ assert_equal ['Title is too short (minimum is 4 characters)'], recipe.errors.full_messages
89
89
  assert_requested endpoint
90
90
  end
91
91
 
@@ -146,10 +146,5 @@ module Spyke
146
146
  RecipeImage.where(recipe_id: 1).destroy
147
147
  assert_requested endpoint
148
148
  end
149
-
150
- def test_validations
151
- assert_equal false, RecipeImage.new.valid?
152
- assert_equal true, RecipeImage.new(url: 'bob.jpg').valid?
153
- end
154
149
  end
155
150
  end
data/test/support/api.rb CHANGED
@@ -5,10 +5,10 @@ class JSONParser < Faraday::Response::Middleware
5
5
  {
6
6
  data: json[:result],
7
7
  metadata: json[:metadata],
8
- errors: [json[:message]]
8
+ errors: json[:errors]
9
9
  }
10
10
  rescue MultiJson::ParseError => exception
11
- { errors: [exception.cause] }
11
+ { errors: { base: [ error: exception.message ] } }
12
12
  end
13
13
  end
14
14
 
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.7.2
4
+ version: 1.8.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-26 00:00:00.000000000 Z
11
+ date: 2015-01-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport