relax 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION.yml CHANGED
@@ -1,4 +1,4 @@
1
1
  ---
2
2
  :major: 0
3
3
  :minor: 1
4
- :patch: 1
4
+ :patch: 2
data/lib/relax/action.rb CHANGED
@@ -14,10 +14,10 @@ module Relax
14
14
  context.evaluate(&block) if block_given?
15
15
  end
16
16
 
17
- def execute(values, credentials, *args)
17
+ def execute(values, options, *args)
18
18
  args.unshift(values) if values
19
- instance = Instance.new(*args)
20
- response = performer(instance, credentials).perform
19
+ instance = Instance.new(options, *args)
20
+ response = performer(instance, options).perform
21
21
  context.parse(response)
22
22
  end
23
23
 
@@ -31,9 +31,9 @@ module Relax
31
31
  end
32
32
  private :url
33
33
 
34
- def performer(instance, credentials)
34
+ def performer(instance, options)
35
35
  values = instance.values(context)
36
- Performer.new(method, url, values, credentials)
36
+ Performer.new(method, url, values, options)
37
37
  end
38
38
  private :performer
39
39
 
data/lib/relax/context.rb CHANGED
@@ -11,7 +11,7 @@ module Relax
11
11
  end
12
12
 
13
13
  def parameter(name, options={})
14
- unless @parameters.find { |parameter| parameter.name == name }
14
+ unless @parameters.find { |parameter| parameter.named?(name) }
15
15
  @parameters << Parameter.new(name, options)
16
16
  else
17
17
  raise ArgumentError.new("Duplicate parameter '#{name}'.")
@@ -19,14 +19,19 @@ module Relax
19
19
  end
20
20
 
21
21
  def set(name, value)
22
- if parameter = @parameters.find { |parameter| parameter.name == name }
22
+ if parameter = @parameters.find { |parameter| parameter.named?(name) }
23
23
  parameter.value = value
24
24
  end
25
25
  end
26
26
 
27
27
  def parser(root, options={}, &block) # :nodoc:
28
- @parser ||= root.kind_of?(Class) ? root.new(options, &block) :
29
- Relief::Parser.new(root, options, &block)
28
+ @parser ||= begin
29
+ if root.kind_of?(Class)
30
+ root.new(options, &block)
31
+ else
32
+ Relief::Parser.new(root, options, &block)
33
+ end
34
+ end
30
35
  end
31
36
 
32
37
  def parse(response) # :nodoc:
@@ -1,6 +1,8 @@
1
1
  module Relax
2
2
  class Instance # :nodoc:
3
- def initialize(*args)
3
+ def initialize(options, *args)
4
+ @options = options
5
+
4
6
  @values = args.inject({}) do |values, arg|
5
7
  arg.is_a?(Hash) ? values.merge(arg) : values
6
8
  end
@@ -8,12 +10,12 @@ module Relax
8
10
 
9
11
  def values(context)
10
12
  context.parameters.inject({}) do |values, parameter|
11
- name = parameter.name
13
+ name = parameter.options[:as] || parameter.name
12
14
 
13
- if value = @values[parameter.name] || parameter.value
15
+ if (value = @values[name] || parameter.value) || @options[:include_blank_values]
14
16
  values[parameter.name] = value
15
17
  elsif parameter.required?
16
- raise ArgumentError.new("Missing value for '#{parameter.name}'.")
18
+ raise ArgumentError.new("Missing value for '#{name}'.")
17
19
  end
18
20
 
19
21
  values
@@ -8,6 +8,10 @@ module Relax
8
8
  @options = options
9
9
  end
10
10
 
11
+ def named?(name)
12
+ name == (@options[:as] || @name)
13
+ end
14
+
11
15
  def value
12
16
  @value || @options[:default]
13
17
  end
@@ -1,15 +1,19 @@
1
1
  module Relax
2
2
  class Performer
3
- def initialize(method, url, values, credentials)
3
+ def initialize(method, url, values, options={})
4
4
  @method = method
5
5
  @url = url
6
6
  @values = values
7
- @credentials = credentials
7
+ @credentials = options.delete(:credentials)
8
+ @proxy = options.delete(:proxy)
9
+ @options = options
8
10
 
9
11
  parse_url_tokens
10
12
  end
11
13
 
12
14
  def perform
15
+ RestClient.proxy = @proxy if @proxy
16
+
13
17
  case @method
14
18
  when :delete, :get, :head then RestClient.send(@method, url)
15
19
  when :post, :put then RestClient.send(@method, url, query)
@@ -30,13 +34,15 @@ module Relax
30
34
 
31
35
  def query
32
36
  @values.collect do |name, value|
33
- "#{name}=#{value}" if value
37
+ "#{name}=#{value}" if value || @options[:include_blank_values]
34
38
  end.compact.join('&')
35
39
  end
36
40
  private :query
37
41
 
38
42
  def parse_url_tokens
39
- @url_values = @url.scan(/(?:\:)([a-z_]+)/).flatten.inject({}) do |values, name|
43
+ tokens = @url.scan(/(?:\:)([a-z_]+)/).flatten
44
+
45
+ @url_values = tokens.inject({}) do |values, name|
40
46
  name = name.to_sym
41
47
  values[name] = @values.delete(name) if @values.key?(name)
42
48
  values
data/lib/relax/service.rb CHANGED
@@ -1,11 +1,22 @@
1
1
  module Relax
2
2
  class Service
3
- def initialize(values={})
3
+ def initialize(values={}, options={})
4
4
  @values = values
5
+ @options = options
5
6
  end
6
7
 
7
8
  def authenticate(*args)
8
- @credentials = 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
9
20
  self
10
21
  end
11
22
 
@@ -23,7 +34,7 @@ module Relax
23
34
  @actions[action.name] = action.name
24
35
 
25
36
  define_method(action.name) do |*args|
26
- action.execute(@values, @credentials, *args)
37
+ action.execute(@values, @options, *args)
27
38
  end
28
39
  else
29
40
  raise ArgumentError.new("Duplicate action '#{action.name}'.")
@@ -1,10 +1,18 @@
1
1
  require File.join(File.dirname(__FILE__), '..', 'spec_helper')
2
2
 
3
3
  describe Relax::Context do
4
-
5
4
  it "utilizes a custom parser for Class parsers" do
6
- service = ServiceWithCustomParser.new
5
+ service = CustomParserService.new
7
6
  service.test.should == 'parsed'
8
7
  end
9
-
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
10
18
  end
@@ -0,0 +1,19 @@
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,23 +1,19 @@
1
1
  class TestCustomParser
2
-
3
2
  def initialize(options = {}, &block)
4
3
  end
5
-
4
+
6
5
  def parse(input)
7
6
  'parsed'
8
7
  end
9
-
10
8
  end
11
9
 
12
- class ServiceWithCustomParser < Relax::Service
10
+ class CustomParserService < Relax::Service
13
11
  endpoint "http://test.local/rest" do
14
-
15
12
  action :test do
16
13
  parser TestCustomParser do
17
14
  element :status, :attribute => :stat
18
15
  end
19
16
  end
20
-
21
17
  end
22
18
  end
23
19
 
@@ -0,0 +1,17 @@
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
data/spec/spec_helper.rb CHANGED
@@ -10,4 +10,6 @@ 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')
13
+ require File.join(File.dirname(__FILE__), 'services', 'custom_parser_service')
14
+ require File.join(File.dirname(__FILE__), 'services', 'parameter_alias_service')
15
+ require File.join(File.dirname(__FILE__), 'services', 'blank_values_service')
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.1
4
+ version: 0.1.2
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-06-03 00:00:00 -04:00
12
+ date: 2009-06-30 00:00:00 -04:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -89,8 +89,10 @@ files:
89
89
  - spec/relax/endpoint_spec.rb
90
90
  - spec/relax/integration_spec.rb
91
91
  - spec/relax/service_spec.rb
92
+ - spec/services/blank_values_service.rb
93
+ - spec/services/custom_parser_service.rb
92
94
  - spec/services/flickr.rb
93
- - spec/services/service_with_custom_parser.rb
95
+ - spec/services/parameter_alias_service.rb
94
96
  - spec/spec_helper.rb
95
97
  has_rdoc: true
96
98
  homepage: http://github.com/tylerhunt/relax
@@ -123,6 +125,8 @@ test_files:
123
125
  - spec/relax/endpoint_spec.rb
124
126
  - spec/relax/integration_spec.rb
125
127
  - spec/relax/service_spec.rb
128
+ - spec/services/blank_values_service.rb
129
+ - spec/services/custom_parser_service.rb
126
130
  - spec/services/flickr.rb
127
- - spec/services/service_with_custom_parser.rb
131
+ - spec/services/parameter_alias_service.rb
128
132
  - spec/spec_helper.rb