rest-client 0.1 → 0.2

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.

Potentially problematic release.


This version of rest-client might be problematic. Click here for more details.

data/Rakefile CHANGED
@@ -30,7 +30,7 @@ require 'rake/gempackagetask'
30
30
  require 'rake/rdoctask'
31
31
  require 'fileutils'
32
32
 
33
- version = "0.1"
33
+ version = "0.2"
34
34
  name = "rest-client"
35
35
 
36
36
  spec = Gem::Specification.new do |s|
@@ -75,7 +75,7 @@ Rake::RDocTask.new do |t|
75
75
  t.options << '--line-numbers' << '--inline-source' << '-A cattr_accessor=object'
76
76
  t.options << '--charset' << 'utf-8'
77
77
  t.rdoc_files.include('README')
78
- t.rdoc_files.include('lib/rest_client.rb')
78
+ t.rdoc_files.include('lib/*.rb')
79
79
  end
80
80
 
81
81
  CLEAN.include [ 'pkg', '*.gem', '.config' ]
@@ -0,0 +1,58 @@
1
+ module RestClient
2
+ # A class that can be instantiated for access to a RESTful resource,
3
+ # including authentication.
4
+ #
5
+ # Example:
6
+ #
7
+ # resource = RestClient::Resource.new('http://some/resource')
8
+ # jpg = resource.get(:accept => 'image/jpg')
9
+ #
10
+ # With HTTP basic authentication:
11
+ #
12
+ # resource = RestClient::Resource.new('http://protected/resource', 'user', 'pass')
13
+ # resource.delete
14
+ #
15
+ class Resource
16
+ attr_reader :url, :user, :password
17
+
18
+ def initialize(url, user=nil, password=nil)
19
+ @url = url
20
+ @user = user
21
+ @password = password
22
+ end
23
+
24
+ def get(headers={})
25
+ Request.execute(:method => :get,
26
+ :url => url,
27
+ :user => user,
28
+ :password => password,
29
+ :headers => headers)
30
+ end
31
+
32
+ def post(payload, headers={})
33
+ Request.execute(:method => :post,
34
+ :url => url,
35
+ :payload => payload,
36
+ :user => user,
37
+ :password => password,
38
+ :headers => headers)
39
+ end
40
+
41
+ def put(payload, headers={})
42
+ Request.execute(:method => :put,
43
+ :url => url,
44
+ :payload => payload,
45
+ :user => user,
46
+ :password => password,
47
+ :headers => headers)
48
+ end
49
+
50
+ def delete(headers={})
51
+ Request.execute(:method => :delete,
52
+ :url => url,
53
+ :user => user,
54
+ :password => password,
55
+ :headers => headers)
56
+ end
57
+ end
58
+ end
@@ -1,33 +1,51 @@
1
1
  require 'uri'
2
2
  require 'net/http'
3
3
 
4
+ require File.dirname(__FILE__) + '/resource'
5
+
4
6
  # This module's static methods are the entry point for using the REST client.
5
7
  module RestClient
6
8
  def self.get(url, headers={})
7
- Request.new(:get, url, nil, headers).execute
9
+ Request.execute(:method => :get,
10
+ :url => url,
11
+ :headers => headers)
8
12
  end
9
13
 
10
- def self.post(url, payload=nil, headers={})
11
- Request.new(:post, url, payload, headers).execute
14
+ def self.post(url, payload, headers={})
15
+ Request.execute(:method => :post,
16
+ :url => url,
17
+ :payload => payload,
18
+ :headers => headers)
12
19
  end
13
20
 
14
- def self.put(url, payload=nil, headers={})
15
- Request.new(:put, url, payload, headers).execute
21
+ def self.put(url, payload, headers={})
22
+ Request.execute(:method => :put,
23
+ :url => url,
24
+ :payload => payload,
25
+ :headers => headers)
16
26
  end
17
27
 
18
28
  def self.delete(url, headers={})
19
- Request.new(:delete, url, nil, headers).execute
29
+ Request.execute(:method => :delete,
30
+ :url => url,
31
+ :headers => headers)
20
32
  end
21
33
 
22
34
  # Internal class used to build and execute the request.
23
35
  class Request
24
- attr_reader :method, :url, :payload, :headers
36
+ attr_reader :method, :url, :payload, :headers, :user, :password
37
+
38
+ def self.execute(args)
39
+ new(args).execute
40
+ end
25
41
 
26
- def initialize(method, url, payload, headers)
27
- @method = method
28
- @url = url
29
- @payload = payload
30
- @headers = headers
42
+ def initialize(args)
43
+ @method = args[:method] or raise ArgumentError, "must pass :method"
44
+ @url = args[:url] or raise ArgumentError, "must pass :url"
45
+ @payload = args[:payload]
46
+ @headers = args[:headers] || {}
47
+ @user = args[:user]
48
+ @password = args[:password]
31
49
  end
32
50
 
33
51
  def execute
@@ -70,11 +88,17 @@ module RestClient
70
88
  class Unauthorized < Exception; end
71
89
 
72
90
  def transmit(uri, req, payload)
91
+ setup_credentials(req)
92
+
73
93
  Net::HTTP.start(uri.host, uri.port) do |http|
74
94
  process_result http.request(req, payload || "")
75
95
  end
76
96
  end
77
97
 
98
+ def setup_credentials(req)
99
+ req.basic_auth(user, password) if user
100
+ end
101
+
78
102
  def process_result(res)
79
103
  if %w(200 201 202).include? res.code
80
104
  res.body
@@ -0,0 +1,31 @@
1
+ require File.dirname(__FILE__) + '/base'
2
+
3
+ describe RestClient::Resource do
4
+ before do
5
+ @resource = RestClient::Resource.new('http://some/resource', 'jane', 'mypass')
6
+ end
7
+
8
+ it "GET" do
9
+ RestClient::Request.should_receive(:execute).with(:method => :get, :url => 'http://some/resource', :headers => {}, :user => 'jane', :password => 'mypass')
10
+ @resource.get
11
+ end
12
+
13
+ it "POST" do
14
+ RestClient::Request.should_receive(:execute).with(:method => :post, :url => 'http://some/resource', :payload => 'abc', :headers => { :content_type => 'image/jpg' }, :user => 'jane', :password => 'mypass')
15
+ @resource.post 'abc', :content_type => 'image/jpg'
16
+ end
17
+
18
+ it "PUT" do
19
+ RestClient::Request.should_receive(:execute).with(:method => :put, :url => 'http://some/resource', :payload => 'abc', :headers => { :content_type => 'image/jpg' }, :user => 'jane', :password => 'mypass')
20
+ @resource.put 'abc', :content_type => 'image/jpg'
21
+ end
22
+
23
+ it "DELETE" do
24
+ RestClient::Request.should_receive(:execute).with(:method => :delete, :url => 'http://some/resource', :headers => {}, :user => 'jane', :password => 'mypass')
25
+ @resource.delete
26
+ end
27
+
28
+ it "can instantiate with no user/password" do
29
+ @resource = RestClient::Resource.new('http://some/resource')
30
+ end
31
+ end
@@ -2,38 +2,30 @@ require File.dirname(__FILE__) + '/base'
2
2
 
3
3
  describe RestClient do
4
4
  context "public API" do
5
- before do
6
- @request = mock("restclient request")
7
- end
8
-
9
5
  it "GET" do
10
- RestClient::Request.should_receive(:new).with(:get, 'http://some/resource', nil, {}).and_return(@request)
11
- @request.should_receive(:execute)
6
+ RestClient::Request.should_receive(:execute).with(:method => :get, :url => 'http://some/resource', :headers => {})
12
7
  RestClient.get('http://some/resource')
13
8
  end
14
9
 
15
10
  it "POST" do
16
- RestClient::Request.should_receive(:new).with(:post, 'http://some/resource', 'payload', {}).and_return(@request)
17
- @request.should_receive(:execute)
11
+ RestClient::Request.should_receive(:execute).with(:method => :post, :url => 'http://some/resource', :payload => 'payload', :headers => {})
18
12
  RestClient.post('http://some/resource', 'payload')
19
13
  end
20
14
 
21
15
  it "PUT" do
22
- RestClient::Request.should_receive(:new).with(:put, 'http://some/resource', 'payload', {}).and_return(@request)
23
- @request.should_receive(:execute)
16
+ RestClient::Request.should_receive(:execute).with(:method => :put, :url => 'http://some/resource', :payload => 'payload', :headers => {})
24
17
  RestClient.put('http://some/resource', 'payload')
25
18
  end
26
19
 
27
20
  it "DELETE" do
28
- RestClient::Request.should_receive(:new).with(:delete, 'http://some/resource', nil, {}).and_return(@request)
29
- @request.should_receive(:execute)
21
+ RestClient::Request.should_receive(:execute).with(:method => :delete, :url => 'http://some/resource', :headers => {})
30
22
  RestClient.delete('http://some/resource')
31
23
  end
32
24
  end
33
25
 
34
26
  context RestClient::Request do
35
27
  before do
36
- @request = RestClient::Request.new(:put, 'http://some/resource', 'payload', {})
28
+ @request = RestClient::Request.new(:method => :put, :url => 'http://some/resource', :payload => 'payload')
37
29
 
38
30
  @uri = mock("uri")
39
31
  @uri.stub!(:path).and_return('/resource')
@@ -106,9 +98,44 @@ describe RestClient do
106
98
  @request.transmit(@uri, 'req', nil)
107
99
  end
108
100
 
101
+ it "sets up the credentials prior to the request" do
102
+ http = mock("net::http connection")
103
+ Net::HTTP.should_receive(:start).and_yield(http)
104
+ http.stub!(:request)
105
+ @request.stub!(:process_result)
106
+
107
+ @request.stub!(:user).and_return('joe')
108
+ @request.stub!(:password).and_return('mypass')
109
+ @request.should_receive(:setup_credentials).with('req')
110
+
111
+ @request.transmit(@uri, 'req', nil)
112
+ end
113
+
114
+ it "does not attempt to send any credentials if user is nil" do
115
+ @request.stub!(:user).and_return(nil)
116
+ req = mock("request")
117
+ req.should_not_receive(:basic_auth)
118
+ @request.setup_credentials(req)
119
+ end
120
+
121
+ it "does not attempt to send any credentials if user is nil" do
122
+ @request.stub!(:user).and_return('joe')
123
+ @request.stub!(:password).and_return('mypass')
124
+ req = mock("request")
125
+ req.should_receive(:basic_auth).with('joe', 'mypass')
126
+ @request.setup_credentials(req)
127
+ end
128
+
109
129
  it "execute calls execute_inner" do
110
130
  @request.should_receive(:execute_inner)
111
131
  @request.execute
112
132
  end
133
+
134
+ it "class method execute wraps constructor" do
135
+ req = mock("rest request")
136
+ RestClient::Request.should_receive(:new).with(1 => 2).and_return(req)
137
+ req.should_receive(:execute)
138
+ RestClient::Request.execute(1 => 2)
139
+ end
113
140
  end
114
141
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rest-client
3
3
  version: !ruby/object:Gem::Version
4
- version: "0.1"
4
+ version: "0.2"
5
5
  platform: ruby
6
6
  authors:
7
7
  - Adam Wiggins
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2008-03-08 23:00:00 -08:00
12
+ date: 2008-03-10 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -23,8 +23,10 @@ extra_rdoc_files: []
23
23
 
24
24
  files:
25
25
  - Rakefile
26
+ - lib/resource.rb
26
27
  - lib/rest_client.rb
27
28
  - spec/base.rb
29
+ - spec/resource_spec.rb
28
30
  - spec/rest_client_spec.rb
29
31
  has_rdoc: true
30
32
  homepage: http://rest-client.heroku.com/