spyke 1.2.1 → 1.3.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: 9261f1e664c27242d946133701985395af962417
4
- data.tar.gz: 11bba7b0d4ca6a79a0066e1faeddf43d19cd339e
3
+ metadata.gz: 54db6401a61650bf3c2a4eb03c31c7bad268e08f
4
+ data.tar.gz: 32f5992d2ad3dc9c412d254e276374e38b862e9c
5
5
  SHA512:
6
- metadata.gz: 1bd18ad389c88b9831ea2b028fb2179064283d7b359a0d4cb99ab4d7fb88edefc1f2fabb45993ef4fea00773eb8f36add91610165b3eaa312a61714b3fcb1389
7
- data.tar.gz: 8b392cff15ce16f2d473f27aa967bd91121e1a80e78a0cbccef1669ae28791fa6501a07eee81079590eb58c2b302c0097eb202025a1f22e9d217bffd7b5226a3
6
+ metadata.gz: 464225c61b899a3fa8c0b67c50e89a6b7899d77ab16d2fdecc965305d010047cbaa2a91ff6afe981352093b97529f43204bc829fc539b7e650d00747c6db7a44
7
+ data.tar.gz: 720b15b5ee804462d4120e271fc03d3a1bc54fb15054fc1d8f370c15a0c35d5914d7533b5d601f554cadb3beec3f90ebcde55e1e1a82559d60d89d7d4cacc036
data/README.md CHANGED
@@ -47,10 +47,11 @@ class JSONParser < Faraday::Response::Middleware
47
47
  json = MultiJson.load(body, symbolize_keys: true)
48
48
  {
49
49
  data: json[:result],
50
- metadata: json[:metadata]
50
+ metadata: json[:metadata],
51
+ errors: [json[:message]]
51
52
  }
52
53
  rescue MultiJson::ParseError => exception
53
- { error: exception.cause }
54
+ { errors: [exception.cause] }
54
55
  end
55
56
  end
56
57
 
@@ -70,19 +71,19 @@ class User < Spyke::Base
70
71
  has_many :posts
71
72
  end
72
73
 
73
- user = User.find(3)
74
+ user = User.find(3)
74
75
  # => GET http://api.com/users/3
75
76
 
76
- user.posts
77
+ user.posts
77
78
  # => find embedded in returned JSON or GET http://api.com/users/3/posts
78
79
 
79
- user.update_attributes(name: 'Alice')
80
+ user.update_attributes(name: 'Alice')
80
81
  # => PUT http://api.com/users/3 - { user: { name: 'Alice' } }
81
82
 
82
- user.destroy
83
+ user.destroy
83
84
  # => DELETE http://api.com/users/3
84
85
 
85
- User.create(name: 'Bob')
86
+ User.create(name: 'Bob')
86
87
  # => POST http://api.com/users - { user: { name: 'Bob' } }
87
88
  ```
88
89
 
data/lib/spyke/http.rb CHANGED
@@ -62,17 +62,27 @@ module Spyke
62
62
  METHODS.each do |method|
63
63
  define_method(method) do |action = nil, params = {}|
64
64
  params = action if action.is_a?(Hash)
65
- path = case action
66
- when Symbol then uri.join(action)
67
- when String then Path.new(action, attributes)
68
- else uri
69
- end
70
- self.attributes = self.class.send("#{method}_raw", path, params).data
65
+ path = resolve_path_from_action(action)
66
+
67
+ result = self.class.send("#{method}_raw", path, params)
68
+
69
+ result.errors.each { |error| errors.add(:base, error) }
70
+ self.attributes = result.data
71
71
  end
72
72
  end
73
73
 
74
74
  def uri
75
75
  Path.new(@uri_template, attributes)
76
76
  end
77
+
78
+ private
79
+
80
+ def resolve_path_from_action(action)
81
+ case action
82
+ when Symbol then uri.join(action)
83
+ when String then Path.new(action, attributes)
84
+ else uri
85
+ end
86
+ end
77
87
  end
78
88
  end
data/lib/spyke/result.rb CHANGED
@@ -19,7 +19,7 @@ module Spyke
19
19
  end
20
20
 
21
21
  def errors
22
- body[:errors]
22
+ body[:errors] || []
23
23
  end
24
24
  end
25
25
  end
data/lib/spyke/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Spyke
2
- VERSION = '1.2.1'
2
+ VERSION = '1.3.0'
3
3
  end
data/test/orm_test.rb CHANGED
@@ -79,6 +79,16 @@ 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')
84
+
85
+ recipe = Recipe.create(id: 1, title: 'Sushi')
86
+
87
+ assert_equal 'Sushi', recipe.title
88
+ assert_equal ['Unable to save recipe'], recipe.errors[:base]
89
+ assert_requested endpoint
90
+ end
91
+
82
92
  def test_find_using_custom_uri_template
83
93
  endpoint = stub_request(:get, 'http://sushi.com/images/photos/1').to_return_json(result: { id: 1 })
84
94
  Photo.find(1)
data/test/support/api.rb CHANGED
@@ -4,10 +4,11 @@ class JSONParser < Faraday::Response::Middleware
4
4
  json = MultiJson.load(body, symbolize_keys: true)
5
5
  {
6
6
  data: json[:result],
7
- metadata: json[:metadata]
7
+ metadata: json[:metadata],
8
+ errors: [json[:message]]
8
9
  }
9
10
  rescue MultiJson::ParseError => exception
10
- { error: exception.cause }
11
+ { errors: [exception.cause] }
11
12
  end
12
13
  end
13
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.2.1
4
+ version: 1.3.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: 2014-12-12 00:00:00.000000000 Z
11
+ date: 2015-01-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport