nap 0.6.0 → 0.7.0pre0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/LICENSE +1 -1
- data/README.md +21 -2
- data/lib/rest.rb +15 -12
- data/lib/rest/request.rb +61 -35
- metadata +20 -19
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a5527a10413e8432aea9467adfb300646dea6be3
|
4
|
+
data.tar.gz: 2398375fc37518cc416a744b5663d25fc7e708ed
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e7eb50aa2b3f1d0a32576ceb0139f6dd6060230d31c7d5424bddad3d45d59c0ab88f103303cce82c142d1591d9ff3ff9b218bbe00aba98ff84a26f00eb89496a
|
7
|
+
data.tar.gz: 7dd407ee2b0b4d9008f4eb879e71f87f2dd47047d0ce0eeabaa46622a1711dd63169a64889aa0b840718dffbac67bc752c0fa813d990b0977cf7040b1a59ec65
|
data/LICENSE
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
Copyright (c)
|
1
|
+
Copyright (c) 2014 Manfred Stienstra, Fingertips <manfred@fngtps.com>
|
2
2
|
|
3
3
|
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
4
4
|
this software and associated documentation files (the "Software"), to deal in
|
data/README.md
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
# Nap
|
2
2
|
|
3
|
-
|
3
|
+
Nap is an extremely simple REST client for Ruby. It was built to quickly
|
4
|
+
fire off HTTP requests without having to research net/http internals.
|
4
5
|
|
5
6
|
## Example
|
6
7
|
|
@@ -23,8 +24,26 @@ It be an extremely simple REST library, yo!
|
|
23
24
|
puts response.body
|
24
25
|
end
|
25
26
|
|
27
|
+
## Advanced request configuration
|
28
|
+
|
29
|
+
If you need more control over the Net::HTTP request you can pass a block to
|
30
|
+
all of the request methods.
|
31
|
+
|
32
|
+
response = REST.get('http://google.com') do |http_request|
|
33
|
+
http_request.open_timeout = 15
|
34
|
+
http_request.set_debug_output(STDERR)
|
35
|
+
end
|
36
|
+
|
26
37
|
## Proxy support
|
27
38
|
|
28
39
|
To enable the proxy settings in Nap, you can either use the HTTP\_PROXY or http\_proxy enviroment variable.
|
29
40
|
|
30
|
-
$ env HTTP_PROXY=http://rob:secret@192.167.1.254:665 ruby app.rb
|
41
|
+
$ env HTTP_PROXY=http://rob:secret@192.167.1.254:665 ruby app.rb
|
42
|
+
|
43
|
+
## Contributions
|
44
|
+
|
45
|
+
Nap couldn't be the shining beacon in the eternal darkness without help from:
|
46
|
+
|
47
|
+
* Eloy Durán
|
48
|
+
* Joshua Sierles
|
49
|
+
* Thijs van der Vossen
|
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
|
+
# Library version
|
7
|
+
VERSION = '0.7.0pre0'
|
8
|
+
|
6
9
|
# Raised when the remote server disconnects when reading the response
|
7
10
|
class DisconnectedError < StandardError; end
|
8
11
|
|
@@ -17,8 +20,8 @@ module REST
|
|
17
20
|
# else
|
18
21
|
# puts "Couldn't fetch your pigeon (#{response.status_code})"
|
19
22
|
# end
|
20
|
-
def self.get(uri, headers={}, options={})
|
21
|
-
REST::Request.perform(:get, URI.parse(uri), nil, headers, options)
|
23
|
+
def self.get(uri, headers={}, options={}, &configure_block)
|
24
|
+
REST::Request.perform(:get, URI.parse(uri), nil, headers, options, &configure_block)
|
22
25
|
end
|
23
26
|
|
24
27
|
# Performs a HEAD on a resource. See REST::Request.new for a complete discussion of options.
|
@@ -31,8 +34,8 @@ module REST
|
|
31
34
|
# else
|
32
35
|
# puts "Couldn't fetch your pigeon (#{response.status_code})"
|
33
36
|
# end
|
34
|
-
def self.head(uri, headers={}, options={})
|
35
|
-
REST::Request.perform(:head, URI.parse(uri), nil, headers, options)
|
37
|
+
def self.head(uri, headers={}, options={}, &configure_block)
|
38
|
+
REST::Request.perform(:head, URI.parse(uri), nil, headers, options, &configure_block)
|
36
39
|
end
|
37
40
|
|
38
41
|
# Performs a DELETE on a resource. See REST::Request.new for a complete discussion of options.
|
@@ -45,8 +48,8 @@ module REST
|
|
45
48
|
# else
|
46
49
|
# puts "Couldn't delete your pigeon (#{response.status_code})"
|
47
50
|
# end
|
48
|
-
def self.delete(uri, headers={}, options={})
|
49
|
-
REST::Request.perform(:delete, URI.parse(uri), nil, headers, options)
|
51
|
+
def self.delete(uri, headers={}, options={}, &configure_block)
|
52
|
+
REST::Request.perform(:delete, URI.parse(uri), nil, headers, options, &configure_block)
|
50
53
|
end
|
51
54
|
|
52
55
|
# Performs a PATCH on a resource. See REST::Request.new for a complete discussion of options.
|
@@ -61,8 +64,8 @@ module REST
|
|
61
64
|
# puts "Couldn't rename your pigeon (#{response.status_code})"
|
62
65
|
# puts XML.parse(response.body).reason
|
63
66
|
# end
|
64
|
-
def self.patch(uri, body, headers={}, options={})
|
65
|
-
REST::Request.perform(:patch, URI.parse(uri), body, headers, options)
|
67
|
+
def self.patch(uri, body, headers={}, options={}, &configure_block)
|
68
|
+
REST::Request.perform(:patch, URI.parse(uri), body, headers, options, &configure_block)
|
66
69
|
end
|
67
70
|
|
68
71
|
# Performs a PUT on a resource. See REST::Request.new for a complete discussion of options.
|
@@ -77,8 +80,8 @@ module REST
|
|
77
80
|
# puts "Couldn't replace your pigeon (#{response.status_code})"
|
78
81
|
# puts XML.parse(response.body).reason
|
79
82
|
# end
|
80
|
-
def self.put(uri, body, headers={}, options={})
|
81
|
-
REST::Request.perform(:put, URI.parse(uri), body, headers, options)
|
83
|
+
def self.put(uri, body, headers={}, options={}, &configure_block)
|
84
|
+
REST::Request.perform(:put, URI.parse(uri), body, headers, options, &configure_block)
|
82
85
|
end
|
83
86
|
|
84
87
|
# Performs a POST on a resource. See REST::Request.new for a complete discussion of options.
|
@@ -93,8 +96,8 @@ module REST
|
|
93
96
|
# puts "Couldn't create your pigeon (#{response.status_code})"
|
94
97
|
# puts XML.parse(response.body).reason
|
95
98
|
# end
|
96
|
-
def self.post(uri, body, headers={}, options={})
|
97
|
-
REST::Request.perform(:post, URI.parse(uri), body, headers, options)
|
99
|
+
def self.post(uri, body, headers={}, options={}, &configure_block)
|
100
|
+
REST::Request.perform(:post, URI.parse(uri), body, headers, options, &configure_block)
|
98
101
|
end
|
99
102
|
end
|
100
103
|
|
data/lib/rest/request.rb
CHANGED
@@ -16,9 +16,12 @@ module REST
|
|
16
16
|
# * <tt>tls_verify/verify_ssl</tt>: Verify the server certificate against known CA's
|
17
17
|
# * <tt>tls_ca_file</tt>: Use a specific file for CA certificates instead of the built-in one
|
18
18
|
# this only works when <tt>:tls_verify</tt> is also set.
|
19
|
-
# * <tt>tls_key_and_certificate_file</tt>: The client key and certificate file to use for this
|
19
|
+
# * <tt>tls_key_and_certificate_file</tt>: The client key and certificate file to use for this
|
20
|
+
# request
|
20
21
|
# * <tt>tls_certificate</tt>: The client certficate to use for this request
|
21
22
|
# * <tt>tls_key</tt>: The client private key to use for this request
|
23
|
+
# * <tt>configure_block</tt>: An optional block that yields the underlying <tt>Net::HTTP</tt>
|
24
|
+
# request object allowing for more fine-grained configuration
|
22
25
|
#
|
23
26
|
# == Examples
|
24
27
|
#
|
@@ -32,6 +35,11 @@ module REST
|
|
32
35
|
# {'Accept' => 'application/json, */*', 'Content-Type' => 'application/json; charset=utf-8'}
|
33
36
|
# )
|
34
37
|
#
|
38
|
+
# # Pass a block to configure the underlying +Net::HTTP+ request.
|
39
|
+
# request = REST::Request.new(:get, URI.parse('http://example.com/pigeons/largest')) do |http_request|
|
40
|
+
# http_request.open_timeout = 15 # seconds
|
41
|
+
# end
|
42
|
+
#
|
35
43
|
# == Authentication example
|
36
44
|
#
|
37
45
|
# request = REST::Request.new(:put,
|
@@ -60,12 +68,13 @@ module REST
|
|
60
68
|
# :tls_verify => true,
|
61
69
|
# :tls_ca_file => '/home/alice/keys/example.pem'
|
62
70
|
# })
|
63
|
-
def initialize(verb, url, body=nil, headers={}, options={})
|
71
|
+
def initialize(verb, url, body=nil, headers={}, options={}, &configure_block)
|
64
72
|
@verb = verb
|
65
73
|
@url = url
|
66
74
|
@body = body
|
67
75
|
@headers = headers
|
68
76
|
@options = options
|
77
|
+
@configure_block = configure_block
|
69
78
|
end
|
70
79
|
|
71
80
|
# Returns the path (including the query) for the request
|
@@ -90,42 +99,14 @@ module REST
|
|
90
99
|
end
|
91
100
|
end
|
92
101
|
|
102
|
+
# Configures and returns a new <tt>Net::HTTP</tt> request object
|
93
103
|
def http_request
|
94
104
|
if http_proxy
|
95
|
-
http_proxy.new(url.host, url.port)
|
96
|
-
else
|
97
|
-
Net::HTTP.new(url.host, url.port)
|
98
|
-
end
|
99
|
-
end
|
100
|
-
|
101
|
-
# Performs the actual request and returns a REST::Response object with the response
|
102
|
-
def perform
|
103
|
-
case verb
|
104
|
-
when :get
|
105
|
-
self.request = Net::HTTP::Get.new(path, headers)
|
106
|
-
when :head
|
107
|
-
self.request = Net::HTTP::Head.new(path, headers)
|
108
|
-
when :delete
|
109
|
-
self.request = Net::HTTP::Delete.new(path, headers)
|
110
|
-
when :patch
|
111
|
-
self.request = Net::HTTP::Patch.new(path, headers)
|
112
|
-
self.request.body = body
|
113
|
-
when :put
|
114
|
-
self.request = Net::HTTP::Put.new(path, headers)
|
115
|
-
self.request.body = body
|
116
|
-
when :post
|
117
|
-
self.request = Net::HTTP::Post.new(path, headers)
|
118
|
-
self.request.body = body
|
105
|
+
http_request = http_proxy.new(url.host, url.port)
|
119
106
|
else
|
120
|
-
|
107
|
+
http_request = Net::HTTP.new(url.host, url.port)
|
121
108
|
end
|
122
109
|
|
123
|
-
if options[:username] and options[:password]
|
124
|
-
request.basic_auth(options[:username], options[:password])
|
125
|
-
end
|
126
|
-
|
127
|
-
http_request = http_request()
|
128
|
-
|
129
110
|
# enable SSL/TLS
|
130
111
|
if url.scheme == 'https'
|
131
112
|
require 'net/https'
|
@@ -158,6 +139,50 @@ module REST
|
|
158
139
|
end
|
159
140
|
end
|
160
141
|
|
142
|
+
if @configure_block
|
143
|
+
@configure_block.call(http_request)
|
144
|
+
end
|
145
|
+
|
146
|
+
http_request
|
147
|
+
end
|
148
|
+
|
149
|
+
def request_for_verb
|
150
|
+
case verb
|
151
|
+
when :get
|
152
|
+
Net::HTTP::Get.new(path, headers)
|
153
|
+
when :head
|
154
|
+
Net::HTTP::Head.new(path, headers)
|
155
|
+
when :delete
|
156
|
+
Net::HTTP::Delete.new(path, headers)
|
157
|
+
when :patch
|
158
|
+
if defined?(Net::HTTP::Patch)
|
159
|
+
Net::HTTP::Patch.new(path, headers)
|
160
|
+
else
|
161
|
+
raise ArgumentError, "This version of the Ruby standard library doesn't support PATCH"
|
162
|
+
end
|
163
|
+
when :put
|
164
|
+
Net::HTTP::Put.new(path, headers)
|
165
|
+
when :post
|
166
|
+
Net::HTTP::Post.new(path, headers)
|
167
|
+
else
|
168
|
+
raise ArgumentError, "Unknown HTTP verb `#{verb}'"
|
169
|
+
end
|
170
|
+
end
|
171
|
+
|
172
|
+
# Performs the actual request and returns a REST::Response object with the response
|
173
|
+
def perform
|
174
|
+
self.request = request_for_verb
|
175
|
+
|
176
|
+
if [:patch, :put, :post].include?(verb)
|
177
|
+
request.body = body
|
178
|
+
end
|
179
|
+
|
180
|
+
if options[:username] and options[:password]
|
181
|
+
request.basic_auth(options[:username], options[:password])
|
182
|
+
end
|
183
|
+
|
184
|
+
http_request = http_request()
|
185
|
+
|
161
186
|
begin
|
162
187
|
response = http_request.start { |http| http.request(request) }
|
163
188
|
rescue EOFError => error
|
@@ -169,9 +194,10 @@ module REST
|
|
169
194
|
# Shortcut for REST::Request.new(*args).perform.
|
170
195
|
#
|
171
196
|
# See new for options.
|
172
|
-
def self.perform(*args)
|
173
|
-
request = new(*args)
|
197
|
+
def self.perform(*args, &configure_block)
|
198
|
+
request = new(*args, &configure_block)
|
174
199
|
request.perform
|
175
200
|
end
|
176
201
|
end
|
177
202
|
end
|
203
|
+
|
metadata
CHANGED
@@ -1,43 +1,43 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: nap
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.7.0pre0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Manfred Stienstra
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2014-03-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rake
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- -
|
17
|
+
- - "~>"
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: '
|
19
|
+
version: '10'
|
20
20
|
type: :development
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
|
-
- -
|
24
|
+
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version: '
|
26
|
+
version: '10'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: peck
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
|
-
- -
|
31
|
+
- - "~>"
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version: '0'
|
33
|
+
version: '0.5'
|
34
34
|
type: :development
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
|
-
- -
|
38
|
+
- - "~>"
|
39
39
|
- !ruby/object:Gem::Version
|
40
|
-
version: '0'
|
40
|
+
version: '0.5'
|
41
41
|
description: |2
|
42
42
|
Nap is a really simple REST library. It allows you to perform HTTP requests
|
43
43
|
with minimal amounts of code.
|
@@ -48,33 +48,34 @@ extra_rdoc_files:
|
|
48
48
|
- README.md
|
49
49
|
- LICENSE
|
50
50
|
files:
|
51
|
+
- LICENSE
|
52
|
+
- README.md
|
51
53
|
- lib/rest.rb
|
52
54
|
- lib/rest/request.rb
|
53
55
|
- lib/rest/response.rb
|
54
56
|
- support/cacert.pem
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
licenses: []
|
57
|
+
homepage: https://github.com/Fingertips/nap
|
58
|
+
licenses:
|
59
|
+
- MIT
|
59
60
|
metadata: {}
|
60
61
|
post_install_message:
|
61
62
|
rdoc_options:
|
62
|
-
- --charset=utf-8
|
63
|
+
- "--charset=utf-8"
|
63
64
|
require_paths:
|
64
65
|
- lib
|
65
66
|
required_ruby_version: !ruby/object:Gem::Requirement
|
66
67
|
requirements:
|
67
|
-
- -
|
68
|
+
- - ">="
|
68
69
|
- !ruby/object:Gem::Version
|
69
70
|
version: '0'
|
70
71
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
71
72
|
requirements:
|
72
|
-
- -
|
73
|
+
- - ">"
|
73
74
|
- !ruby/object:Gem::Version
|
74
|
-
version:
|
75
|
+
version: 1.3.1
|
75
76
|
requirements: []
|
76
77
|
rubyforge_project:
|
77
|
-
rubygems_version: 2.0
|
78
|
+
rubygems_version: 2.2.0
|
78
79
|
signing_key:
|
79
80
|
specification_version: 4
|
80
81
|
summary: Nap is a really simple REST library.
|