giftwrap 0.0.1

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.
Binary file
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
4
+
5
+ gem 'pry'
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Kyle Decot
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.
@@ -0,0 +1,31 @@
1
+ # Giftwrap
2
+
3
+ Rubygem for creating wrappers for RESTful APIs
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'giftwrap'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install giftwrap
18
+
19
+ ## Usage
20
+
21
+ api = Giftwrap.new 'api.example.com'
22
+ person = api.get 'people/1' # calls api.example.com/people/1.json
23
+ puts person.first_name
24
+
25
+ ## Contributing
26
+
27
+ 1. Fork it
28
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
29
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
30
+ 4. Push to the branch (`git push origin my-new-feature`)
31
+ 5. Create new Pull Request
@@ -0,0 +1,11 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core"
3
+ require "rspec/core/rake_task"
4
+
5
+ RSpec::Core::RakeTask.new(:spec) do |spec|
6
+ spec.fail_on_error = false
7
+ spec.rspec_opts = "--color --format documentation"
8
+ spec.pattern = FileList["spec/**/*_spec.rb"]
9
+ end
10
+
11
+ task default: :spec
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'giftwrap/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "giftwrap"
8
+ spec.version = Giftwrap::VERSION
9
+ spec.authors = ["Kyle Decot"]
10
+ spec.email = ["kyle.decot@me.com"]
11
+ spec.description = %q{Rubygem for creating thin wrappers around RESTful JSON APIs}
12
+ spec.summary = %q{Rubygem for creating thin wrappers around RESTful JSON APIs}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
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.3"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "rspec"
24
+
25
+ spec.add_runtime_dependency "json"
26
+ end
@@ -0,0 +1,38 @@
1
+ %w[giftwrap/version net/http json ostruct].each { |f| require f }
2
+
3
+ class Giftwrap
4
+ def initialize(endpoint, *args, &block)
5
+ @endpoint = endpoint
6
+ end
7
+
8
+ def get(path, params = {})
9
+ path = "/api/#{path}.json?#{URI.encode_www_form(params)}"
10
+ response = request(path)
11
+ process(JSON.parse(response.body)) if response.code == "200"
12
+ rescue Exception => exception
13
+ end
14
+
15
+ private
16
+
17
+ def request(path)
18
+ http.request(Net::HTTP::Get.new(path))
19
+ end
20
+
21
+ def http
22
+ @http ||= begin
23
+ http = Net::HTTP.new(@endpoint, 443)
24
+ http.use_ssl = true
25
+ http
26
+ end
27
+ end
28
+
29
+ def process(json)
30
+ if json.is_a? Array
31
+ json.map { |element| process(element) }
32
+ elsif json.is_a? Hash
33
+ OpenStruct.new(Hash[json.map { |key, value| [key, process(value)] }])
34
+ else
35
+ json
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,3 @@
1
+ class Giftwrap
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1 @@
1
+ [{"description":"Summer 2012","id":1,"term_code":"1124"},{"description":"Autumn 2012","id":2,"term_code":"1128"},{"description":"Spring 2013","id":3,"term_code":"1132"},{"description":"Summer 2013","id":4,"term_code":"1134"},{"description":"Autumn 2013","id":5,"term_code":"1138"}]
@@ -0,0 +1,67 @@
1
+ require "spec_helper"
2
+ require "giftwrap"
3
+
4
+ describe Giftwrap do
5
+ let(:giftwrap) { Giftwrap.new 'api.example.com' }
6
+
7
+ describe '.get' do
8
+ let(:json) { File.read(File.join('spec', 'fixtures', 'terms.json')) }
9
+
10
+ context 'success' do
11
+ it 'returns an array of terms' do
12
+ response = double()
13
+ response.stub!(:code) { "200" }
14
+ response.stub!(:body) { json }
15
+
16
+ giftwrap.stub!(:path_with_params).with("/api/terms.json", {})
17
+ giftwrap.stub!(:request).with(anything).and_return(response)
18
+ giftwrap.get('terms').should == giftwrap.send(:process, JSON.parse(json))
19
+ end
20
+ end
21
+
22
+ context 'failure' do
23
+ it 'returns nil' do
24
+ response = double()
25
+ response.stub!(:code) { "500" }
26
+ response.stub!(:body) { nil }
27
+
28
+ giftwrap.stub!(:path_with_params).with("/api/terms.json", {})
29
+ giftwrap.stub!(:request).with(anything).and_return(response)
30
+ giftwrap.get('terms').should be_nil
31
+ end
32
+ end
33
+
34
+ end
35
+
36
+ describe '.process' do
37
+ let(:data) { [1, 2, { foo: 'bar', baz: true }] }
38
+ subject { giftwrap.send :process, data }
39
+ it 'transforms json' do
40
+ subject[0].should eql 1
41
+ subject[2].foo.should eql 'bar'
42
+ subject[2].baz.should be_true
43
+ end
44
+ end
45
+
46
+ describe '.http' do
47
+ subject { giftwrap.send :http }
48
+ it { should respond_to(:request) }
49
+ end
50
+
51
+ describe '.request' do
52
+ let(:response) do
53
+ response = double()
54
+ response.stub!(:body).and_return('')
55
+ response.stub!(:code).and_return('200')
56
+ response
57
+ end
58
+ subject { giftwrap.send(:request, '/api/terms.json') }
59
+ it 'should respond to body and code' do
60
+ http = double()
61
+ http.stub!(:request).and_return(response)
62
+ giftwrap.stub!(:http).and_return(http)
63
+
64
+ subject.should respond_to(:body, :code)
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,4 @@
1
+ RSpec.configure do |config|
2
+ config.mock_with :rspec
3
+ config.order = "random"
4
+ end
metadata ADDED
@@ -0,0 +1,131 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: giftwrap
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Kyle Decot
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-06-24 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '1.3'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '1.3'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rake
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: rspec
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: json
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :runtime
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ description: Rubygem for creating thin wrappers around RESTful JSON APIs
79
+ email:
80
+ - kyle.decot@me.com
81
+ executables: []
82
+ extensions: []
83
+ extra_rdoc_files: []
84
+ files:
85
+ - .DS_Store
86
+ - .gitignore
87
+ - Gemfile
88
+ - LICENSE.txt
89
+ - README.md
90
+ - Rakefile
91
+ - giftwrap.gemspec
92
+ - lib/giftwrap.rb
93
+ - lib/giftwrap/version.rb
94
+ - spec/fixtures/terms.json
95
+ - spec/giftwrap_spec.rb
96
+ - spec/spec_helper.rb
97
+ homepage: ''
98
+ licenses:
99
+ - MIT
100
+ post_install_message:
101
+ rdoc_options: []
102
+ require_paths:
103
+ - lib
104
+ required_ruby_version: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ! '>='
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ segments:
111
+ - 0
112
+ hash: 1831239448922278887
113
+ required_rubygems_version: !ruby/object:Gem::Requirement
114
+ none: false
115
+ requirements:
116
+ - - ! '>='
117
+ - !ruby/object:Gem::Version
118
+ version: '0'
119
+ segments:
120
+ - 0
121
+ hash: 1831239448922278887
122
+ requirements: []
123
+ rubyforge_project:
124
+ rubygems_version: 1.8.25
125
+ signing_key:
126
+ specification_version: 3
127
+ summary: Rubygem for creating thin wrappers around RESTful JSON APIs
128
+ test_files:
129
+ - spec/fixtures/terms.json
130
+ - spec/giftwrap_spec.rb
131
+ - spec/spec_helper.rb