remote_api 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/test/base_test.rb ADDED
@@ -0,0 +1,58 @@
1
+ require File.dirname(__FILE__) + '/test_helper.rb'
2
+
3
+ FakeWeb.register_uri(
4
+ 'http://test.com/base',
5
+ :string => '<server_response>The server recieved your message</server_response>'
6
+ )
7
+ FakeWeb.register_uri(
8
+ 'http://test.com/base/error',
9
+ :string => '<server_response>An error occurred!</server_response>'
10
+ )
11
+
12
+ class RemoteApiTest < Test::Unit::TestCase
13
+
14
+ def setup
15
+ BaseApi.url = 'http://test.com/base'
16
+ end
17
+
18
+ def test_initialize_should_set_hash_keys_as_instance_variables
19
+ result = BaseApi.new(:foo => 'bar', :baz => 'taz', :message => 'Hello')
20
+
21
+ assert_equal('bar', result.instance_eval { @foo })
22
+ assert_equal('taz', result.instance_eval { @baz })
23
+ assert_equal('Hello', result.instance_eval { @message })
24
+ end
25
+
26
+ def test_initialize_should_perform_api_call
27
+ result = BaseApi.new(:message => 'Hello World!')
28
+ assert_equal 'The server recieved your message', result.return_message
29
+ end
30
+
31
+ def test_assert_success_should_raise_exception
32
+ BaseApi.url = 'http://test.com/base/error'
33
+ assert_raise RemoteAPI::ResponseFailure do
34
+ BaseApi.new(:message => 'foo')
35
+ end
36
+ end
37
+ end
38
+
39
+
40
+ class BaseApi < RemoteAPI
41
+ attr_reader :return_message
42
+
43
+ def request
44
+ x = Builder::XmlMarkup.new
45
+ x.my_request @message
46
+ end
47
+
48
+ def assert_success
49
+ if @response =~ %r{<server_response>(.*error.*)</server_response>}
50
+ raise ResponseFailure, $1
51
+ end
52
+ end
53
+
54
+ def process
55
+ @return_message = @response.gsub(%r{</?server_response>}, '')
56
+ end
57
+
58
+ end
@@ -0,0 +1,7 @@
1
+ require 'test/unit'
2
+
3
+ require 'rubygems'
4
+ require 'fake_web'
5
+ require 'builder'
6
+
7
+ require File.dirname(__FILE__) + '/../lib/remote_api'
@@ -0,0 +1,77 @@
1
+ require File.dirname(__FILE__) + '/test_helper.rb'
2
+
3
+ class XmlResponseTest < Test::Unit::TestCase
4
+
5
+ XML = <<XML
6
+ <response>
7
+ <title>Test Title</title>
8
+ <items>
9
+ <item>
10
+ <name>Bob</name>
11
+ <id>1</id>
12
+ </item>
13
+ <item>
14
+ <name>Joe</name>
15
+ <id>2</id>
16
+ </item>
17
+ <item>
18
+ <name>Walter</name>
19
+ <id>3</id>
20
+ </item>
21
+ </items>
22
+ </response>
23
+ XML
24
+
25
+ def setup
26
+ @xml = RemoteAPI::XML::Response.new(XML)
27
+ end
28
+
29
+ def test_brace_accessor
30
+ assert_equal('Test Title', @xml['//title'])
31
+ end
32
+
33
+ def test_bad_xpath_should_return_nil
34
+ assert_nil @xml['/foo/bar/baz']
35
+ end
36
+
37
+ def test_each_on_bad_xpath_should_not_run_block
38
+ count = 0
39
+ @xml.each('/foo/bar/baz') { |node| count += 1 }
40
+ assert_equal(0, count)
41
+ end
42
+
43
+ def test_each
44
+ expected_names = %w( Bob Joe Walter )
45
+ expected_ids = %w( 1 2 3 )
46
+ count = 0
47
+
48
+ @xml.each '//items/item' do |node|
49
+ count += 1
50
+ assert_equal(expected_names.shift, node['name'])
51
+ assert_equal(expected_ids.shift, node['id'])
52
+ end
53
+
54
+ assert_equal(3, count)
55
+ end
56
+
57
+ def test_each_without_block_should_raise_exception
58
+ assert_raise(ArgumentError) do
59
+ @xml.each('//title')
60
+ end
61
+ end
62
+
63
+ def test_to_formatted_s
64
+ input = '<a><b><c>foo</c></b></a>'
65
+ expected = <<XML
66
+ <a>
67
+ <b>
68
+ <c>foo</c>
69
+ </b>
70
+ </a>
71
+ XML
72
+ xml_response = RemoteAPI::XML::Response.new(input)
73
+
74
+ assert_equal(expected.chomp, xml_response.to_formatted_s)
75
+ end
76
+
77
+ end
data/test/xml_test.rb ADDED
@@ -0,0 +1,63 @@
1
+ require File.dirname(__FILE__) + '/test_helper.rb'
2
+
3
+ FakeWeb.register_uri(
4
+ 'http://test.com/xml',
5
+ :string => '<foo><server_response>The server recieved your message</server_response></foo>'
6
+ )
7
+ FakeWeb.register_uri(
8
+ 'http://test.com/xml/error',
9
+ :string => '<server_response><error>Big fat error</error></server_response>'
10
+ )
11
+
12
+ class RemoteApiXMLTest < Test::Unit::TestCase
13
+
14
+ def setup
15
+ XmlApi.url = 'http://test.com/xml'
16
+ end
17
+
18
+ def test_response_should_be_xml
19
+ result = XmlApi.new(:message => 'foo')
20
+ assert_equal('The server recieved your message', result.return_message)
21
+ end
22
+
23
+ def test_format_response_should_produce_pretty_xml
24
+ expected = <<XML
25
+ <foo>
26
+ <server_response>The server recieved your message</server_response>
27
+ </foo>
28
+ XML
29
+
30
+ result = XmlApi.new(:message => 'foo')
31
+ data = result.send(:format_response, '<foo><server_response>The server recieved your message</server_response></foo>')
32
+ assert_equal(expected.chomp, data)
33
+ end
34
+
35
+ def test_assert_success_should_raise_exceptions
36
+ XmlApi.url = 'http://test.com/xml/error'
37
+ assert_raise RemoteAPI::ResponseFailure do
38
+ XmlApi.new(:message => 'foo')
39
+ end
40
+ end
41
+
42
+ end
43
+
44
+
45
+ class XmlApi < RemoteAPI::XML
46
+ attr_reader :return_message
47
+
48
+ def request
49
+ x = Builder::XmlMarkup.new
50
+ x.my_request @message
51
+ end
52
+
53
+ def assert_success
54
+ if error = @response['/server_response/error']
55
+ raise ResponseFailure, error
56
+ end
57
+ end
58
+
59
+ def process
60
+ @return_message = @response['/foo/server_response']
61
+ end
62
+
63
+ end
metadata ADDED
@@ -0,0 +1,70 @@
1
+ --- !ruby/object:Gem::Specification
2
+ rubygems_version: 0.9.0
3
+ specification_version: 1
4
+ name: remote_api
5
+ version: !ruby/object:Gem::Version
6
+ version: 0.1.0
7
+ date: 2007-01-15 00:00:00 -08:00
8
+ summary: Provides a basic framework for easily creating classes that access remote APIs.
9
+ require_paths:
10
+ - lib
11
+ email: rubyonrails@beautifulpixel.com
12
+ homepage: http://remote-api.rubyforge.org
13
+ rubyforge_project: remote-api
14
+ description: Provides a basic framework for easily creating classes that access remote APIs.
15
+ autorequire:
16
+ default_executable:
17
+ bindir: bin
18
+ has_rdoc: true
19
+ required_ruby_version: !ruby/object:Gem::Version::Requirement
20
+ requirements:
21
+ - - ">"
22
+ - !ruby/object:Gem::Version
23
+ version: 0.0.0
24
+ version:
25
+ platform: ruby
26
+ signing_key:
27
+ cert_chain:
28
+ post_install_message:
29
+ authors:
30
+ - Alex Wayne
31
+ files:
32
+ - History.txt
33
+ - Manifest.txt
34
+ - README.txt
35
+ - Rakefile
36
+ - setup.rb
37
+ - lib/dsl_accessor.rb
38
+ - lib/remote_api.rb
39
+ - lib/remote_api/base.rb
40
+ - lib/remote_api/xml.rb
41
+ - lib/remote_api/xml_response.rb
42
+ - lib/remote_api/version.rb
43
+ - test/test_helper.rb
44
+ - test/base_test.rb
45
+ - test/xml_test.rb
46
+ - test/xml_response_test.rb
47
+ test_files:
48
+ - test/base_test.rb
49
+ - test/xml_response_test.rb
50
+ - test/xml_test.rb
51
+ rdoc_options: []
52
+
53
+ extra_rdoc_files: []
54
+
55
+ executables: []
56
+
57
+ extensions: []
58
+
59
+ requirements: []
60
+
61
+ dependencies:
62
+ - !ruby/object:Gem::Dependency
63
+ name: activesupport
64
+ version_requirement:
65
+ version_requirements: !ruby/object:Gem::Version::Requirement
66
+ requirements:
67
+ - - ">"
68
+ - !ruby/object:Gem::Version
69
+ version: 0.0.0
70
+ version: