pluct 0.1.4 → 0.1.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.
- checksums.yaml +4 -4
- data/lib/pluct.rb +1 -0
- data/lib/pluct/helpers/request.rb +7 -6
- data/lib/pluct/link_description_object.rb +24 -0
- data/lib/pluct/resource.rb +12 -23
- data/lib/pluct/version.rb +1 -1
- data/spec/pluct/helpers/request_spec.rb +9 -1
- data/spec/pluct/link_description_object_spec.rb +24 -0
- metadata +4 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 67665aa5ada6f45c24166430f601c7fd6c8c5eb4
|
4
|
+
data.tar.gz: 3834088016a2809b1b7f734a1d22d4ae44354544
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: dfb73b9d7f6d5b4be672821a28f7c033944c788e7700bc8713b35ee8e7e4c1392122e4628c03bd734d3f4bbb0f656a1447b535bf24f6e0577bd445c993d17b30
|
7
|
+
data.tar.gz: c1c6cd9e9922befdc62a8808b597de88dc8439ec133da4024f2597a9a3ae0e90e05c7640515fbc329c8ad5866cadf5953b65f14d5b366187032b084d23c8a60d
|
data/lib/pluct.rb
CHANGED
@@ -8,6 +8,7 @@ module Pluct
|
|
8
8
|
autoload :Errors, "pluct/errors"
|
9
9
|
autoload :Helpers, "pluct/helpers"
|
10
10
|
autoload :JSON, "pluct/extensions/json"
|
11
|
+
autoload :LinkDescriptionObject, "pluct/link_description_object"
|
11
12
|
autoload :Resource, "pluct/resource"
|
12
13
|
autoload :Schema, "pluct/schema"
|
13
14
|
|
@@ -7,12 +7,13 @@ module Pluct
|
|
7
7
|
'content-type' => 'application/json'
|
8
8
|
}
|
9
9
|
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
options =
|
15
|
-
|
10
|
+
protected
|
11
|
+
|
12
|
+
def get(url, data = nil, headers = nil)
|
13
|
+
headers = (headers ? DEFAULT_HEADERS.merge(headers) : DEFAULT_HEADERS)
|
14
|
+
options = headers.dup
|
15
|
+
options.merge!(params: data)
|
16
|
+
RestClient.get(url, options)
|
16
17
|
rescue RestClient::Exception => e
|
17
18
|
raise_exception(url, e)
|
18
19
|
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
module Pluct
|
2
|
+
class LinkDescriptionObject
|
3
|
+
def initialize(raw_link)
|
4
|
+
@href = raw_link["href"]
|
5
|
+
end
|
6
|
+
|
7
|
+
def expand_href(mapping)
|
8
|
+
template.expand(mapping).to_s
|
9
|
+
end
|
10
|
+
|
11
|
+
def unused_mapping(orig_mapping)
|
12
|
+
mapping = orig_mapping.dup
|
13
|
+
expanded_href = self.expand_href(mapping)
|
14
|
+
used_mapping = template.extract(expanded_href)
|
15
|
+
mapping.delete_if { |key, value| used_mapping.include?(key.to_s) }
|
16
|
+
end
|
17
|
+
|
18
|
+
private
|
19
|
+
|
20
|
+
def template
|
21
|
+
@template ||= Addressable::Template.new(@href)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
data/lib/pluct/resource.rb
CHANGED
@@ -1,6 +1,5 @@
|
|
1
|
-
#TODO: Returns Pluct::Response if there is no header, otherwise Pluct::Resource.
|
2
1
|
module Pluct
|
3
|
-
class Resource
|
2
|
+
class Resource
|
4
3
|
include Pluct::Helpers::Request
|
5
4
|
|
6
5
|
attr_reader :data, :response, :schema, :uri
|
@@ -9,7 +8,7 @@ module Pluct
|
|
9
8
|
@uri = uri
|
10
9
|
@response = response || get(@uri)
|
11
10
|
@data ||= (JSON.is_json?(@response.body) ? JSON.parse(@response.body, {symbolize_names: true}) : {})
|
12
|
-
|
11
|
+
|
13
12
|
@schema = Schema.from_header(@response.headers)
|
14
13
|
Resource.create_methods(@schema.links) if @schema
|
15
14
|
end
|
@@ -18,39 +17,29 @@ module Pluct
|
|
18
17
|
@data[method] || super
|
19
18
|
end
|
20
19
|
|
21
|
-
|
20
|
+
private
|
21
|
+
|
22
22
|
def self.create_methods(links=[])
|
23
23
|
links.each do |link|
|
24
|
+
ldo = Pluct::LinkDescriptionObject.new(link)
|
25
|
+
|
24
26
|
define_method link["rel"] do |*args|
|
25
|
-
|
27
|
+
params, *options = *args
|
28
|
+
|
26
29
|
method = link["method"] || "GET"
|
27
|
-
href = link["href"]
|
28
30
|
|
29
|
-
payload = query_string.dup
|
30
31
|
if ['PATCH', 'PUT'].include? method
|
31
|
-
|
32
|
+
params.merge!(@data)
|
32
33
|
end
|
33
34
|
|
34
|
-
uri =
|
35
|
-
payload =
|
35
|
+
uri = ldo.expand_href(params)
|
36
|
+
payload = ldo.unused_mapping(params)
|
36
37
|
options.unshift(payload)
|
37
38
|
|
38
39
|
response = send(method.downcase, uri, *options)
|
39
40
|
Resource.new(uri, response)
|
40
|
-
end
|
41
|
+
end
|
41
42
|
end
|
42
43
|
end
|
43
|
-
|
44
|
-
def define_request_uri(uri_template, query_string)
|
45
|
-
template = Addressable::Template.new(uri_template)
|
46
|
-
Addressable::URI.parse(template.expand(query_string)).to_s
|
47
|
-
end
|
48
|
-
|
49
|
-
def define_request_payload(uri_template, href, payload)
|
50
|
-
template = Addressable::Template.new(uri_template)
|
51
|
-
uri_template = template.extract(href)
|
52
|
-
|
53
|
-
payload.delete_if{ |key, value| uri_template.include?(key) }
|
54
|
-
end
|
55
44
|
end
|
56
45
|
end
|
data/lib/pluct/version.rb
CHANGED
@@ -19,7 +19,7 @@ describe Pluct::Helpers::Request do
|
|
19
19
|
end
|
20
20
|
end
|
21
21
|
|
22
|
-
|
22
|
+
it 'returns 200 for valid request' do
|
23
23
|
body = File.read('spec/assets/user.json')
|
24
24
|
stub_request(:get, 'http://www.example.com/success').to_return(body: body, status: 200)
|
25
25
|
response = client.send(:get, 'http://www.example.com/success')
|
@@ -28,4 +28,12 @@ describe Pluct::Helpers::Request do
|
|
28
28
|
expect(response.code).to eq 200
|
29
29
|
expect(response.body).to eq body
|
30
30
|
end
|
31
|
+
|
32
|
+
describe "#get" do
|
33
|
+
it "should GET resource" do
|
34
|
+
RestClient.should_receive(:get).with(
|
35
|
+
"http://example.org", {params: {key: "value"}, "content-type" => "application/json"})
|
36
|
+
client.send(:get, "http://example.org", {key: "value"})
|
37
|
+
end
|
38
|
+
end
|
31
39
|
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe Pluct::LinkDescriptionObject do
|
4
|
+
describe "#expand_href" do
|
5
|
+
let(:raw_link) { {"href" => "http://example.org/{variable}", "rel" => "my-rel"} }
|
6
|
+
subject(:ldo) { Pluct::LinkDescriptionObject.new(raw_link) }
|
7
|
+
specify { expect(ldo.expand_href({variable: "my-variable"})).to eq("http://example.org/my-variable") }
|
8
|
+
end
|
9
|
+
|
10
|
+
describe "#unused_mapping" do
|
11
|
+
let(:raw_link) { {"href" => "http://example.org/{variable}", "rel" => "my-rel"} }
|
12
|
+
let(:mapping) { {variable: "my-variable", unused_variable: "my-unused-variable"} }
|
13
|
+
subject(:ldo) { Pluct::LinkDescriptionObject.new(raw_link) }
|
14
|
+
|
15
|
+
it "should return unused mapping" do
|
16
|
+
expect(ldo.unused_mapping(mapping)).to eq({unused_variable: "my-unused-variable"})
|
17
|
+
end
|
18
|
+
|
19
|
+
it "should not change original mapping" do
|
20
|
+
ldo.unused_mapping(mapping)
|
21
|
+
expect(mapping).to eq({variable: "my-variable", unused_variable: "my-unused-variable"})
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: pluct
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Alberto Leal
|
@@ -168,6 +168,7 @@ files:
|
|
168
168
|
- lib/pluct/extensions/json.rb
|
169
169
|
- lib/pluct/helpers.rb
|
170
170
|
- lib/pluct/helpers/request.rb
|
171
|
+
- lib/pluct/link_description_object.rb
|
171
172
|
- lib/pluct/resource.rb
|
172
173
|
- lib/pluct/schema.rb
|
173
174
|
- lib/pluct/version.rb
|
@@ -177,6 +178,7 @@ files:
|
|
177
178
|
- spec/integration/resource_spec.rb
|
178
179
|
- spec/pluct/extensions/json_spec.rb
|
179
180
|
- spec/pluct/helpers/request_spec.rb
|
181
|
+
- spec/pluct/link_description_object_spec.rb
|
180
182
|
- spec/pluct/resource_spec.rb
|
181
183
|
- spec/pluct/schema_spec.rb
|
182
184
|
- spec/spec_helper.rb
|
@@ -215,6 +217,7 @@ test_files:
|
|
215
217
|
- spec/integration/resource_spec.rb
|
216
218
|
- spec/pluct/extensions/json_spec.rb
|
217
219
|
- spec/pluct/helpers/request_spec.rb
|
220
|
+
- spec/pluct/link_description_object_spec.rb
|
218
221
|
- spec/pluct/resource_spec.rb
|
219
222
|
- spec/pluct/schema_spec.rb
|
220
223
|
- spec/spec_helper.rb
|