consumer 0.0.2 → 0.0.3

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: 138a7d9bf5c8905eb3a18364b42c449c3205bf28
4
- data.tar.gz: 61517626ad5f9470832d21a4e08f34c4aa4fabfd
3
+ metadata.gz: 1030e7427ab1393c68f0f03b306b841e237c3eed
4
+ data.tar.gz: 4f6630aae34aac32caee6c9ef0b449233a823b42
5
5
  SHA512:
6
- metadata.gz: 13ce87dd41e85f2299c91652f5dbdf9b475fb4c953fb1801246d238afa7661f482767041f9f100eddf5ce1f11ffcbebc14a2065b2b21e8d74f98b4fbe7c6970b
7
- data.tar.gz: 3793fcf3943ee14c9b1cfc71d32bf6ed37d2f83b34479ffab4200292ab14eb42218785615b1cbea99ad20a258c5b83e22cc5306fa70b318f82cdd1662175b1c0
6
+ metadata.gz: 137ec48b052e6fda33489b30988724b83ec96b423d2e008304854971bab427ff8c02563e34adc4b04bf404c097b0028cdcba49ab7c1abe755402197a99f0ee48
7
+ data.tar.gz: 2a21fb9f0f11365fe65e66bcb02096aa66ce29b735a865179bbf2f1d425514d20449553877d46014ecefe84d53c62f53131ea4dc7cfb530886a740eb83cc46cb
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- consumer (0.0.2)
4
+ consumer (0.0.3)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/README.md CHANGED
@@ -22,6 +22,8 @@ Or install it yourself as:
22
22
  user = Cosumer.get("http://user.api.url")
23
23
  puts user.name
24
24
  puts user.purchases.first.date
25
+ # with hipermidia
26
+ puts user.link("rel_name").link
25
27
  ```
26
28
 
27
29
  ## Contributing
@@ -11,6 +11,14 @@ module Consumer
11
11
  return methodize_me(self[_method])
12
12
  end
13
13
 
14
+ def link(rel)
15
+ _link = self["link"] || self["links"]
16
+ _link = [_link].flatten
17
+ _link = _link.find {|l| l.is_a?(Hash) and l["rel"] == rel }
18
+
19
+ methodize_me(_link)
20
+ end
21
+
14
22
  def respond_to?(method)
15
23
  self.has_key?(method.to_s) or super
16
24
  end
@@ -1,3 +1,3 @@
1
1
  module Consumer
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
@@ -2,44 +2,21 @@ require 'spec_helper'
2
2
 
3
3
  describe Consumer do
4
4
 
5
- let(:hash) do
6
- {
7
- "id" => "0ffe01",
8
- "first_item" => "item 1",
9
- "second" => "item 2",
10
- "third" => 3,
11
- "fourthly" => nil,
12
- "array" => [ {
13
- "item1" => "test 1",
14
- "item2" => "test 2"
15
- },
16
- {
17
- "item3" => "test 3",
18
- "item4" => "test 4"
19
- } ],
20
- "child_hash" => {
21
- "hash1" => "value 1",
22
- "hash2" => "value 2",
23
- "array1" => [1,2,3]
24
- }
25
- }
26
- end
27
-
28
- let(:url) { "http://mock.url/feed_test" }
29
- before { register_uri(:get, url, body: hash.to_json) }
30
- subject { described_class.get(url) }
5
+ let(:url) { "http://mock.url/feed_test" }
6
+ before { register_uri(:get, url, body: fixture("example.json")) }
7
+ subject { described_class.get(url) }
31
8
 
32
- its(:id) { should == "0ffe01" }
33
- its(:first_item) { should == "item 1" }
34
- its(:second) { should == "item 2" }
35
- its(:third) { should == 3 }
36
- its(:fourthly) { should be_nil }
9
+ its(:id) { should == "0ffe01" }
10
+ its(:first_item) { should == "item 1" }
11
+ its(:second) { should == "item 2" }
12
+ its(:third) { should == 3 }
13
+ its(:fourthly) { should be_nil }
37
14
 
38
- it { expect(subject.respond_to?(:id)).to be_truthy }
39
- it { expect(subject.respond_to?(:first_item)).to be_truthy }
40
- it { expect(subject.respond_to?(:second)).to be_truthy }
41
- it { expect(subject.respond_to?(:third)).to be_truthy }
42
- it { expect(subject.respond_to?(:fourthly)).to be_truthy }
15
+ it { expect(subject.respond_to?(:id)).to be_truthy }
16
+ it { expect(subject.respond_to?(:first_item)).to be_truthy }
17
+ it { expect(subject.respond_to?(:second)).to be_truthy }
18
+ it { expect(subject.respond_to?(:third)).to be_truthy }
19
+ it { expect(subject.respond_to?(:fourthly)).to be_truthy }
43
20
 
44
21
  it { expect(subject.array).to be_a Array }
45
22
  it { expect(subject.array.size).to eq(2) }
@@ -70,5 +47,60 @@ describe Consumer do
70
47
  it { expect(subject.respond_to?(:fifth)).to be_falsey }
71
48
  it { expect(subject.respond_to?(:class)).to be_truthy }
72
49
 
73
- it { expect(subject).to eq(hash) }
50
+ it { expect(subject).to eq(JSON.parse(fixture("example.json"))) }
51
+
52
+ describe "#link" do
53
+ shared_examples_for "first item" do
54
+ it { expect(subject.link("interno").rel).to eq("interno") }
55
+ it { expect(subject.link("interno").href).to eq("http://url-interna") }
56
+ it { expect(subject.link("interno").type).to eq("image/jpeg") }
57
+ end
58
+
59
+ shared_examples_for "second item" do
60
+ it { expect(subject.link("externo").rel).to eq("externo") }
61
+ it { expect(subject.link("externo").href).to eq("http://url-externa") }
62
+ it { expect(subject.link("externo").type).to eq("image/jpeg") }
63
+ end
64
+
65
+ let(:example) { JSON.parse(fixture('link.json')) }
66
+
67
+ let(:url) { "http://mock.url/feed_with_link" }
68
+ before { register_uri(:get, url, body: link.to_json) }
69
+ subject { described_class.get(url) }
70
+
71
+ context "when node is links" do
72
+ context "when is an array" do
73
+ let(:link) { {"links" => example } }
74
+ it_behaves_like "first item"
75
+ it_behaves_like "second item"
76
+ end
77
+
78
+ context "when is a hash" do
79
+ let(:link) { {"links" => example.first } }
80
+ it_behaves_like "first item"
81
+ it { expect(subject.link("externo")).to be_nil }
82
+ end
83
+ end
84
+
85
+ context "when node is link" do
86
+ context "when is an array" do
87
+ let(:link) { {"link" => example } }
88
+ it_behaves_like "first item"
89
+ it_behaves_like "second item"
90
+ end
91
+
92
+ context "when is a hash" do
93
+ let(:link) { {"link" => example.first } }
94
+ it_behaves_like "first item"
95
+ it { expect(subject.link("externo")).to be_nil }
96
+ end
97
+ end
98
+
99
+ context "when doesnt exists link node" do
100
+ let(:link) { {} }
101
+
102
+ it { expect(subject.link("interno")).to be_nil }
103
+ it { expect(subject.link("externo")).to be_nil }
104
+ end
105
+ end
74
106
  end
@@ -0,0 +1,26 @@
1
+ {
2
+ "id": "0ffe01",
3
+ "first_item": "item 1",
4
+ "second": "item 2",
5
+ "third": 3,
6
+ "fourthly": null,
7
+ "array": [
8
+ {
9
+ "item1": "test 1",
10
+ "item2": "test 2"
11
+ },
12
+ {
13
+ "item3": "test 3",
14
+ "item4": "test 4"
15
+ }
16
+ ],
17
+ "child_hash": {
18
+ "hash1": "value 1",
19
+ "hash2": "value 2",
20
+ "array1": [
21
+ 1,
22
+ 2,
23
+ 3
24
+ ]
25
+ }
26
+ }
@@ -0,0 +1,12 @@
1
+ [
2
+ {
3
+ "rel": "interno",
4
+ "href": "http://url-interna",
5
+ "type": "image/jpeg"
6
+ },
7
+ {
8
+ "rel": "externo",
9
+ "href": "http://url-externa",
10
+ "type": "image/jpeg"
11
+ }
12
+ ]
data/spec/spec_helper.rb CHANGED
@@ -12,4 +12,8 @@ RSpec.configure do |config|
12
12
  FakeWeb.register_uri(method, uri, options)
13
13
  end
14
14
 
15
+ def fixture(name)
16
+ File.read("#{Dir.pwd}/spec/fixtures/#{name}")
17
+ end
18
+
15
19
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: consumer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nadilson
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-01-22 00:00:00.000000000 Z
11
+ date: 2015-01-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -114,6 +114,8 @@ files:
114
114
  - lib/consumer/proxy.rb
115
115
  - lib/consumer/version.rb
116
116
  - spec/consumer_spec.rb
117
+ - spec/fixtures/example.json
118
+ - spec/fixtures/link.json
117
119
  - spec/spec_helper.rb
118
120
  homepage: ''
119
121
  licenses:
@@ -141,4 +143,6 @@ specification_version: 4
141
143
  summary: Simple gem to consume json api
142
144
  test_files:
143
145
  - spec/consumer_spec.rb
146
+ - spec/fixtures/example.json
147
+ - spec/fixtures/link.json
144
148
  - spec/spec_helper.rb