weary 0.3.1 → 0.4.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/README.md CHANGED
@@ -61,12 +61,13 @@ Besides the name of the resource, you can also give `declare_resource` a block l
61
61
 
62
62
  declare "foo" do |r|
63
63
  r.url = "path/to/foo"
64
- r.via = :post # defaults to :get
65
- r.format = :xml # defaults to :json
66
- r.requires = [:id, :bar] # an array of params that the resource requires to be in the query/body
67
- r.with = [:blah] # an array of params that you can optionally send to the resource
68
- r.authenticates = false # does the method require basic authentication? defaults to false
69
- r.follows = false # if this is set to false, the formed request will not follow redirects.
64
+ r.via = :post # defaults to :get
65
+ r.format = :xml # defaults to :json
66
+ r.requires = [:id, :bar] # an array of params that the resource requires to be in the query/body
67
+ r.with = [:blah] # an array of params that you can optionally send to the resource
68
+ r.authenticates = false # does the method require basic authentication? defaults to false
69
+ r.follows = false # if this is set to false, the formed request will not follow redirects.
70
+ r.headers = {'Accept' => 'text/html'} # send custom headers. defaults to nil.
70
71
  end
71
72
 
72
73
  So this would form a method:
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.3.1
1
+ 0.4.0
data/lib/weary.rb CHANGED
@@ -75,6 +75,11 @@ module Weary
75
75
  def always_with(params)
76
76
  @always_with = params
77
77
  end
78
+
79
+ # Set custom Headers for your Request
80
+ def set_headers(headers)
81
+ @headers = headers
82
+ end
78
83
 
79
84
  # Declare a resource. Use it with a block to setup the resource
80
85
  #
@@ -125,6 +130,7 @@ module Weary
125
130
  preparation.domain = @domain
126
131
  preparation.url = (@url_pattern || "<domain><resource>.<format>")
127
132
  preparation.with = @always_with unless @always_with.nil?
133
+ preparation.headers = @headers unless (@headers.nil? || @headers.empty?)
128
134
  return preparation
129
135
  end
130
136
 
@@ -146,7 +152,14 @@ module Weary
146
152
  }
147
153
  if resource.with.is_a?(Hash)
148
154
  hash_string = ""
149
- resource.with.each_pair {|k,v| hash_string << ":#{k} => '#{v}',"}
155
+ resource.with.each_pair {|k,v|
156
+ if k.is_a?(Symbol)
157
+ k_string = ":#{k}"
158
+ else
159
+ k_string = "'#{k}'"
160
+ end
161
+ hash_string << "#{k_string} => '#{v}',"
162
+ }
150
163
  code << %Q{
151
164
  params = {#{hash_string.chop}}.delete_if {|key,value| value.empty? }.merge(params)
152
165
  }
@@ -181,6 +194,13 @@ module Weary
181
194
  url << "?" + options[:query].to_params unless options[:query].nil?
182
195
  }
183
196
  end
197
+ unless (resource.headers.nil? || resource.headers.empty?)
198
+ header_hash = ""
199
+ resource.headers.each_pair {|k,v|
200
+ header_hash << "'#{k}' => '#{v}',"
201
+ }
202
+ code << %Q{ options[:headers] = {#{header_hash.chop}} \n}
203
+ end
184
204
  if resource.authenticates?
185
205
  code << %Q{options[:basic_auth] = {:username => "#{@username}", :password => "#{@password}"} \n}
186
206
  end
data/lib/weary/request.rb CHANGED
@@ -73,6 +73,11 @@ module Weary
73
73
  end
74
74
  prepare.body = options[:body].is_a?(Hash) ? options[:body].to_params : options[:body] if options[:body]
75
75
  prepare.basic_auth(options[:basic_auth][:username], options[:basic_auth][:password]) if options[:basic_auth]
76
+ if options[:headers]
77
+ options[:headers].each_pair do |key, value|
78
+ prepare[key] = value
79
+ end
80
+ end
76
81
  prepare
77
82
  end
78
83
 
@@ -1,6 +1,6 @@
1
1
  module Weary
2
2
  class Resource
3
- attr_accessor :name, :domain, :with, :requires, :via, :format, :url, :authenticates, :follows
3
+ attr_accessor :name, :domain, :with, :requires, :via, :format, :url, :authenticates, :follows, :headers
4
4
 
5
5
  def initialize(name)
6
6
  self.name = name
@@ -106,7 +106,8 @@ module Weary
106
106
  :authenticates => authenticates?,
107
107
  :format => @format,
108
108
  :url => @url,
109
- :domain => @domain}}
109
+ :domain => @domain,
110
+ :headers => @headers}}
110
111
  end
111
112
 
112
113
  end
@@ -64,6 +64,9 @@ describe Weary::Resource do
64
64
  lambda { @test.format = :foobar }.should raise_error
65
65
  end
66
66
 
67
- it 'should be able to set Headers'
67
+ it 'should be able to set Headers' do
68
+ @test.headers = {'Content-Type' => 'text/html'}
69
+ @test.headers.should == {'Content-Type' => 'text/html'}
70
+ end
68
71
 
69
72
  end
data/spec/weary_spec.rb CHANGED
@@ -58,7 +58,12 @@ describe Weary do
58
58
  end
59
59
 
60
60
  describe "Set Headers" do
61
- it "should be a hash of values to pass in the Request head"
61
+ it "should be a hash of values to pass in the Request head" do
62
+ @test.on_domain "http://foo.bar"
63
+ @test.set_headers "Content-Type" => "text/html"
64
+ r = @test.get "resource"
65
+ r.headers.should == {"Content-Type"=>'text/html'}
66
+ end
62
67
  end
63
68
 
64
69
  describe "Common Request Paramaters" do
data/weary.gemspec CHANGED
@@ -2,11 +2,11 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = %q{weary}
5
- s.version = "0.3.1"
5
+ s.version = "0.4.0"
6
6
 
7
7
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
8
8
  s.authors = ["Mark Wunsch"]
9
- s.date = %q{2009-06-18}
9
+ s.date = %q{2009-06-24}
10
10
  s.description = %q{The Weary need REST: a tiny DSL that makes the consumption of RESTful web services simple.}
11
11
  s.email = %q{mark@markwunsch.com}
12
12
  s.extra_rdoc_files = [
@@ -36,11 +36,12 @@ Gem::Specification.new do |s|
36
36
  "spec/weary_spec.rb",
37
37
  "weary.gemspec"
38
38
  ]
39
+ s.has_rdoc = true
39
40
  s.homepage = %q{http://github.com/mwunsch/weary}
40
41
  s.rdoc_options = ["--charset=UTF-8"]
41
42
  s.require_paths = ["lib"]
42
43
  s.rubyforge_project = %q{weary}
43
- s.rubygems_version = %q{1.3.4}
44
+ s.rubygems_version = %q{1.3.1}
44
45
  s.summary = %q{A little DSL for consuming RESTful web services}
45
46
  s.test_files = [
46
47
  "spec/spec_helper.rb",
@@ -54,7 +55,7 @@ Gem::Specification.new do |s|
54
55
 
55
56
  if s.respond_to? :specification_version then
56
57
  current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
57
- s.specification_version = 3
58
+ s.specification_version = 2
58
59
 
59
60
  if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
60
61
  s.add_runtime_dependency(%q<crack>, [">= 0.1.2"])
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: weary
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mark Wunsch
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-06-18 00:00:00 -04:00
12
+ date: 2009-06-24 00:00:00 -04:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -55,8 +55,6 @@ files:
55
55
  - weary.gemspec
56
56
  has_rdoc: true
57
57
  homepage: http://github.com/mwunsch/weary
58
- licenses: []
59
-
60
58
  post_install_message:
61
59
  rdoc_options:
62
60
  - --charset=UTF-8
@@ -77,9 +75,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
77
75
  requirements: []
78
76
 
79
77
  rubyforge_project: weary
80
- rubygems_version: 1.3.4
78
+ rubygems_version: 1.3.1
81
79
  signing_key:
82
- specification_version: 3
80
+ specification_version: 2
83
81
  summary: A little DSL for consuming RESTful web services
84
82
  test_files:
85
83
  - spec/spec_helper.rb