Skip to content

Commit

Permalink
Isolated access to helpers in Behaviours block
Browse files Browse the repository at this point in the history
  • Loading branch information
rogerleite committed May 6, 2012
1 parent 703c770 commit bbf9a4f
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 9 deletions.
6 changes: 3 additions & 3 deletions lib/restfolia/http.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module Restfolia

# Public: Store and execute behaviours defined by user. Behaviour is action
# for one or mode HTTP code. See "default behaviours" below.
# for one or more HTTP code. See "default behaviours" below.
#
# Examples
#
Expand All @@ -18,7 +18,7 @@ module Restfolia
#
# http_body = http_response.body.to_s
# if !http_body.empty?
# json_parsed = parse_json(http_response)
# json_parsed = helpers.parse_json(http_response)
# Restfolia.create_resource(json_parsed)
# elsif (location = http_response["location"])
# http_resp = Request.do_request(:get, location)
Expand Down Expand Up @@ -100,7 +100,7 @@ def self.behaviours(&block)

http_body = http_response.body.to_s
if !http_body.empty?
json_parsed = parse_json(http_response)
json_parsed = helpers.parse_json(http_response)
Restfolia.create_resource(json_parsed)
elsif (location = http_response["location"])
http_resp = Request.do_request(:get, location)
Expand Down
10 changes: 5 additions & 5 deletions lib/restfolia/http/behaviour.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,10 @@ def self.store
#
# Restfolia::HTTP.behaviours do
# on(200) do |http_response|
# # parse_json is a helper from
# # Restfolia::HTTP::Behaviour::Helpers
# parse_json(http_response.body)
# helpers.parse_json(http_response.body)
# end
# end
module Helpers
class Helpers

# Internal: Parse response body, checking for errors.
#
Expand All @@ -42,11 +40,13 @@ def parse_json(http_response)
# Public: Responsible to store behaviours. See #behaviours for details.
class Store

include Restfolia::HTTP::Behaviour::Helpers
# Returns Restfolia::HTTP::Behaviour::Helpers instance.
attr_reader :helpers

# Public: Creates a Store.
def initialize
self.clear
@helpers = Helpers.new
end

# Public: clear all defined behaviours.
Expand Down
2 changes: 1 addition & 1 deletion samples/http_behaviour.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

http_body = http_response.body.to_s
if !http_body.empty?
json_parsed = parse_json(http_response)
json_parsed = helpers.parse_json(http_response)
Restfolia.create_resource(json_parsed)
elsif (location = http_response["location"])
http_resp = do_request(:get, location)
Expand Down
32 changes: 32 additions & 0 deletions test/restfolia/http_behaviour_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,38 @@
end
end

it "#helpers" do
subject.must_respond_to(:helpers)
subject.helpers.must_be_instance_of(Restfolia::HTTP::Behaviour::Helpers)
end

end

describe "Helpers" do

subject { Restfolia::HTTP::Behaviour::Helpers.new }

before do
@http_mock = MiniTest::Mock.new
end

describe "#parse_json" do
it "should returns parsed json" do
json_sample = '{"test":"ok"}'
@http_mock.expect(:body, json_sample)
json = subject.parse_json(@http_mock)

json.must_be_instance_of(Hash)
json[:test].must_equal("ok")
end
it "should raise error for invalid body" do
@http_mock.expect(:body, "<html><body>error</body></html>")
lambda do
subject.parse_json(@http_mock)
end.must_raise(Restfolia::ResponseError)
end
end

end

end

0 comments on commit bbf9a4f

Please sign in to comment.