Skip to content

Commit

Permalink
allow returned errors to be added to base
Browse files Browse the repository at this point in the history
  • Loading branch information
balvig committed Jan 5, 2015
1 parent 10569b5 commit 9e5667d
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 17 deletions.
15 changes: 8 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,11 @@ class JSONParser < Faraday::Response::Middleware
json = MultiJson.load(body, symbolize_keys: true)
{
data: json[:result],
metadata: json[:metadata]
metadata: json[:metadata],
errors: [json[:message]]
}
rescue MultiJson::ParseError => exception
{ error: exception.cause }
{ errors: [exception.cause] }
end
end

Expand All @@ -70,19 +71,19 @@ class User < Spyke::Base
has_many :posts
end

user = User.find(3)
user = User.find(3)
# => GET http://api.com/users/3

user.posts
user.posts
# => find embedded in returned JSON or GET http://api.com/users/3/posts

user.update_attributes(name: 'Alice')
user.update_attributes(name: 'Alice')
# => PUT http://api.com/users/3 - { user: { name: 'Alice' } }

user.destroy
user.destroy
# => DELETE http://api.com/users/3

User.create(name: 'Bob')
User.create(name: 'Bob')
# => POST http://api.com/users - { user: { name: 'Bob' } }
```

Expand Down
22 changes: 16 additions & 6 deletions lib/spyke/http.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,17 +62,27 @@ def connection
METHODS.each do |method|
define_method(method) do |action = nil, params = {}|
params = action if action.is_a?(Hash)
path = case action
when Symbol then uri.join(action)
when String then Path.new(action, attributes)
else uri
end
self.attributes = self.class.send("#{method}_raw", path, params).data
path = resolve_path_from_action(action)

result = self.class.send("#{method}_raw", path, params)

result.errors.each { |error| errors.add(:base, error) }
self.attributes = result.data
end
end

def uri
Path.new(@uri_template, attributes)
end

private

def resolve_path_from_action(action)
case action
when Symbol then uri.join(action)
when String then Path.new(action, attributes)
else uri
end
end
end
end
2 changes: 1 addition & 1 deletion lib/spyke/result.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def metadata
end

def errors
body[:errors]
body[:errors] || []
end
end
end
2 changes: 1 addition & 1 deletion lib/spyke/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Spyke
VERSION = '1.2.1'
VERSION = '1.3.0'
end
10 changes: 10 additions & 0 deletions test/orm_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,16 @@ def test_create
assert_requested endpoint
end

def test_create_with_server_failure
endpoint = stub_request(:put, 'http://sushi.com/recipes/1').to_return_json(id: 'write_error:400', message: 'Unable to save recipe')

recipe = Recipe.create(id: 1, title: 'Sushi')

assert_equal 'Sushi', recipe.title
assert_equal ['Unable to save recipe'], recipe.errors[:base]
assert_requested endpoint
end

def test_find_using_custom_uri_template
endpoint = stub_request(:get, 'http://sushi.com/images/photos/1').to_return_json(result: { id: 1 })
Photo.find(1)
Expand Down
5 changes: 3 additions & 2 deletions test/support/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ def parse(body)
json = MultiJson.load(body, symbolize_keys: true)
{
data: json[:result],
metadata: json[:metadata]
metadata: json[:metadata],
errors: [json[:message]]
}
rescue MultiJson::ParseError => exception
{ error: exception.cause }
{ errors: [exception.cause] }
end
end

Expand Down

0 comments on commit 9e5667d

Please sign in to comment.