pluct 0.1.4 → 0.1.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 632ce7f4bf78bf8c5ae4c5dd973767dbf6c9611d
4
- data.tar.gz: 8f26330cfc0b1f0019748397aa41e77eed82ea4e
3
+ metadata.gz: 67665aa5ada6f45c24166430f601c7fd6c8c5eb4
4
+ data.tar.gz: 3834088016a2809b1b7f734a1d22d4ae44354544
5
5
  SHA512:
6
- metadata.gz: a452415d7153ee4e98c32d5f948602269422b5a70fe27f184d2a2acc0e29a2b0dba4e0f4de604b0501df8f63b3c4d2167b61140ab05e3da8ed80bb4d6ad94a30
7
- data.tar.gz: dd3a30db0d336890c2d8fb235fd739b55cfefef0b52a57bee4f91ff1c6e42b95d75f64c5897c9c4b62cd2883c18ad31445f07fd24aea69ad1c8934c2e6adf79f
6
+ metadata.gz: dfb73b9d7f6d5b4be672821a28f7c033944c788e7700bc8713b35ee8e7e4c1392122e4628c03bd734d3f4bbb0f656a1447b535bf24f6e0577bd445c993d17b30
7
+ data.tar.gz: c1c6cd9e9922befdc62a8808b597de88dc8439ec133da4024f2597a9a3ae0e90e05c7640515fbc329c8ad5866cadf5953b65f14d5b366187032b084d23c8a60d
@@ -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
- protected
11
- def get(url, *opts)
12
- options = Hash[opts] if opts
13
- resource = RestClient::Resource.new(url)
14
- options = (options ? DEFAULT_HEADERS.merge(options) : DEFAULT_HEADERS)
15
- resource.get(options)
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
@@ -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
- private
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
- query_string, *options = *args
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
- query_string.merge!(@data)
32
+ params.merge!(@data)
32
33
  end
33
34
 
34
- uri = define_request_uri(href, query_string)
35
- payload = define_request_payload(href, uri, 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
@@ -1,3 +1,3 @@
1
1
  module Pluct
2
- VERSION = "0.1.4"
2
+ VERSION = "0.1.5"
3
3
  end
@@ -19,7 +19,7 @@ describe Pluct::Helpers::Request do
19
19
  end
20
20
  end
21
21
 
22
- it 'returns 200 for valid request' do
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
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