http_magic 0.1.1 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +2 -0
- data/Gemfile.lock +7 -7
- data/http_magic.gemspec +1 -1
- data/lib/http_magic/api.rb +55 -0
- data/lib/http_magic/request.rb +54 -10
- data/test/api/delete_test.rb +21 -0
- data/test/api/put_test.rb +34 -0
- data/test/request_test.rb +27 -1
- metadata +21 -7
- checksums.yaml +0 -7
data/.gitignore
CHANGED
data/Gemfile.lock
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
http_magic (0.1.
|
4
|
+
http_magic (0.1.1)
|
5
5
|
|
6
6
|
GEM
|
7
7
|
remote: https://rubygems.org/
|
@@ -26,7 +26,7 @@ GEM
|
|
26
26
|
pry-debugger (0.2.2)
|
27
27
|
debugger (~> 1.3)
|
28
28
|
pry (~> 0.9.10)
|
29
|
-
rake (
|
29
|
+
rake (0.9.6)
|
30
30
|
safe_yaml (0.9.7)
|
31
31
|
slop (3.4.7)
|
32
32
|
webmock (1.16.1)
|
@@ -38,8 +38,8 @@ PLATFORMS
|
|
38
38
|
|
39
39
|
DEPENDENCIES
|
40
40
|
http_magic!
|
41
|
-
minitest (~> 4.7
|
42
|
-
pry
|
43
|
-
pry-debugger
|
44
|
-
rake
|
45
|
-
webmock (~> 1.16
|
41
|
+
minitest (~> 4.7)
|
42
|
+
pry (~> 0)
|
43
|
+
pry-debugger (~> 0)
|
44
|
+
rake (~> 0)
|
45
|
+
webmock (~> 1.16)
|
data/http_magic.gemspec
CHANGED
data/lib/http_magic/api.rb
CHANGED
@@ -137,6 +137,26 @@ module HttpMagic
|
|
137
137
|
self
|
138
138
|
end
|
139
139
|
|
140
|
+
# Deletes a resource from the URI.
|
141
|
+
#
|
142
|
+
# Assuming an api where each resource is namespaced with 'api/v1' and where
|
143
|
+
# the url http://www.example.com/api/v1/foo/99 references a specific
|
144
|
+
# resource.
|
145
|
+
#
|
146
|
+
# == Example
|
147
|
+
#
|
148
|
+
# class ExampleApi < HttpMagic::Api
|
149
|
+
# url 'www.example.com'
|
150
|
+
# namespace 'api/v1'
|
151
|
+
# end
|
152
|
+
#
|
153
|
+
# ExampleApi.foo[99].delete
|
154
|
+
# => ""
|
155
|
+
def delete
|
156
|
+
request = Request.new(@uri, headers: @headers)
|
157
|
+
request.delete
|
158
|
+
end
|
159
|
+
|
140
160
|
# Gets a resource from the URI and returns it based on its content type.
|
141
161
|
# JSON content will be parsed and returned as equivalent Ruby objects. All
|
142
162
|
# other content types will be returned as text.
|
@@ -205,6 +225,41 @@ module HttpMagic
|
|
205
225
|
request.post
|
206
226
|
end
|
207
227
|
|
228
|
+
# PUT's a resource to the URI and returns the result based on its content
|
229
|
+
# type. JSON content will be parsed and returned as equivalent Ruby objects.
|
230
|
+
# All other content types will be returned as text.
|
231
|
+
#
|
232
|
+
# Assuming an api where each resource is namespaced with 'api/v1' and where
|
233
|
+
# a GET to the url http://www.example.com/api/v1/foo/99 responds with the
|
234
|
+
# following.
|
235
|
+
#
|
236
|
+
# Header:
|
237
|
+
#
|
238
|
+
# Content-Type: application/json
|
239
|
+
#
|
240
|
+
# Body:
|
241
|
+
#
|
242
|
+
# {
|
243
|
+
# "name": "Foo"
|
244
|
+
# }
|
245
|
+
#
|
246
|
+
# == Example
|
247
|
+
#
|
248
|
+
# class ExampleApi < HttpMagic::Api
|
249
|
+
# url 'www.example.com'
|
250
|
+
# namespace 'api/v1'
|
251
|
+
# end
|
252
|
+
#
|
253
|
+
# ExampleApi.foo[99].put(name: 'Changed Foo')
|
254
|
+
# => { "name" => "Changed Foo" }
|
255
|
+
def put(data = {})
|
256
|
+
request = Request.new(@uri,
|
257
|
+
headers: @headers,
|
258
|
+
data: data,
|
259
|
+
)
|
260
|
+
request.put
|
261
|
+
end
|
262
|
+
|
208
263
|
# Instance scoped method_missing that accumulates the URN parts used in
|
209
264
|
# forming the URI. It returns a reference to the current instance so that
|
210
265
|
# method and [] based parts can be chained together to from the URN. In the
|
data/lib/http_magic/request.rb
CHANGED
@@ -23,8 +23,24 @@ module HttpMagic
|
|
23
23
|
}
|
24
24
|
end
|
25
25
|
|
26
|
+
# Makes a DELETE request to the url provided by the Uri object and returns
|
27
|
+
# the response parsed according to the content type specified in the
|
28
|
+
# repsonse.
|
29
|
+
#
|
30
|
+
# == Example
|
31
|
+
#
|
32
|
+
# uri_object = HttpMagic::Uri.new('http://example.com')
|
33
|
+
#
|
34
|
+
# request = Request.new(uri_object)
|
35
|
+
#
|
36
|
+
# request.delete
|
37
|
+
# => { 'name' => 'Foo Bar' }
|
38
|
+
def delete
|
39
|
+
parse_response http.delete(@uri.urn, @options[:headers])
|
40
|
+
end
|
41
|
+
|
26
42
|
# Makes a GET request to the url provided by the Uri object and returns the
|
27
|
-
#
|
43
|
+
# response parsed according to the content type specified in the repsonse.
|
28
44
|
#
|
29
45
|
# == Example
|
30
46
|
#
|
@@ -35,12 +51,13 @@ module HttpMagic
|
|
35
51
|
# request.get
|
36
52
|
# => { 'name' => 'Foo Bar' }
|
37
53
|
def get
|
38
|
-
|
54
|
+
parse_response http.request_get(@uri.urn, @options[:headers])
|
39
55
|
end
|
40
56
|
|
41
57
|
# Makes a POST request to the url provided by the Uri object and returns the
|
42
|
-
#
|
43
|
-
# initialization parameter, then that is
|
58
|
+
# response parsed according to the content type specified in the repsonse.
|
59
|
+
# If data was provided as an optional initialization parameter, then that is
|
60
|
+
# also POSTed.
|
44
61
|
#
|
45
62
|
# == Example
|
46
63
|
#
|
@@ -51,10 +68,38 @@ module HttpMagic
|
|
51
68
|
# request.post
|
52
69
|
def post
|
53
70
|
if !@data.empty?
|
54
|
-
@options[:headers].merge!( '
|
71
|
+
@options[:headers].merge!( 'Content-Type' => 'application/json' )
|
72
|
+
end
|
73
|
+
|
74
|
+
parse_response http.request_post(
|
75
|
+
@uri.urn,
|
76
|
+
@data.to_json,
|
77
|
+
@options[:headers]
|
78
|
+
)
|
79
|
+
end
|
80
|
+
|
81
|
+
# Makes a PUT request to the url provided by the Uri object and returns the
|
82
|
+
# response parsed according to the content type specified in the repsonse.
|
83
|
+
# If data was provided as an optional initialization parameter, then that is
|
84
|
+
# also PUTed.
|
85
|
+
#
|
86
|
+
# == Example
|
87
|
+
#
|
88
|
+
# uri_object = HttpMagic::Uri.new('http://example.com')
|
89
|
+
#
|
90
|
+
# request = Request.new(uri_object, data: { name: 'New Foo' })
|
91
|
+
#
|
92
|
+
# request.put
|
93
|
+
def put
|
94
|
+
if !@data.empty?
|
95
|
+
@options[:headers].merge!( 'Content-Type' => 'application/json' )
|
55
96
|
end
|
56
97
|
|
57
|
-
|
98
|
+
parse_response http.request_put(
|
99
|
+
@uri.urn,
|
100
|
+
@data.to_json,
|
101
|
+
@options[:headers]
|
102
|
+
)
|
58
103
|
end
|
59
104
|
|
60
105
|
private
|
@@ -66,13 +111,12 @@ module HttpMagic
|
|
66
111
|
@http
|
67
112
|
end
|
68
113
|
|
69
|
-
def
|
70
|
-
response = http.send_request(method, @uri.urn, @data.to_json, @options[:headers])
|
114
|
+
def parse_response(response)
|
71
115
|
if response && response.is_a?(Net::HTTPSuccess)
|
72
|
-
if response.content_type == 'application/json'
|
116
|
+
if response.body && response.content_type == 'application/json'
|
73
117
|
JSON.parse(response.body)
|
74
118
|
else
|
75
|
-
response.body
|
119
|
+
response.body.to_s
|
76
120
|
end
|
77
121
|
else
|
78
122
|
nil
|
@@ -0,0 +1,21 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
require 'http_magic'
|
3
|
+
|
4
|
+
class HttpMagicTest < HttpMagic::Api
|
5
|
+
end
|
6
|
+
|
7
|
+
describe 'HttpMagic#delete' do
|
8
|
+
before do
|
9
|
+
@url = HttpMagicTest.url 'www.example.com'
|
10
|
+
HttpMagicTest.namespace nil
|
11
|
+
HttpMagicTest.headers nil
|
12
|
+
end
|
13
|
+
|
14
|
+
it 'should send DELETE request' do
|
15
|
+
stub_delete = stub_request(:delete, "https://#{@url}")
|
16
|
+
|
17
|
+
HttpMagicTest.delete
|
18
|
+
assert_requested stub_delete
|
19
|
+
end
|
20
|
+
|
21
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
require 'http_magic'
|
3
|
+
|
4
|
+
class HttpMagicTest < HttpMagic::Api
|
5
|
+
end
|
6
|
+
|
7
|
+
describe 'HttpMagic#put' do
|
8
|
+
before do
|
9
|
+
@url = HttpMagicTest.url 'www.example.com'
|
10
|
+
HttpMagicTest.namespace nil
|
11
|
+
HttpMagicTest.headers nil
|
12
|
+
end
|
13
|
+
|
14
|
+
it 'should send PUT request' do
|
15
|
+
stub_put = stub_request(:put, "https://#{@url}")
|
16
|
+
|
17
|
+
HttpMagicTest.put
|
18
|
+
assert_requested stub_put
|
19
|
+
end
|
20
|
+
|
21
|
+
it 'should send data with PUT request' do
|
22
|
+
expected_data = {
|
23
|
+
apple: 'sauce',
|
24
|
+
banana: 'bread'
|
25
|
+
}
|
26
|
+
stub_put = stub_request(:put, "https://#{@url}/foo").with(
|
27
|
+
body: expected_data
|
28
|
+
)
|
29
|
+
|
30
|
+
HttpMagicTest.foo.put(expected_data)
|
31
|
+
|
32
|
+
assert_requested stub_put
|
33
|
+
end
|
34
|
+
end
|
data/test/request_test.rb
CHANGED
@@ -32,6 +32,16 @@ describe 'HttpMagic::Request' do
|
|
32
32
|
assert_equal content, request.get
|
33
33
|
end
|
34
34
|
|
35
|
+
it 'should be able to delete a resource' do
|
36
|
+
stub_request = stub_request(:delete, "https://#{@domain}").
|
37
|
+
to_return(status: 204)
|
38
|
+
|
39
|
+
request = HttpMagic::Request.new(@uri)
|
40
|
+
request.delete
|
41
|
+
|
42
|
+
assert_requested stub_request
|
43
|
+
end
|
44
|
+
|
35
45
|
it 'should be able to post data as hash' do
|
36
46
|
expected_data = {
|
37
47
|
apple: 'crispy',
|
@@ -39,7 +49,7 @@ describe 'HttpMagic::Request' do
|
|
39
49
|
}
|
40
50
|
stub_request = stub_request(:post, "https://#{@domain}").with(
|
41
51
|
body: expected_data.to_json,
|
42
|
-
headers: { '
|
52
|
+
headers: { 'Content-Type' => 'application/json' }
|
43
53
|
)
|
44
54
|
|
45
55
|
request = HttpMagic::Request.new(@uri, data: expected_data)
|
@@ -48,6 +58,22 @@ describe 'HttpMagic::Request' do
|
|
48
58
|
assert_requested stub_request
|
49
59
|
end
|
50
60
|
|
61
|
+
it 'should be able to put data as hash' do
|
62
|
+
expected_data = {
|
63
|
+
apple: 'crispy',
|
64
|
+
banana: 'soft'
|
65
|
+
}
|
66
|
+
stub_request = stub_request(:put, "https://#{@domain}").with(
|
67
|
+
body: expected_data.to_json,
|
68
|
+
headers: { 'Content-Type' => 'application/json' }
|
69
|
+
)
|
70
|
+
|
71
|
+
request = HttpMagic::Request.new(@uri, data: expected_data)
|
72
|
+
request.put
|
73
|
+
|
74
|
+
assert_requested stub_request
|
75
|
+
end
|
76
|
+
|
51
77
|
it 'should set correct headers' do
|
52
78
|
stub_request = stub_request(:get, "https://#{@domain}").
|
53
79
|
with(headers: { 'X-AuthToken' => 'test_token' })
|
metadata
CHANGED
@@ -1,18 +1,20 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: http_magic
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.2
|
5
|
+
prerelease:
|
5
6
|
platform: ruby
|
6
7
|
authors:
|
7
8
|
- GradesFirst
|
8
9
|
autorequire:
|
9
10
|
bindir: bin
|
10
11
|
cert_chain: []
|
11
|
-
date: 2014-
|
12
|
+
date: 2014-03-10 00:00:00.000000000 Z
|
12
13
|
dependencies:
|
13
14
|
- !ruby/object:Gem::Dependency
|
14
15
|
name: rake
|
15
16
|
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
16
18
|
requirements:
|
17
19
|
- - ~>
|
18
20
|
- !ruby/object:Gem::Version
|
@@ -20,6 +22,7 @@ dependencies:
|
|
20
22
|
type: :development
|
21
23
|
prerelease: false
|
22
24
|
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
23
26
|
requirements:
|
24
27
|
- - ~>
|
25
28
|
- !ruby/object:Gem::Version
|
@@ -27,6 +30,7 @@ dependencies:
|
|
27
30
|
- !ruby/object:Gem::Dependency
|
28
31
|
name: minitest
|
29
32
|
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
30
34
|
requirements:
|
31
35
|
- - ~>
|
32
36
|
- !ruby/object:Gem::Version
|
@@ -34,6 +38,7 @@ dependencies:
|
|
34
38
|
type: :development
|
35
39
|
prerelease: false
|
36
40
|
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
37
42
|
requirements:
|
38
43
|
- - ~>
|
39
44
|
- !ruby/object:Gem::Version
|
@@ -41,6 +46,7 @@ dependencies:
|
|
41
46
|
- !ruby/object:Gem::Dependency
|
42
47
|
name: webmock
|
43
48
|
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
44
50
|
requirements:
|
45
51
|
- - ~>
|
46
52
|
- !ruby/object:Gem::Version
|
@@ -48,6 +54,7 @@ dependencies:
|
|
48
54
|
type: :development
|
49
55
|
prerelease: false
|
50
56
|
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
51
58
|
requirements:
|
52
59
|
- - ~>
|
53
60
|
- !ruby/object:Gem::Version
|
@@ -55,6 +62,7 @@ dependencies:
|
|
55
62
|
- !ruby/object:Gem::Dependency
|
56
63
|
name: pry
|
57
64
|
requirement: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
58
66
|
requirements:
|
59
67
|
- - ~>
|
60
68
|
- !ruby/object:Gem::Version
|
@@ -62,6 +70,7 @@ dependencies:
|
|
62
70
|
type: :development
|
63
71
|
prerelease: false
|
64
72
|
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
65
74
|
requirements:
|
66
75
|
- - ~>
|
67
76
|
- !ruby/object:Gem::Version
|
@@ -69,6 +78,7 @@ dependencies:
|
|
69
78
|
- !ruby/object:Gem::Dependency
|
70
79
|
name: pry-debugger
|
71
80
|
requirement: !ruby/object:Gem::Requirement
|
81
|
+
none: false
|
72
82
|
requirements:
|
73
83
|
- - ~>
|
74
84
|
- !ruby/object:Gem::Version
|
@@ -76,6 +86,7 @@ dependencies:
|
|
76
86
|
type: :development
|
77
87
|
prerelease: false
|
78
88
|
version_requirements: !ruby/object:Gem::Requirement
|
89
|
+
none: false
|
79
90
|
requirements:
|
80
91
|
- - ~>
|
81
92
|
- !ruby/object:Gem::Version
|
@@ -97,8 +108,10 @@ files:
|
|
97
108
|
- lib/http_magic/api.rb
|
98
109
|
- lib/http_magic/request.rb
|
99
110
|
- lib/http_magic/uri.rb
|
111
|
+
- test/api/delete_test.rb
|
100
112
|
- test/api/get_test.rb
|
101
113
|
- test/api/post_test.rb
|
114
|
+
- test/api/put_test.rb
|
102
115
|
- test/fixtures/projects.json
|
103
116
|
- test/request_test.rb
|
104
117
|
- test/test_helper.rb
|
@@ -106,25 +119,26 @@ files:
|
|
106
119
|
homepage: https://github.com/Thoughtwright-LLC/httpmagic
|
107
120
|
licenses:
|
108
121
|
- MIT
|
109
|
-
metadata: {}
|
110
122
|
post_install_message:
|
111
123
|
rdoc_options: []
|
112
124
|
require_paths:
|
113
125
|
- lib
|
114
126
|
required_ruby_version: !ruby/object:Gem::Requirement
|
127
|
+
none: false
|
115
128
|
requirements:
|
116
|
-
- - '>='
|
129
|
+
- - ! '>='
|
117
130
|
- !ruby/object:Gem::Version
|
118
131
|
version: '0'
|
119
132
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
133
|
+
none: false
|
120
134
|
requirements:
|
121
|
-
- - '>='
|
135
|
+
- - ! '>='
|
122
136
|
- !ruby/object:Gem::Version
|
123
137
|
version: '0'
|
124
138
|
requirements: []
|
125
139
|
rubyforge_project:
|
126
|
-
rubygems_version:
|
140
|
+
rubygems_version: 1.8.24
|
127
141
|
signing_key:
|
128
|
-
specification_version:
|
142
|
+
specification_version: 3
|
129
143
|
summary: Provides a more Object Oriented interface to RESTful apis.
|
130
144
|
test_files: []
|
checksums.yaml
DELETED
@@ -1,7 +0,0 @@
|
|
1
|
-
---
|
2
|
-
SHA1:
|
3
|
-
metadata.gz: ae2f80540b2ebc639265dd8a2b4afffa39b72f27
|
4
|
-
data.tar.gz: 38ed7512bca5927a8961ce9279e85b61521902ad
|
5
|
-
SHA512:
|
6
|
-
metadata.gz: 0b9c6a898d4172ef774bc79e46a8b714d137270a0022bb3cf09f97d68b6b02e180d151b4d736bd9884b9e010cf8755b9e39c054a9ac95d82584b4f6b1fe4ae84
|
7
|
-
data.tar.gz: 53da0b3b6cdf245d2afcaefd1e0f8c3ab3a4e6522d3e344a1585cf14364a55c774d7ee4492e861bf4f4bb4c62d34b3a8c1d7dae6cc235e80ef2750329b3e9a99
|