relax 0.1.3 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/lib/relax/service.rb DELETED
@@ -1,45 +0,0 @@
1
- module Relax
2
- class Service
3
- def initialize(values={}, options={})
4
- @values = values
5
- @options = options
6
- end
7
-
8
- def authenticate(*args)
9
- @options[:credentials] = args
10
- self
11
- end
12
-
13
- def proxy(url)
14
- @options[:proxy] = url
15
- self
16
- end
17
-
18
- def include_blank_values(value)
19
- @options[:include_blank_values] = value
20
- self
21
- end
22
-
23
- class << self
24
- include Contextable
25
-
26
- def endpoint(url, options={}, &block)
27
- Endpoint.new(self, url, options, &block)
28
- end
29
-
30
- def register_action(action) # :nodoc:
31
- @actions ||= {}
32
-
33
- unless @actions[action.name]
34
- @actions[action.name] = action.name
35
-
36
- define_method(action.name) do |*args|
37
- action.execute(@values, @options, *args)
38
- end
39
- else
40
- raise ArgumentError.new("Duplicate action '#{action.name}'.")
41
- end
42
- end
43
- end
44
- end
45
- end
@@ -1,18 +0,0 @@
1
- require File.join(File.dirname(__FILE__), '..', 'spec_helper')
2
-
3
- describe Relax::Context do
4
- it "utilizes a custom parser for Class parsers" do
5
- service = CustomParserService.new
6
- service.test.should == 'parsed'
7
- end
8
-
9
- it "allows parameters with aliases" do
10
- service = ParameterAliasService.new
11
- service.test(:api_key => 'secret')[:stat].should == 'ok'
12
- end
13
-
14
- it "allows blank parameters values" do
15
- service = BlankValuesService.new({}, :include_blank_values => true)
16
- service.test[:stat].should == 'ok'
17
- end
18
- end
@@ -1,99 +0,0 @@
1
- require File.join(File.dirname(__FILE__), '..', 'spec_helper')
2
-
3
- describe Relax::Endpoint do
4
- it "provides access to the URL" do
5
- service = Class.new(Relax::Service)
6
- endpoint = service.endpoint("http://api.example.com/") { }
7
- endpoint.url.should == "http://api.example.com/"
8
- end
9
-
10
- it "allows contextual defaults to be set" do
11
- service = Class.new(Relax::Service)
12
- endpoint = service.endpoint("http://api.example.com/") { }
13
- endpoint.should respond_to(:defaults)
14
- end
15
-
16
- describe "actions" do
17
- it "should check for required values for service defaults" do
18
- service = Class.new(Relax::Service) do
19
- defaults { parameter :api_key, :required => true }
20
- endpoint("http://api.example.com/") { action(:fetch) { } }
21
- end
22
-
23
- proc {
24
- service.new.fetch
25
- }.should raise_error(ArgumentError, /missing.*api_key/i)
26
- end
27
-
28
- it "should check for required values for endpoint defaults" do
29
- service = Class.new(Relax::Service) do
30
- endpoint("http://api.example.com/") do
31
- defaults { parameter :operation, :required => true }
32
- action(:fetch) { }
33
- end
34
- end
35
-
36
- proc {
37
- service.new.fetch
38
- }.should raise_error(ArgumentError, /missing.*operation/i)
39
- end
40
-
41
- it "should check for required values for action parameters" do
42
- service = Class.new(Relax::Service) do
43
- endpoint("http://api.example.com/") do
44
- action(:fetch) { parameter :id, :required => true }
45
- end
46
- end
47
-
48
- proc {
49
- service.new.fetch
50
- }.should raise_error(ArgumentError, /missing.*id/i)
51
- end
52
-
53
- it "should create required parameters from tokens in the endpoint URL" do
54
- service = Class.new(Relax::Service) do
55
- endpoint("http://api.example.com/:version/") do
56
- action(:fetch) { parameter :id }
57
- end
58
- end
59
-
60
- proc {
61
- service.new.fetch
62
- }.should raise_error(ArgumentError, /missing.*version/i)
63
- end
64
-
65
- it "should replace parameter tokens in the endpoint URL" do
66
- service = Class.new(Relax::Service) do
67
- endpoint("http://api.example.com/:version/") do
68
- action(:fetch) do
69
- parameter :id
70
- parser(:xml) { attribute :status }
71
- end
72
- end
73
- end
74
-
75
- FakeWeb.register_uri(:get, 'http://api.example.com/v1/', :string => <<-RESPONSE)
76
- <?xml version="1.0" encoding="utf-8" ?>
77
- <response status="ok" />
78
- RESPONSE
79
-
80
- service.new(:version => 'v1').fetch.should == { :status => 'ok' }
81
- end
82
- end
83
-
84
- describe ".action" do
85
- it "is callable from within an Endpoint" do
86
- service = Class.new(Relax::Service)
87
- endpoint = service.endpoint("http://api.example.com/") { }
88
- endpoint.should respond_to(:action)
89
- end
90
-
91
- it "defines an instance method by the same name on the Service" do
92
- service = Class.new(Relax::Service)
93
- service.new.should_not respond_to(:fetch)
94
-
95
- service.endpoint("http://api.example.com/") { action :fetch }
96
- service.new.should respond_to(:fetch)
97
- end
98
- end
99
- end
@@ -1,63 +0,0 @@
1
- require File.join(File.dirname(__FILE__), '..', 'spec_helper')
2
-
3
- describe "an example service's" do
4
- describe "get_photos action" do
5
- it "includes :get_photos" do
6
- Flickr.new.should respond_to(:get_photos)
7
- end
8
-
9
- it "requires an API key" do
10
- proc {
11
- Flickr.new.get_photos
12
- }.should raise_error(ArgumentError, /missing.*api_key/i)
13
- end
14
-
15
- it "requires a user ID" do
16
- proc {
17
- Flickr.new(:api_key => 'secret').get_photos
18
- }.should raise_error(ArgumentError, /missing.*user_id/i)
19
- end
20
-
21
- it "parses the response" do
22
- flickr = Flickr.new(:api_key => 'secret')
23
- flickr.get_photos(:user_id => '59532755@N00', :per_page => 3).should == {
24
- :status => 'ok',
25
- :photos => {
26
- :total => '7830',
27
- :photo => [
28
- { :ispublic => '1', :isfriend => '0', :owner => '59532755@N00', :isfamily => '0', :secret => '2ebe0307e3', :server => '3562', :farm => '4', :id => '3508500178', :title => 'Rich Kilmer'},
29
- {:ispublic => '1', :isfriend => '0', :owner => '59532755@N00', :isfamily => '0', :secret => '10b217377b', :server => '3593', :farm => '4', :id => '3508500140', :title => 'Women In Rails'},
30
- {:ispublic => '1', :isfriend => '0', :owner => '59532755@N00', :isfamily => '0', :secret => '83bc8fbf71', :server => '3620', :farm => '4', :id => '3507688713', :title => 'Obie Fernandez'}
31
- ],
32
- :per_page => '3',
33
- :pages => '2610',
34
- :page => '1'
35
- }
36
- }
37
- end
38
- end
39
-
40
- describe "get_user_by_username action" do
41
- it "includes :get_user_by_username" do
42
- Flickr.new.should respond_to(:get_user_by_username)
43
- end
44
-
45
- it "requires an API key" do
46
- proc {
47
- Flickr.new.get_user_by_username
48
- }.should raise_error(ArgumentError, /missing.*api_key/i)
49
- end
50
-
51
- it "parses the response" do
52
- flickr = Flickr.new(:api_key => 'secret')
53
- flickr.get_user_by_username(:username => 'duncandavidson').should == {
54
- :user => {
55
- :username => 'duncandavidson',
56
- :nsid => '59532755@N00',
57
- :id => '59532755@N00'
58
- },
59
- :status => 'ok'
60
- }
61
- end
62
- end
63
- end
@@ -1,32 +0,0 @@
1
- require File.join(File.dirname(__FILE__), '..', 'spec_helper')
2
-
3
- describe Relax::Service do
4
- it "allows contextual defaults to be set" do
5
- Relax::Service.should respond_to(:defaults)
6
- end
7
-
8
- describe "#authenticate" do
9
- it "is callable from within a Service" do
10
- Relax::Service.new.should respond_to(:authenticate)
11
- end
12
-
13
- it "returns the service" do
14
- service = Relax::Service.new
15
- service.authenticate('username', 'password').should == service
16
- end
17
- end
18
-
19
- describe ".endpoint" do
20
- it "is callable from within a Service" do
21
- Relax::Service.should respond_to(:endpoint)
22
- end
23
-
24
- it "creates a new Endpoint" do
25
- Relax::Endpoint.should_receive(:new)
26
-
27
- class Service < Relax::Service
28
- endpoint "http://api.example.com/"
29
- end
30
- end
31
- end
32
- end
@@ -1,19 +0,0 @@
1
- class BlankValuesService < Relax::Service
2
- endpoint 'http://example.com/' do
3
- action :test do
4
- parameter :one
5
- parameter :two
6
- parameter :three
7
-
8
- parser :response do
9
- attribute :stat
10
- end
11
- end
12
- end
13
- end
14
-
15
- FakeWeb.register_uri(:get, 'http://example.com/?one=&two=&three=', :string => <<-RESPONSE)
16
- <?xml version="1.0" encoding="utf-8" ?>
17
- <response stat="ok">
18
- </response>
19
- RESPONSE
@@ -1,24 +0,0 @@
1
- class TestCustomParser
2
- def initialize(options = {}, &block)
3
- end
4
-
5
- def parse(input)
6
- 'parsed'
7
- end
8
- end
9
-
10
- class CustomParserService < Relax::Service
11
- endpoint "http://test.local/rest" do
12
- action :test do
13
- parser TestCustomParser do
14
- element :status, :attribute => :stat
15
- end
16
- end
17
- end
18
- end
19
-
20
- FakeWeb.register_uri(:get, 'http://test.local/rest', :string => <<-RESPONSE)
21
- <?xml version="1.0" encoding="utf-8" ?>
22
- <test stat="ok">
23
- </test>
24
- RESPONSE
@@ -1,78 +0,0 @@
1
- class Flickr < Relax::Service
2
- defaults do
3
- parameter :api_key, :required => true
4
- end
5
-
6
- endpoint "http://api.flickr.com/services/rest" do
7
- defaults do
8
- parameter :method, :required => true
9
- end
10
-
11
- action :get_photos do
12
- set :method, "flickr.people.getPublicPhotos"
13
- parameter :user_id, :required => true
14
- parameter :safe_search
15
- parameter :extras
16
- parameter :per_page
17
- parameter :page
18
-
19
- parser :rsp do
20
- element :status, :attribute => :stat
21
-
22
- element :photos do
23
- element :page, :attribute => true
24
- element :pages, :attribute => true
25
- element :per_page, :attribute => :perpage
26
- element :total, :attribute => true
27
-
28
- elements :photo do
29
- element :id, :attribute => true
30
- element :owner, :attribute => true
31
- element :secret, :attribute => true
32
- element :server, :attribute => true
33
- element :farm, :attribute => true
34
- element :title, :attribute => true
35
- element :ispublic, :attribute => true
36
- element :isfriend, :attribute => true
37
- element :isfamily, :attribute => true
38
- end
39
- end
40
- end
41
- end
42
-
43
- action :get_user_by_username do
44
- set :method, "flickr.people.findByUsername"
45
- parameter :username, :required => true
46
-
47
- parser :rsp do
48
- element :status, :attribute => :stat
49
-
50
- element :user do
51
- element :id, :attribute => true
52
- element :nsid, :attribute => true
53
- element :username
54
- end
55
- end
56
- end
57
- end
58
- end
59
-
60
- FakeWeb.register_uri(:get, 'http://api.flickr.com/services/rest?api_key=secret&method=flickr.people.findByUsername&username=duncandavidson', :string => <<-RESPONSE)
61
- <?xml version="1.0" encoding="utf-8" ?>
62
- <rsp stat="ok">
63
- <user id="59532755@N00" nsid="59532755@N00">
64
- <username>duncandavidson</username>
65
- </user>
66
- </rsp>
67
- RESPONSE
68
-
69
- FakeWeb.register_uri(:get, 'http://api.flickr.com/services/rest?user_id=59532755@N00&per_page=3&method=flickr.people.getPublicPhotos&api_key=secret', :string => <<-RESPONSE)
70
- <?xml version="1.0" encoding="utf-8" ?>
71
- <rsp stat="ok">
72
- <photos page="1" pages="2610" perpage="3" total="7830">
73
- <photo id="3508500178" owner="59532755@N00" secret="2ebe0307e3" server="3562" farm="4" title="Rich Kilmer" ispublic="1" isfriend="0" isfamily="0" />
74
- <photo id="3508500140" owner="59532755@N00" secret="10b217377b" server="3593" farm="4" title="Women In Rails" ispublic="1" isfriend="0" isfamily="0" />
75
- <photo id="3507688713" owner="59532755@N00" secret="83bc8fbf71" server="3620" farm="4" title="Obie Fernandez" ispublic="1" isfriend="0" isfamily="0" />
76
- </photos>
77
- </rsp>
78
- RESPONSE
@@ -1,17 +0,0 @@
1
- class ParameterAliasService < Relax::Service
2
- endpoint 'http://example.com/' do
3
- action :test do
4
- parameter :APIKey, :as => :api_key, :required => true
5
-
6
- parser :response do
7
- attribute :stat
8
- end
9
- end
10
- end
11
- end
12
-
13
- FakeWeb.register_uri(:get, 'http://example.com/?APIKey=secret', :string => <<-RESPONSE)
14
- <?xml version="1.0" encoding="utf-8" ?>
15
- <response stat="ok">
16
- </response>
17
- RESPONSE