consumer 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 138a7d9bf5c8905eb3a18364b42c449c3205bf28
4
+ data.tar.gz: 61517626ad5f9470832d21a4e08f34c4aa4fabfd
5
+ SHA512:
6
+ metadata.gz: 13ce87dd41e85f2299c91652f5dbdf9b475fb4c953fb1801246d238afa7661f482767041f9f100eddf5ce1f11ffcbebc14a2065b2b21e8d74f98b4fbe7c6970b
7
+ data.tar.gz: 3793fcf3943ee14c9b1cfc71d32bf6ed37d2f83b34479ffab4200292ab14eb42218785615b1cbea99ad20a258c5b83e22cc5306fa70b318f82cdd1662175b1c0
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ .bundle/
2
+ coverage/
3
+ tmp/
4
+ pkg/
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.0
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in consumer.gemspec
4
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,45 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ consumer (0.0.2)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ byebug (3.5.1)
10
+ columnize (~> 0.8)
11
+ debugger-linecache (~> 1.2)
12
+ slop (~> 3.6)
13
+ columnize (0.9.0)
14
+ debugger-linecache (1.2.0)
15
+ diff-lcs (1.2.5)
16
+ fakeweb (1.3.0)
17
+ rake (10.4.2)
18
+ rspec (3.1.0)
19
+ rspec-core (~> 3.1.0)
20
+ rspec-expectations (~> 3.1.0)
21
+ rspec-mocks (~> 3.1.0)
22
+ rspec-core (3.1.7)
23
+ rspec-support (~> 3.1.0)
24
+ rspec-expectations (3.1.2)
25
+ diff-lcs (>= 1.2.0, < 2.0)
26
+ rspec-support (~> 3.1.0)
27
+ rspec-its (1.1.0)
28
+ rspec-core (>= 3.0.0)
29
+ rspec-expectations (>= 3.0.0)
30
+ rspec-mocks (3.1.3)
31
+ rspec-support (~> 3.1.0)
32
+ rspec-support (3.1.2)
33
+ slop (3.6.0)
34
+
35
+ PLATFORMS
36
+ ruby
37
+
38
+ DEPENDENCIES
39
+ bundler (~> 1.7)
40
+ byebug
41
+ consumer!
42
+ fakeweb
43
+ rake (~> 10.0)
44
+ rspec (~> 3.1.0)
45
+ rspec-its
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Nadilson
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,33 @@
1
+ # Consumer
2
+
3
+ Simple gem to consumer json API by HTTP GET method
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'consumer'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+ Or install it yourself as:
17
+
18
+ $ gem install consumer
19
+
20
+ ## Usage
21
+ ```
22
+ user = Cosumer.get("http://user.api.url")
23
+ puts user.name
24
+ puts user.purchases.first.date
25
+ ```
26
+
27
+ ## Contributing
28
+
29
+ 1. Fork it ( https://github.com/[my-github-username]/consumer/fork )
30
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
31
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
32
+ 4. Push to the branch (`git push origin my-new-feature`)
33
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,7 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
7
+
data/consumer.gemspec ADDED
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'consumer/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "consumer"
8
+ spec.version = Consumer::VERSION
9
+ spec.authors = ["Nadilson"]
10
+ spec.email = ["nadilsons@gmail.com"]
11
+ spec.summary = %q{Simple gem to consume json api}
12
+ spec.description = ""
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler" , "~> 1.7"
22
+ spec.add_development_dependency "rake" , "~> 10.0"
23
+ spec.add_development_dependency "rspec" , "~> 3.1.0"
24
+ spec.add_development_dependency "rspec-its"
25
+ spec.add_development_dependency "byebug"
26
+ spec.add_development_dependency "fakeweb"
27
+ end
@@ -0,0 +1,29 @@
1
+ module Consumer
2
+ class Proxy < Hash
3
+
4
+ def initialize(hash)
5
+ hash.each { |k, v| self[k] = v }
6
+ end
7
+
8
+ def method_missing(method, *args, &block)
9
+ _method = method.to_s
10
+ super if args.any? or not respond_to?(_method)
11
+ return methodize_me(self[_method])
12
+ end
13
+
14
+ def respond_to?(method)
15
+ self.has_key?(method.to_s) or super
16
+ end
17
+
18
+ private
19
+ def methodize_me(obj)
20
+ if obj.is_a? Hash
21
+ Consumer::Proxy.new(obj)
22
+ elsif obj.is_a? Array
23
+ obj.map { |i| methodize_me(i) }
24
+ else
25
+ obj
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,3 @@
1
+ module Consumer
2
+ VERSION = "0.0.2"
3
+ end
data/lib/consumer.rb ADDED
@@ -0,0 +1,14 @@
1
+ require "consumer/version"
2
+ require "consumer/proxy"
3
+ require 'open-uri'
4
+ require 'json'
5
+
6
+ module Consumer
7
+
8
+ def self.get(url)
9
+ json = open(url).read
10
+ hash = JSON.parse(json)
11
+ Consumer::Proxy.new(hash)
12
+ end
13
+
14
+ end
@@ -0,0 +1,74 @@
1
+ require 'spec_helper'
2
+
3
+ describe Consumer do
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) }
31
+
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 }
37
+
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 }
43
+
44
+ it { expect(subject.array).to be_a Array }
45
+ it { expect(subject.array.size).to eq(2) }
46
+ it { expect(subject.array[0].item1).to eq("test 1") }
47
+ it { expect(subject.array[0].item2).to eq("test 2") }
48
+ it { expect(subject.array[1].item3).to eq("test 3") }
49
+ it { expect(subject.array[1].item4).to eq("test 4") }
50
+
51
+ it { expect(subject.array[0].respond_to?(:item1)).to be_truthy }
52
+ it { expect(subject.array[0].respond_to?(:item2)).to be_truthy }
53
+ it { expect(subject.array[1].respond_to?(:item3)).to be_truthy }
54
+ it { expect(subject.array[1].respond_to?(:item4)).to be_truthy }
55
+
56
+ it { expect(subject.child_hash.hash1).to eq("value 1") }
57
+ it { expect(subject.child_hash.hash2).to eq("value 2") }
58
+ it { expect(subject.child_hash.keys).to eq(["hash1", "hash2", "array1"]) }
59
+ it { expect(subject.child_hash.values).to eq(["value 1", "value 2", [1,2,3]]) }
60
+ it { expect(subject.child_hash.array1).to eq([1,2,3]) }
61
+
62
+ it { expect(subject.respond_to?(:child_hash)).to be_truthy }
63
+ it { expect(subject.child_hash.respond_to?(:hash1)).to be_truthy }
64
+ it { expect(subject.child_hash.respond_to?(:hash2)).to be_truthy }
65
+ it { expect(subject.child_hash.respond_to?(:array1)).to be_truthy }
66
+
67
+ it { expect{ subject.fifth }.to raise_error(NoMethodError) }
68
+ it { expect{ subject.first = 11 }.to raise_error(NoMethodError) }
69
+
70
+ it { expect(subject.respond_to?(:fifth)).to be_falsey }
71
+ it { expect(subject.respond_to?(:class)).to be_truthy }
72
+
73
+ it { expect(subject).to eq(hash) }
74
+ end
@@ -0,0 +1,15 @@
1
+ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
2
+ require 'consumer'
3
+ require 'fakeweb'
4
+ require 'json'
5
+ require 'rspec/its'
6
+ require 'byebug'
7
+
8
+ RSpec.configure do |config|
9
+
10
+ def register_uri(method, uri, options = {})
11
+ options.merge!(:content_type => "application/json; charset=utf-8") if options[:content_type].nil?
12
+ FakeWeb.register_uri(method, uri, options)
13
+ end
14
+
15
+ end
metadata ADDED
@@ -0,0 +1,144 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: consumer
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Nadilson
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-01-22 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.7'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.7'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 3.1.0
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 3.1.0
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec-its
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: byebug
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: fakeweb
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ description: ''
98
+ email:
99
+ - nadilsons@gmail.com
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - ".gitignore"
105
+ - ".rspec"
106
+ - ".travis.yml"
107
+ - Gemfile
108
+ - Gemfile.lock
109
+ - LICENSE.txt
110
+ - README.md
111
+ - Rakefile
112
+ - consumer.gemspec
113
+ - lib/consumer.rb
114
+ - lib/consumer/proxy.rb
115
+ - lib/consumer/version.rb
116
+ - spec/consumer_spec.rb
117
+ - spec/spec_helper.rb
118
+ homepage: ''
119
+ licenses:
120
+ - MIT
121
+ metadata: {}
122
+ post_install_message:
123
+ rdoc_options: []
124
+ require_paths:
125
+ - lib
126
+ required_ruby_version: !ruby/object:Gem::Requirement
127
+ requirements:
128
+ - - ">="
129
+ - !ruby/object:Gem::Version
130
+ version: '0'
131
+ required_rubygems_version: !ruby/object:Gem::Requirement
132
+ requirements:
133
+ - - ">="
134
+ - !ruby/object:Gem::Version
135
+ version: '0'
136
+ requirements: []
137
+ rubyforge_project:
138
+ rubygems_version: 2.4.5
139
+ signing_key:
140
+ specification_version: 4
141
+ summary: Simple gem to consume json api
142
+ test_files:
143
+ - spec/consumer_spec.rb
144
+ - spec/spec_helper.rb