relax 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
File without changes
data/VERSION.yml CHANGED
@@ -1,4 +1,4 @@
1
1
  ---
2
2
  :major: 0
3
3
  :minor: 1
4
- :patch: 0
4
+ :patch: 1
data/lib/relax/action.rb CHANGED
@@ -10,6 +10,7 @@ module Relax
10
10
  @options = options
11
11
 
12
12
  extend_context(endpoint)
13
+ parse_url_parameters
13
14
  context.evaluate(&block) if block_given?
14
15
  end
15
16
 
@@ -35,5 +36,14 @@ module Relax
35
36
  Performer.new(method, url, values, credentials)
36
37
  end
37
38
  private :performer
39
+
40
+ def parse_url_parameters
41
+ url.scan(/(?:\:)([a-z_]+)/).flatten.each do |name|
42
+ defaults do
43
+ parameter name.to_sym, :required => true
44
+ end
45
+ end
46
+ end
47
+ private :parse_url_parameters
38
48
  end
39
49
  end
data/lib/relax/context.rb CHANGED
@@ -25,7 +25,8 @@ module Relax
25
25
  end
26
26
 
27
27
  def parser(root, options={}, &block) # :nodoc:
28
- @parser ||= Relief::Parser.new(root, options, &block)
28
+ @parser ||= root.kind_of?(Class) ? root.new(options, &block) :
29
+ Relief::Parser.new(root, options, &block)
29
30
  end
30
31
 
31
32
  def parse(response) # :nodoc:
@@ -5,6 +5,8 @@ module Relax
5
5
  @url = url
6
6
  @values = values
7
7
  @credentials = credentials
8
+
9
+ parse_url_tokens
8
10
  end
9
11
 
10
12
  def perform
@@ -15,7 +17,11 @@ module Relax
15
17
  end
16
18
 
17
19
  def url
18
- uri = URI.parse(@url)
20
+ url = @url.gsub(/\:[a-z_]+/) do |name|
21
+ @url_values[name[1..-1].to_sym]
22
+ end
23
+
24
+ uri = URI.parse(url)
19
25
  uri.query = query unless query.nil? || query.empty?
20
26
  uri.userinfo = @credentials.join(':') if @credentials
21
27
  uri.to_s
@@ -28,5 +34,14 @@ module Relax
28
34
  end.compact.join('&')
29
35
  end
30
36
  private :query
37
+
38
+ def parse_url_tokens
39
+ @url_values = @url.scan(/(?:\:)([a-z_]+)/).flatten.inject({}) do |values, name|
40
+ name = name.to_sym
41
+ values[name] = @values.delete(name) if @values.key?(name)
42
+ values
43
+ end
44
+ end
45
+ private :parse_url_tokens
31
46
  end
32
47
  end
data/lib/relax/service.rb CHANGED
@@ -6,6 +6,7 @@ module Relax
6
6
 
7
7
  def authenticate(*args)
8
8
  @credentials = args
9
+ self
9
10
  end
10
11
 
11
12
  class << self
@@ -0,0 +1,10 @@
1
+ require File.join(File.dirname(__FILE__), '..', 'spec_helper')
2
+
3
+ describe Relax::Context do
4
+
5
+ it "utilizes a custom parser for Class parsers" do
6
+ service = ServiceWithCustomParser.new
7
+ service.test.should == 'parsed'
8
+ end
9
+
10
+ end
@@ -14,7 +14,7 @@ describe Relax::Endpoint do
14
14
  end
15
15
 
16
16
  describe "actions" do
17
- it "actions should check for required values for service defaults" do
17
+ it "should check for required values for service defaults" do
18
18
  service = Class.new(Relax::Service) do
19
19
  defaults { parameter :api_key, :required => true }
20
20
  endpoint("http://api.example.com/") { action(:fetch) { } }
@@ -25,7 +25,7 @@ describe Relax::Endpoint do
25
25
  }.should raise_error(ArgumentError, /missing.*api_key/i)
26
26
  end
27
27
 
28
- it "actions should check for required values for endpoint defaults" do
28
+ it "should check for required values for endpoint defaults" do
29
29
  service = Class.new(Relax::Service) do
30
30
  endpoint("http://api.example.com/") do
31
31
  defaults { parameter :operation, :required => true }
@@ -38,7 +38,7 @@ describe Relax::Endpoint do
38
38
  }.should raise_error(ArgumentError, /missing.*operation/i)
39
39
  end
40
40
 
41
- it "actions should check for required values for action parameters" do
41
+ it "should check for required values for action parameters" do
42
42
  service = Class.new(Relax::Service) do
43
43
  endpoint("http://api.example.com/") do
44
44
  action(:fetch) { parameter :id, :required => true }
@@ -49,6 +49,36 @@ describe Relax::Endpoint do
49
49
  service.new.fetch
50
50
  }.should raise_error(ArgumentError, /missing.*id/i)
51
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
52
82
  end
53
83
 
54
84
  describe ".action" do
@@ -5,6 +5,17 @@ describe Relax::Service do
5
5
  Relax::Service.should respond_to(:defaults)
6
6
  end
7
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
+
8
19
  describe ".endpoint" do
9
20
  it "is callable from within a Service" do
10
21
  Relax::Service.should respond_to(:endpoint)
@@ -0,0 +1,28 @@
1
+ class TestCustomParser
2
+
3
+ def initialize(options = {}, &block)
4
+ end
5
+
6
+ def parse(input)
7
+ 'parsed'
8
+ end
9
+
10
+ end
11
+
12
+ class ServiceWithCustomParser < Relax::Service
13
+ endpoint "http://test.local/rest" do
14
+
15
+ action :test do
16
+ parser TestCustomParser do
17
+ element :status, :attribute => :stat
18
+ end
19
+ end
20
+
21
+ end
22
+ end
23
+
24
+ FakeWeb.register_uri(:get, 'http://test.local/rest', :string => <<-RESPONSE)
25
+ <?xml version="1.0" encoding="utf-8" ?>
26
+ <test stat="ok">
27
+ </test>
28
+ RESPONSE
data/spec/spec_helper.rb CHANGED
@@ -10,3 +10,4 @@ require 'fakeweb'
10
10
 
11
11
  require File.join(File.dirname(__FILE__), '..', 'lib', 'relax')
12
12
  require File.join(File.dirname(__FILE__), 'services', 'flickr')
13
+ require File.join(File.dirname(__FILE__), 'services', 'service_with_custom_parser')
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: relax
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tyler Hunt
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-05-07 00:00:00 -07:00
12
+ date: 2009-06-03 00:00:00 -04:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -70,9 +70,10 @@ extensions: []
70
70
 
71
71
  extra_rdoc_files:
72
72
  - LICENSE
73
- - README
73
+ - README.rdoc
74
74
  files:
75
75
  - LICENSE
76
+ - README.rdoc
76
77
  - Rakefile
77
78
  - VERSION.yml
78
79
  - lib/relax.rb
@@ -84,12 +85,13 @@ files:
84
85
  - lib/relax/parameter.rb
85
86
  - lib/relax/performer.rb
86
87
  - lib/relax/service.rb
88
+ - spec/relax/context_spec.rb
87
89
  - spec/relax/endpoint_spec.rb
88
90
  - spec/relax/integration_spec.rb
89
91
  - spec/relax/service_spec.rb
90
92
  - spec/services/flickr.rb
93
+ - spec/services/service_with_custom_parser.rb
91
94
  - spec/spec_helper.rb
92
- - README
93
95
  has_rdoc: true
94
96
  homepage: http://github.com/tylerhunt/relax
95
97
  post_install_message:
@@ -117,8 +119,10 @@ signing_key:
117
119
  specification_version: 2
118
120
  summary: A flexible library for creating web service consumers.
119
121
  test_files:
122
+ - spec/relax/context_spec.rb
120
123
  - spec/relax/endpoint_spec.rb
121
124
  - spec/relax/integration_spec.rb
122
125
  - spec/relax/service_spec.rb
123
126
  - spec/services/flickr.rb
127
+ - spec/services/service_with_custom_parser.rb
124
128
  - spec/spec_helper.rb