restfolia 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,52 @@
1
+
2
+ # Run this sample from root project:
3
+ # $ ruby samples/http_behaviour.rb
4
+
5
+ require "rubygems"
6
+ $LOAD_PATH << "lib"
7
+ require "restfolia"
8
+ require "ostruct"
9
+
10
+ Restfolia::HTTP.behaviours do
11
+
12
+ on(200) do |http_response|
13
+ content_type = (http_response["content-type"] =~ /application\/json/)
14
+ if !content_type
15
+ msg_error = "Response \"content-type\" header should be \"application/json\""
16
+ raise Restfolia::ResponseError.new(msg_error, caller, http_response)
17
+ end
18
+
19
+ http_body = http_response.body.to_s
20
+ if !http_body.empty?
21
+ json_parsed = helpers.parse_json(http_response)
22
+ Restfolia.create_resource(json_parsed)
23
+ elsif (location = http_response["location"])
24
+ helpers.follow_url(location)
25
+ else
26
+ nil
27
+ end
28
+ end
29
+
30
+ # 3xx range
31
+ on(300...400) do
32
+ raise "3xx error"
33
+ end
34
+
35
+ #on([404, 402]) do
36
+ # custom_helper
37
+ #end
38
+
39
+ #helpers do
40
+ # def custom_helper
41
+ # 'lixo'
42
+ # end
43
+ #end
44
+
45
+ end
46
+
47
+ SERVICE_URL = "http://localhost:9292/recursos/busca"
48
+ resource = Restfolia.at(SERVICE_URL).get
49
+ puts resource.inspect
50
+
51
+ Restfolia.at("http://google.com").get
52
+ # => "3xx error"
@@ -0,0 +1,25 @@
1
+
2
+ # Run this sample from root project:
3
+ # $ ruby samples/using_custom_factory.rb
4
+
5
+ require "rubygems"
6
+ $LOAD_PATH << "lib"
7
+ require "restfolia"
8
+ require "ostruct"
9
+
10
+ SERVICE_URL = "http://localhost:9292/recursos/busca"
11
+
12
+ resource = Restfolia.at(SERVICE_URL).get
13
+ puts resource.inspect # => #<Restfolia::Resource ...>
14
+
15
+ # Here you have the "pure" json from response body.
16
+ # You can do anything.
17
+ module Restfolia
18
+ def self.create_resource(json)
19
+ OpenStruct.new(json)
20
+ end
21
+ end
22
+ resource = Restfolia.at(SERVICE_URL).get
23
+ puts resource.inspect # => #<OpenStruct ...>
24
+
25
+ puts "Done!"
@@ -0,0 +1,25 @@
1
+
2
+ # Run this sample from root project:
3
+ # $ ruby samples/using_custom_resource.rb
4
+
5
+ require "rubygems"
6
+ $LOAD_PATH << "lib"
7
+ require "restfolia"
8
+ require "ostruct"
9
+
10
+ SERVICE_URL = "http://localhost:9292/recursos/busca"
11
+
12
+ resource = Restfolia.at(SERVICE_URL).get
13
+ puts resource.inspect # => #<Restfolia::Resource ...>
14
+
15
+ # Here you have the advantage to use a custom resource
16
+ # and the same time you have the recursive lookup at hash
17
+ class Restfolia::ResourceCreator
18
+ def resource_class
19
+ OpenStruct
20
+ end
21
+ end
22
+ resource = Restfolia.at(SERVICE_URL).get
23
+ puts resource.inspect # => #<OpenStruct ...>
24
+
25
+ puts "Done!"
@@ -0,0 +1,123 @@
1
+ require "test_helper"
2
+
3
+ describe Restfolia::EntryPoint do
4
+
5
+ include Restfolia::Test::JsonSamples
6
+ include Restfolia::Test::StubHelpers
7
+
8
+ subject { Restfolia::EntryPoint.new(Restfolia::Test::FAKE_URL) }
9
+
10
+ describe "#get" do
11
+
12
+ it "create Resource for valid request" do
13
+ stub_get_request(:status => 200, :body => valid_json)
14
+
15
+ resource = subject.get
16
+ resource.must_be_instance_of(Restfolia::Resource)
17
+ end
18
+
19
+ it "should accept String params" do
20
+ stub_api = stub_get_request(:status => 200,
21
+ :body => valid_json,
22
+ :query => {:q => "stringtest",
23
+ :p1 => "test"})
24
+ subject.get("q=stringtest&p1=test")
25
+ end
26
+
27
+ it "should accept Hash params" do
28
+ stub_api = stub_get_request(:status => 200,
29
+ :body => valid_json,
30
+ :query => {:q => "hashtest"})
31
+ subject.get(:q => "hashtest")
32
+ end
33
+
34
+ it "should send Custom HTTP header" do
35
+ stub_api = stub_get_request(:status => 200,
36
+ :body => valid_json,
37
+ :query => {:q => "hashtest"},
38
+ :with_headers => {"X-Custom" => "test"})
39
+
40
+ subject.headers["X-Custom"] = "test"
41
+ subject.get(:q => "hashtest")
42
+ end
43
+
44
+ end
45
+
46
+ describe "#post" do
47
+
48
+ it "can post Hash params" do
49
+ headers = {"Content-Type" => "application/json"}
50
+ stub_method_request(:post,
51
+ :status => 201,
52
+ :body => "{\"attr_test\":\"test\"}",
53
+ :headers => headers)
54
+
55
+ subject.post(:attr_test => "test")
56
+ end
57
+
58
+ it "should send Custom HTTP header" do
59
+ headers = {"Content-Type" => "application/json"}
60
+ stub_method_request(:post,
61
+ :status => 201,
62
+ :body => "{\"attr_test\":\"test\"}",
63
+ :headers => headers,
64
+ :with_headers => {"X-Custom" => "test"})
65
+
66
+ subject.headers["X-Custom"] = "test"
67
+ subject.post(:attr_test => "test")
68
+ end
69
+
70
+ end
71
+
72
+ describe "#put" do
73
+
74
+ it "can put Hash params" do
75
+ headers = {"Content-Type" => "application/json"}
76
+ stub_method_request(:put,
77
+ :status => 200,
78
+ :body => "{\"attr_test\":\"upd test\"}",
79
+ :headers => headers)
80
+
81
+ subject.put(:attr_test => "upd test")
82
+ end
83
+
84
+ it "should send Custom HTTP header" do
85
+ headers = {"Content-Type" => "application/json"}
86
+ stub_method_request(:put,
87
+ :status => 200,
88
+ :body => "{\"attr_test\":\"upd test\"}",
89
+ :headers => headers,
90
+ :with_headers => {"X-Custom" => "test"})
91
+
92
+ subject.headers["X-Custom"] = "test"
93
+ subject.put(:attr_test => "upd test")
94
+ end
95
+ end
96
+
97
+ describe "#delete" do
98
+
99
+ it "can send DELETE request" do
100
+ headers = {"Content-Type" => "application/json"}
101
+ stub_method_request(:delete,
102
+ :status => 204,
103
+ :body => nil,
104
+ :headers => headers)
105
+
106
+ subject.delete
107
+ end
108
+
109
+ it "should send Custom HTTP header" do
110
+ headers = {"Content-Type" => "application/json"}
111
+ stub_method_request(:delete,
112
+ :status => 204,
113
+ :body => nil,
114
+ :headers => headers,
115
+ :with_headers => {"X-Custom" => "test"})
116
+
117
+ subject.headers["X-Custom"] = "test"
118
+ subject.delete
119
+ end
120
+
121
+ end
122
+
123
+ end
@@ -0,0 +1,86 @@
1
+ require "test_helper"
2
+
3
+ describe Restfolia::HTTP::Behaviour do
4
+
5
+ describe "Store" do
6
+ subject { Restfolia::HTTP::Behaviour::Store.new }
7
+
8
+ describe "#execute" do
9
+ before do
10
+ @http_mock = MiniTest::Mock.new
11
+ end
12
+
13
+ it "should execute block for Integer code match" do
14
+ @http_mock.expect(:code, "200")
15
+ subject.on(200) { 'test' }
16
+
17
+ return_value = subject.execute(@http_mock)
18
+ return_value.must_equal('test')
19
+ end
20
+
21
+ it "should execute block for Range code match" do
22
+ @http_mock.expect(:code, "300")
23
+ subject.on(300...400) { 'test' }
24
+
25
+ return_value = subject.execute(@http_mock)
26
+ return_value.must_equal('test')
27
+ end
28
+
29
+ it "should execute block for Array code match" do
30
+ @http_mock.expect(:code, "404")
31
+ subject.on([404, 403]) { 'test' }
32
+
33
+ return_value = subject.execute(@http_mock)
34
+ return_value.must_equal('test')
35
+ end
36
+
37
+ it "should call #default_behaviour for non match code" do
38
+ @http_mock.expect(:code, "666")
39
+ lambda { subject.execute(@http_mock) }.
40
+ must_raise(Restfolia::ResponseError)
41
+ end
42
+ end
43
+
44
+ describe "#behaviours" do
45
+ it "should be possible to call 'on' method" do
46
+ subject.behaviours do
47
+ on(200) { 'test' }
48
+ end
49
+ end
50
+ end
51
+
52
+ it "#helpers" do
53
+ subject.must_respond_to(:helpers)
54
+ subject.helpers.must_be_instance_of(Restfolia::HTTP::Behaviour::Helpers)
55
+ end
56
+
57
+ end
58
+
59
+ describe "Helpers" do
60
+
61
+ subject { Restfolia::HTTP::Behaviour::Helpers.new }
62
+
63
+ before do
64
+ @http_mock = MiniTest::Mock.new
65
+ end
66
+
67
+ describe "#parse_json" do
68
+ it "should returns parsed json" do
69
+ json_sample = '{"test":"ok"}'
70
+ @http_mock.expect(:body, json_sample)
71
+ json = subject.parse_json(@http_mock)
72
+
73
+ json.must_be_instance_of(Hash)
74
+ json[:test].must_equal("ok")
75
+ end
76
+ it "should raise error for invalid body" do
77
+ @http_mock.expect(:body, "<html><body>error</body></html>")
78
+ lambda do
79
+ subject.parse_json(@http_mock)
80
+ end.must_raise(Restfolia::ResponseError)
81
+ end
82
+ end
83
+
84
+ end
85
+
86
+ end
@@ -0,0 +1,45 @@
1
+ require "test_helper"
2
+
3
+ describe Restfolia::HTTP::Configuration do
4
+
5
+ class TestConfiguration; include Restfolia::HTTP::Configuration; end
6
+
7
+ before do
8
+ @object = TestConfiguration.new
9
+ @expected_headers = {"Content-Type" => "application/json"}
10
+ end
11
+
12
+ it "#headers" do
13
+ @object.headers["Content-Type"] = "application/json"
14
+ @object.headers.must_equal(@expected_headers)
15
+ end
16
+
17
+ it "#with_headers" do
18
+ return_value = @object.with_headers("Content-Type" => "application/json")
19
+
20
+ @object.must_be_same_as(return_value)
21
+ @object.headers.must_equal(@expected_headers)
22
+ end
23
+
24
+ it "#as" do
25
+ expected_headers = {"Content-Type" => "application/json",
26
+ "Accept" => "application/json"}
27
+ return_value = @object.as("application/json")
28
+
29
+ @object.must_be_same_as(return_value)
30
+ @object.headers.must_equal(expected_headers)
31
+ end
32
+
33
+ it "#cookies" do
34
+ @object.must_respond_to(:cookies)
35
+ end
36
+
37
+ it "#set_cookies" do
38
+ expected_cookies = "key=value;"
39
+ return_value = @object.set_cookies(expected_cookies)
40
+
41
+ @object.must_be_same_as(return_value)
42
+ @object.cookies.must_equal(expected_cookies)
43
+ end
44
+
45
+ end
@@ -0,0 +1,54 @@
1
+ require "test_helper"
2
+ require "ostruct"
3
+
4
+ describe Restfolia::ResourceCreator do
5
+
6
+ let(:subject) do
7
+ creator = Restfolia::ResourceCreator.new
8
+ def creator.resource_class
9
+ OpenStruct
10
+ end
11
+ creator
12
+ end
13
+
14
+ describe ".create" do
15
+
16
+ it "accept only hash object as parameter" do
17
+ lambda { subject.create(nil) }.must_raise(ArgumentError)
18
+ end
19
+
20
+ it "should create Resource for simple hash" do
21
+ resource = subject.create(:attr_test => "test")
22
+ resource.must_be_instance_of(OpenStruct)
23
+ end
24
+
25
+ it "transforms nested hash in Resource" do
26
+ resource = subject.create(:attr_test => {:nested => "nested"})
27
+ resource.attr_test.must_be_instance_of(OpenStruct)
28
+ resource.attr_test.nested.must_equal("nested")
29
+ end
30
+
31
+ it "transforms nested hash from Arrays in Resource" do
32
+ resource = subject.create(:attr_test => [{:nested_array => "object"}],
33
+ :attr_test2 => ["not object"])
34
+
35
+ resource.attr_test.must_be_instance_of(Array)
36
+ resource.attr_test[0].must_be_instance_of(OpenStruct)
37
+ resource.attr_test[0].nested_array.must_equal("object")
38
+
39
+ resource.attr_test2[0].must_be_instance_of(String)
40
+ resource.attr_test2[0].must_equal("not object")
41
+ end
42
+
43
+ it "transforms nested hash from nested Array from Array in Resource" do
44
+ resource = subject.create(:attr_test => [[{:nested => "nested2"}]])
45
+
46
+ resource.attr_test.must_be_instance_of(Array)
47
+ resource.attr_test[0].must_be_instance_of(Array)
48
+ resource.attr_test[0][0].must_be_instance_of(OpenStruct)
49
+ resource.attr_test[0][0].nested.must_equal("nested2")
50
+ end
51
+
52
+ end
53
+
54
+ end
@@ -0,0 +1,89 @@
1
+ require "test_helper"
2
+
3
+ describe Restfolia::Resource do
4
+
5
+ let(:subject) { Restfolia::Resource }
6
+
7
+ describe "#initialize" do
8
+
9
+ it "accept only hash object as parameter" do
10
+ lambda { subject.new(nil) }.must_raise(ArgumentError)
11
+ end
12
+
13
+ end
14
+
15
+ describe "#initialize - look_for_resource" do
16
+
17
+ it "transforms hash keys in attributes" do
18
+ resource = subject.new(:attr_test => "test")
19
+
20
+ resource.must_respond_to(:attr_test)
21
+ resource.attr_test.must_equal("test")
22
+ end
23
+
24
+ end
25
+
26
+ describe "#links" do
27
+
28
+ let(:hash_link) do
29
+ {:href => "http://service.com", :rel => "rel", :type => "type"}
30
+ end
31
+
32
+ let(:array_links) do
33
+ link2 = hash_link
34
+ link2[:rel] = "rel2"
35
+
36
+ [hash_link, link2]
37
+ end
38
+
39
+ it "returns empty Array for no links" do
40
+ resource = subject.new(:attr_test => "test")
41
+ resource.links.must_be_empty
42
+ end
43
+
44
+ it "returns Array for one link" do
45
+ resource = subject.new(:links => hash_link)
46
+ resource.links.must_be_instance_of(Array)
47
+ resource.links[0].must_be_instance_of(Restfolia::EntryPoint)
48
+ end
49
+
50
+ it "returns Array for many links" do
51
+ resource = subject.new(:links => array_links)
52
+ resource.links.must_be_instance_of(Array)
53
+ resource.links[0].must_be_instance_of(Restfolia::EntryPoint)
54
+ resource.links.size.must_equal(2)
55
+ end
56
+
57
+ it "understand 'link' node too" do
58
+ resource = subject.new(:link => array_links)
59
+ resource.links.must_be_instance_of(Array)
60
+ resource.links[0].must_be_instance_of(Restfolia::EntryPoint)
61
+ end
62
+
63
+ it "raises Error for invalid link" do
64
+ resource = subject.new(:links => {:invalid => "invalid"})
65
+ lambda { resource.links }.must_raise(RuntimeError)
66
+ end
67
+
68
+ describe "rel search" do
69
+
70
+ it "returns nil for rel not found (without link)" do
71
+ resource = subject.new(:attr_test => "test")
72
+ resource.links("notexist").must_be_nil
73
+ end
74
+
75
+ it "returns nil for rel not found (with links)" do
76
+ resource = subject.new(:links => array_links)
77
+ resource.links("notexist").must_be_nil
78
+ end
79
+
80
+ it "returns EntryPoint for rel found" do
81
+ resource = subject.new(:links => array_links)
82
+ resource.links("rel2").must_be_instance_of(Restfolia::EntryPoint)
83
+ end
84
+
85
+ end
86
+
87
+ end
88
+
89
+ end
@@ -0,0 +1,10 @@
1
+ require "test_helper"
2
+
3
+ describe Restfolia do
4
+
5
+ it ".url" do
6
+ ep = Restfolia.at("http://fakeurl.com")
7
+ ep.must_be_instance_of(Restfolia::EntryPoint)
8
+ end
9
+
10
+ end
@@ -0,0 +1,41 @@
1
+ module Restfolia::Test
2
+
3
+ FAKE_URL = "http://fakeurl.com"
4
+ FAKE_LOCATION_URL = "http://fakeurl.com/resource/666"
5
+
6
+ module JsonSamples
7
+
8
+
9
+ def valid_json
10
+ json_body = <<json_body
11
+ { "itens_por_pagina" : 10,
12
+ "links" : { "href" : "http://localhost:9292/recursos/busca",
13
+ "rel" : "self",
14
+ "type" : "application/json"
15
+ },
16
+ "paginal_atual" : 1,
17
+ "paginas_totais" : 1,
18
+ "query" : "",
19
+ "resultado" : [ { "id" : 1,
20
+ "links" : [ { "href" : "http://localhost:9292/recursos/id/1",
21
+ "rel" : "recurso",
22
+ "type" : "application/json"
23
+ } ],
24
+ "name" : "Test1"
25
+ },
26
+ { "id" : 2,
27
+ "links" : [ { "href" : "http://localhost:9292/recursos/id/2",
28
+ "rel" : "recurso",
29
+ "type" : "application/json"
30
+ } ],
31
+ "name" : "Test2"
32
+ }
33
+ ],
34
+ "total_resultado" : 100
35
+ }
36
+ json_body
37
+ end
38
+
39
+ end
40
+
41
+ end
@@ -0,0 +1,36 @@
1
+ module Restfolia::Test
2
+ module StubHelpers
3
+
4
+ def stub_get_request(args = {})
5
+ status = args[:status]
6
+ body = args[:body]
7
+ query = args[:query]
8
+ headers = args[:headers] || {}
9
+ headers["Content-Type"] = "application/json"
10
+ with_headers = args[:with_headers]
11
+
12
+ stub = stub_request(:get, Restfolia::Test::FAKE_URL)
13
+ stub.with(:query => query) unless query.nil?
14
+ stub.with(:headers => with_headers) unless with_headers.nil?
15
+
16
+ stub.to_return(:body => body,
17
+ :status => status,
18
+ :headers => headers)
19
+ end
20
+
21
+ def stub_method_request(method, args = {})
22
+ status = args[:status]
23
+ body = args[:body]
24
+ headers = args[:headers]
25
+ with_headers = args[:with_headers] || {'Accept'=>'*/*'}
26
+
27
+ stub_request(method, Restfolia::Test::FAKE_URL).
28
+ with(:body => body,
29
+ :headers => with_headers).
30
+ to_return(:status => status,
31
+ :body => "",
32
+ :headers => headers)
33
+ end
34
+
35
+ end
36
+ end
@@ -0,0 +1,13 @@
1
+ require "restfolia"
2
+
3
+ require "minitest/autorun"
4
+ require "minitest/reporters"
5
+ require "webmock/minitest"
6
+
7
+ require "support/json_samples"
8
+ require "support/stub_helpers"
9
+
10
+ WebMock.disable_net_connect!
11
+
12
+ MiniTest::Unit.runner = MiniTest::SuiteRunner.new
13
+ MiniTest::Unit.runner.reporters << MiniTest::Reporters::SpecReporter.new