nap 0.4 → 0.5
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 +30 -0
- data/lib/rest.rb +3 -0
- data/lib/rest/request.rb +30 -6
- metadata +63 -13
- data/README +0 -24
data/README.md
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
# Nap
|
2
|
+
|
3
|
+
It be an extremely simple REST library, yo!
|
4
|
+
|
5
|
+
## Example
|
6
|
+
|
7
|
+
gem 'nap'
|
8
|
+
require 'rest'
|
9
|
+
require 'json'
|
10
|
+
|
11
|
+
response = REST.get('http://twitter.com/statuses/friends_timeline.json', {},
|
12
|
+
{:username => '_evan', :password => 'buttonscat'}
|
13
|
+
)
|
14
|
+
if response.ok?
|
15
|
+
timeline = JSON.parse(response.body)
|
16
|
+
puts(timeline.map do |item|
|
17
|
+
"#{item['user']['name']}\n\n#{item['text']}"
|
18
|
+
end.join("\n\n--\n\n"))
|
19
|
+
elsif response.forbidden?
|
20
|
+
puts "Are you sure you're `_evan' and your password is the name of your cat?"
|
21
|
+
else
|
22
|
+
puts "Something went wrong (#{response.status_code})"
|
23
|
+
puts response.body
|
24
|
+
end
|
25
|
+
|
26
|
+
## Proxy support
|
27
|
+
|
28
|
+
To enable the proxy settings in Nap, you can either use the HTTP\_PROXY or http\_proxy enviroment variable.
|
29
|
+
|
30
|
+
$ env HTTP_PROXY=http://rob:secret@192.167.1.254:665 ruby app.rb
|
data/lib/rest.rb
CHANGED
@@ -3,6 +3,9 @@ require 'uri'
|
|
3
3
|
# REST is basically a convenience wrapper around Net::HTTP. It defines a simple and consistant API for doing REST-style
|
4
4
|
# HTTP calls.
|
5
5
|
module REST
|
6
|
+
# Raised when the remote server disconnects when reading the response
|
7
|
+
class DisconnectedError < StandardError; end
|
8
|
+
|
6
9
|
# Performs a HEAD on a resource. See REST::Request.new for a complete discussion of options.
|
7
10
|
#
|
8
11
|
# response = REST.get('http://example.com/pigeons/12',
|
data/lib/rest/request.rb
CHANGED
@@ -44,19 +44,19 @@ module REST
|
|
44
44
|
# == TLS / SSL examples
|
45
45
|
#
|
46
46
|
# # Use a client key and certificate
|
47
|
-
# request = REST::Request.new(:get, URI.parse('https://example.com/pigeons/1'),
|
47
|
+
# request = REST::Request.new(:get, URI.parse('https://example.com/pigeons/1'), nil, {}, {
|
48
48
|
# :tls_key_and_certificate_file => '/home/alice/keys/example.pem'
|
49
49
|
# })
|
50
50
|
#
|
51
51
|
# # Use a client certificate and key from a specific location
|
52
52
|
# key_and_certificate = File.read('/home/alice/keys/example.pem')
|
53
|
-
# request = REST::Request.new(:get, URI.parse('https://example.com/pigeons/1'),
|
53
|
+
# request = REST::Request.new(:get, URI.parse('https://example.com/pigeons/1'), nil, {}, {
|
54
54
|
# :tls_key => OpenSSL::PKey::RSA.new(key_and_certificate),
|
55
55
|
# :tls_certificate => OpenSSL::X509::Certificate.new(key_and_certificate)
|
56
56
|
# })
|
57
57
|
#
|
58
58
|
# # Verify the server certificate against a specific certificate
|
59
|
-
# request = REST::Request.new(:get, URI.parse('https://example.com/pigeons/1'),
|
59
|
+
# request = REST::Request.new(:get, URI.parse('https://example.com/pigeons/1'), nil, {}, {
|
60
60
|
# :tls_verify => true,
|
61
61
|
# :tls_ca_file => '/home/alice/keys/example.pem'
|
62
62
|
# })
|
@@ -73,6 +73,26 @@ module REST
|
|
73
73
|
[url.path, url.query].compact.join('?')
|
74
74
|
end
|
75
75
|
|
76
|
+
def http_proxy
|
77
|
+
ENV['HTTP_PROXY'] || ENV['http_proxy']
|
78
|
+
end
|
79
|
+
|
80
|
+
def proxy_settings
|
81
|
+
http_proxy ? URI.parse(http_proxy) : nil
|
82
|
+
end
|
83
|
+
|
84
|
+
def proxy
|
85
|
+
@proxy ||= Net::HTTP.Proxy(proxy_settings.host, proxy_settings.port, proxy_settings.user, proxy_settings.password)
|
86
|
+
end
|
87
|
+
|
88
|
+
def http_request
|
89
|
+
if proxy_settings
|
90
|
+
proxy.new(url.host, url.port)
|
91
|
+
else
|
92
|
+
Net::HTTP.new(url.host, url.port)
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
76
96
|
# Performs the actual request and returns a REST::Response object with the response
|
77
97
|
def perform
|
78
98
|
case verb
|
@@ -95,8 +115,8 @@ module REST
|
|
95
115
|
if options[:username] and options[:password]
|
96
116
|
request.basic_auth(options[:username], options[:password])
|
97
117
|
end
|
98
|
-
|
99
|
-
http_request =
|
118
|
+
|
119
|
+
http_request = http_request()
|
100
120
|
|
101
121
|
# enable SSL/TLS
|
102
122
|
if url.scheme == 'https'
|
@@ -130,7 +150,11 @@ module REST
|
|
130
150
|
end
|
131
151
|
end
|
132
152
|
|
133
|
-
|
153
|
+
begin
|
154
|
+
response = http_request.start { |http| http.request(request) }
|
155
|
+
rescue EOFError => error
|
156
|
+
raise REST::DisconnectedError, error.message
|
157
|
+
end
|
134
158
|
REST::Response.new(response.code, response.instance_variable_get('@header'), response.body)
|
135
159
|
end
|
136
160
|
|
metadata
CHANGED
@@ -1,7 +1,12 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: nap
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
|
4
|
+
hash: 1
|
5
|
+
prerelease:
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 5
|
9
|
+
version: "0.5"
|
5
10
|
platform: ruby
|
6
11
|
authors:
|
7
12
|
- Manfred Stienstra
|
@@ -9,27 +14,66 @@ autorequire:
|
|
9
14
|
bindir: bin
|
10
15
|
cert_chain: []
|
11
16
|
|
12
|
-
date:
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
+
date: 2012-03-12 00:00:00 Z
|
18
|
+
dependencies:
|
19
|
+
- !ruby/object:Gem::Dependency
|
20
|
+
name: rake
|
21
|
+
prerelease: false
|
22
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
23
|
+
none: false
|
24
|
+
requirements:
|
25
|
+
- - ">="
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
hash: 3
|
28
|
+
segments:
|
29
|
+
- 0
|
30
|
+
version: "0"
|
31
|
+
type: :development
|
32
|
+
version_requirements: *id001
|
33
|
+
- !ruby/object:Gem::Dependency
|
34
|
+
name: test-spec
|
35
|
+
prerelease: false
|
36
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
37
|
+
none: false
|
38
|
+
requirements:
|
39
|
+
- - ">="
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
hash: 3
|
42
|
+
segments:
|
43
|
+
- 0
|
44
|
+
version: "0"
|
45
|
+
type: :development
|
46
|
+
version_requirements: *id002
|
47
|
+
- !ruby/object:Gem::Dependency
|
48
|
+
name: mocha
|
49
|
+
prerelease: false
|
50
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
51
|
+
none: false
|
52
|
+
requirements:
|
53
|
+
- - ">="
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
hash: 3
|
56
|
+
segments:
|
57
|
+
- 0
|
58
|
+
version: "0"
|
59
|
+
type: :development
|
60
|
+
version_requirements: *id003
|
61
|
+
description: " Nap is a really simple REST library. It allows you to perform HTTP requests\n with minimal amounts of code.\n"
|
17
62
|
email: manfred@fngtps.com
|
18
63
|
executables: []
|
19
64
|
|
20
65
|
extensions: []
|
21
66
|
|
22
67
|
extra_rdoc_files:
|
23
|
-
- README
|
68
|
+
- README.md
|
24
69
|
- LICENSE
|
25
70
|
files:
|
26
71
|
- lib/rest.rb
|
27
72
|
- lib/rest/request.rb
|
28
73
|
- lib/rest/response.rb
|
29
74
|
- support/cacert.pem
|
30
|
-
- README
|
75
|
+
- README.md
|
31
76
|
- LICENSE
|
32
|
-
has_rdoc: true
|
33
77
|
homepage:
|
34
78
|
licenses: []
|
35
79
|
|
@@ -39,23 +83,29 @@ rdoc_options:
|
|
39
83
|
require_paths:
|
40
84
|
- lib
|
41
85
|
required_ruby_version: !ruby/object:Gem::Requirement
|
86
|
+
none: false
|
42
87
|
requirements:
|
43
88
|
- - ">="
|
44
89
|
- !ruby/object:Gem::Version
|
90
|
+
hash: 3
|
91
|
+
segments:
|
92
|
+
- 0
|
45
93
|
version: "0"
|
46
|
-
version:
|
47
94
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
95
|
+
none: false
|
48
96
|
requirements:
|
49
97
|
- - ">="
|
50
98
|
- !ruby/object:Gem::Version
|
99
|
+
hash: 3
|
100
|
+
segments:
|
101
|
+
- 0
|
51
102
|
version: "0"
|
52
|
-
version:
|
53
103
|
requirements: []
|
54
104
|
|
55
105
|
rubyforge_project:
|
56
|
-
rubygems_version: 1.
|
106
|
+
rubygems_version: 1.8.18
|
57
107
|
signing_key:
|
58
108
|
specification_version: 3
|
59
|
-
summary: Nap is a really simple REST
|
109
|
+
summary: Nap is a really simple REST library.
|
60
110
|
test_files: []
|
61
111
|
|
data/README
DELETED
@@ -1,24 +0,0 @@
|
|
1
|
-
= Nap
|
2
|
-
|
3
|
-
It be an extremely simple REST library, yo!
|
4
|
-
|
5
|
-
== Example
|
6
|
-
|
7
|
-
gem 'nap'
|
8
|
-
require 'rest'
|
9
|
-
require 'json'
|
10
|
-
|
11
|
-
response = REST.get('http://twitter.com/statuses/friends_timeline.json', {},
|
12
|
-
{:username => '_evan', :password => 'buttonscat'}
|
13
|
-
)
|
14
|
-
if response.ok?
|
15
|
-
timeline = JSON.parse(response.body)
|
16
|
-
puts(timeline.map do |item|
|
17
|
-
"#{item['user']['name']}\n\n#{item['text']}"
|
18
|
-
end.join("\n\n--\n\n"))
|
19
|
-
elsif response.forbidden?
|
20
|
-
puts "Are you sure you're `_evan' and your password is the name of your cat?"
|
21
|
-
else
|
22
|
-
puts "Something went wrong (#{response.status_code})"
|
23
|
-
puts response.body
|
24
|
-
end
|