happi 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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: 6ab6180f7bd4996bbc905f7fd2469fcc70ccfa2a
4
+ data.tar.gz: a2c1f4fcab04dd623e1880c85d48a4df9bc41762
5
+ SHA512:
6
+ metadata.gz: 8cd2b01d91ea668c1fb3111c9f3a8f01ec8cafff30fdae63a252514ff75c03d7f47cd443361682dc8dc1b97b25e715695424b4736dd4a3bbf5b8b2992ceb3674
7
+ data.tar.gz: 08ddc2c0c881a989ee72283412a85dd80d3adb247eece4202e9bdea55c00c633111e90f008141ec3773d52080f627acd11a30911db05ffc7d1943d9a120fb087
data/.gitignore ADDED
@@ -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,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in happi.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 John D'Agostino
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,68 @@
1
+ # Happi
2
+
3
+ Happi - preconfigured faraday client
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'happi'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install happi
18
+
19
+ ## Usage
20
+
21
+ require 'happi'
22
+
23
+ client = Happi::Client.new(
24
+ base_url: 'http://localhost:3000',
25
+ oauth_token: '63ba06720acf97959f5ba3e3fe1020bf69a7596e2fe3091f821a35cdfe615ceb')
26
+
27
+ templates = client.get('templates')[:templates]
28
+ templates.each do |template|
29
+ puts template[:name]
30
+ end
31
+
32
+ response = client.post('templates', template: {name: 'test',
33
+ file: Happi::File.new(File.join(File.dirname(__FILE__), 'spec/fixtures/award.docx')) } )
34
+ template = response[:template]
35
+
36
+ templates = client.get('templates')[:templates]
37
+ template = client.get('templates/1')[:template]
38
+
39
+ template = client.patch("templates/#{template[:id]}",
40
+ template: {id: template[:id],
41
+ name: 'test' }
42
+ )[:template]
43
+
44
+ client.get('documents')[:documents].each do |document|
45
+ puts document[:name]
46
+ end
47
+
48
+ document = client.post('documents',
49
+ document: {template_id: template[:id],
50
+ name: 'Test Document',
51
+ params: JSON.dump({name: 'Test'}) } )[:document]
52
+
53
+ puts document[:id]
54
+
55
+ ## Contributing
56
+
57
+ 1. Fork it
58
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
59
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
60
+ 4. Push to the branch (`git push origin my-new-feature`)
61
+ 5. Create new Pull Request
62
+
63
+
64
+ ### Testing
65
+
66
+ To run the specs
67
+
68
+ rspec
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new('spec')
5
+
6
+ task :default => :spec
data/happi.gemspec ADDED
@@ -0,0 +1,34 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'happi/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "happi"
8
+ spec.version = Happi::VERSION
9
+ spec.authors = ["John D'Agostino"]
10
+ spec.email = ["john.dagostino@gmail.com"]
11
+ spec.description = %q{Simple faraday client preconfigured}
12
+ spec.summary = %q{Simple faraday client wrapper preconfigured for specific usecase}
13
+ spec.homepage = "http://github.com/jobready/happi"
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_dependency 'faraday'
22
+ spec.add_dependency 'faraday_middleware'
23
+ spec.add_dependency 'activesupport'
24
+ spec.add_dependency 'activemodel'
25
+ spec.add_dependency 'oauth2'
26
+ spec.add_dependency 'mime-types'
27
+ spec.add_dependency 'multi_json'
28
+
29
+ spec.add_development_dependency 'bundler', '~> 1.3'
30
+ spec.add_development_dependency 'simplecov'
31
+ spec.add_development_dependency 'cane'
32
+ spec.add_development_dependency 'rake'
33
+ spec.add_development_dependency 'rspec'
34
+ end
@@ -0,0 +1,105 @@
1
+ require 'faraday'
2
+ require 'faraday_middleware'
3
+ require 'active_support/core_ext/string/inflections'
4
+ require 'active_support/core_ext/hash'
5
+
6
+ class Happi::Client
7
+ DEFAULTS = {
8
+ host: 'http://localhost:8080',
9
+ port: 443,
10
+ timeout: 60,
11
+ version: 'v1'
12
+ }
13
+
14
+ attr_accessor :oauth_token, :host, :port, :timeout, :version
15
+
16
+ def initialize(options = {})
17
+ DEFAULTS.merge(options).each do |key, value|
18
+ send("#{key}=", value)
19
+ end
20
+
21
+ if block_given?
22
+ yield
23
+ end
24
+ end
25
+
26
+ def get(resource, params = {})
27
+ call(:get, url(resource), param_check(params))
28
+ .body.with_indifferent_access
29
+ end
30
+
31
+ def patch(resource, params = {})
32
+ call(:patch, url(resource), param_check(params))
33
+ .body.with_indifferent_access
34
+ end
35
+
36
+ def post(resource, params = {})
37
+ call(:post, url(resource), param_check(params))
38
+ .body.with_indifferent_access
39
+ end
40
+
41
+ def url(resource)
42
+ "/api/#{version}/#{resource}"
43
+ end
44
+
45
+ def call(method, url, params)
46
+ logger.info("#{method}, #{url}, #{params}")
47
+ response = connection.send(method, url, params)
48
+ raise_error(response) if errors[response.status]
49
+ response
50
+ end
51
+
52
+ def raise_error(response)
53
+ if response.body['errors']
54
+ message = response.body['errors']
55
+ else
56
+ message = response.body
57
+ end
58
+
59
+ fail errors[response.status].new(message)
60
+ end
61
+
62
+ def logger
63
+ @logger ||= Logger.new(STDOUT)
64
+ end
65
+
66
+ def param_check(params)
67
+ Hash[params.map do |key, value|
68
+ if value.is_a? Hash
69
+ [key, param_check(value)]
70
+ end
71
+ if value.respond_to?(:multipart)
72
+ [key, value.multipart]
73
+ else
74
+ [key, value]
75
+ end
76
+ end]
77
+ end
78
+
79
+ def connection
80
+ @connection ||= Faraday.new(host) do |f|
81
+ f.request :multipart
82
+ f.use FaradayMiddleware::OAuth2, oauth_token
83
+ f.use FaradayMiddleware::ParseJson, content_type: 'application/json'
84
+ f.request :url_encoded
85
+ f.adapter :net_http
86
+ end
87
+ end
88
+
89
+ def errors
90
+ @errors ||= {
91
+ 400 => Happi::Error::BadRequest,
92
+ 401 => Happi::Error::Unauthorized,
93
+ 403 => Happi::Error::Forbidden,
94
+ 404 => Happi::Error::NotFound,
95
+ 406 => Happi::Error::NotAcceptable,
96
+ 408 => Happi::Error::RequestTimeout,
97
+ 422 => Happi::Error::UnprocessableEntity,
98
+ 429 => Happi::Error::TooManyRequests,
99
+ 500 => Happi::Error::InternalServerError,
100
+ 502 => Happi::Error::BadGateway,
101
+ 503 => Happi::Error::ServiceUnavailable,
102
+ 504 => Happi::Error::GatewayTimeout,
103
+ }
104
+ end
105
+ end
@@ -0,0 +1,16 @@
1
+ class Happi::Error < Exception
2
+ class ClientError < self; end
3
+ class BadRequest < ClientError; end
4
+ class Unauthorized < ClientError; end
5
+ class Forbidden < ClientError; end
6
+ class NotFound < ClientError; end
7
+ class NotAcceptable < ClientError; end
8
+ class RequestTimeout < ClientError; end
9
+ class UnprocessableEntity < ClientError; end
10
+ class ServerError < self; end
11
+ class InternalServerError < ServerError; end
12
+ class BadGateway < ServerError; end
13
+ class TooManyRequests < ServerError; end
14
+ class ServiceUnavailable < ServerError; end
15
+ class GatewayTimeout < ServerError; end
16
+ end
data/lib/happi/file.rb ADDED
@@ -0,0 +1,25 @@
1
+ require 'mime/types'
2
+
3
+ class Happi::File
4
+ attr_accessor :file_name
5
+
6
+ def initialize(file_name)
7
+ @file_name = file_name
8
+ end
9
+
10
+ def exists?
11
+ File.exists?(file_name)
12
+ end
13
+
14
+ def multipart
15
+ Faraday::UploadIO.new(file_name, mime_type) if exists?
16
+ end
17
+
18
+ def mime_type
19
+ MIME::Types.type_for(file_name).first.content_type
20
+ end
21
+
22
+ def encode_file
23
+ Base64.encode64(File.read(file_name))
24
+ end
25
+ end
@@ -0,0 +1,3 @@
1
+ module Happi
2
+ VERSION = '0.0.1'
3
+ end
data/lib/happi.rb ADDED
@@ -0,0 +1,7 @@
1
+ require 'happi/version'
2
+ require 'happi/error'
3
+ require 'happi/client'
4
+ require 'happi/file'
5
+
6
+ module Happi
7
+ end
@@ -0,0 +1,17 @@
1
+ require 'spec_helper'
2
+
3
+ describe Happi::Client do
4
+
5
+ describe 'options' do
6
+ specify { expect(Happi::Client.new(host: 'http://www.google.com').host).to eql('http://www.google.com') }
7
+ specify { expect(Happi::Client.new(port: 80).port).to eql(80) }
8
+ end
9
+
10
+ describe 'call' do
11
+
12
+ end
13
+
14
+ describe 'raise_error' do
15
+
16
+ end
17
+ end
data/spec/file_spec.rb ADDED
@@ -0,0 +1,22 @@
1
+ require 'spec_helper'
2
+
3
+ describe Happi::File do
4
+ subject { Happi::File.new(__FILE__) }
5
+
6
+ describe '#encode_file' do
7
+ encoded = Base64.encode64(File.read(__FILE__))
8
+ specify { expect(subject.encode_file).to eql(encoded) }
9
+ end
10
+
11
+ describe '#mime_type' do
12
+ specify { expect(subject.mime_type).to eql('application/x-ruby') }
13
+ end
14
+
15
+ describe '#multipart' do
16
+ # specify { expect(subject.multipart).to }
17
+ end
18
+
19
+ describe '#exists?' do
20
+ specify { expect(subject.exists?).to be_true }
21
+ end
22
+ end
Binary file
@@ -0,0 +1,12 @@
1
+ require 'simplecov'
2
+ SimpleCov.start
3
+
4
+ $LOAD_PATH.unshift File.join(File.dirname(__FILE__), *%w[.. lib])
5
+
6
+ require 'rspec'
7
+ require 'happi'
8
+
9
+ Dir[File.expand_path('../../spec/support/**/*.rb', __FILE__)].map(&method(:require))
10
+
11
+ RSpec.configure do |config|
12
+ end
metadata ADDED
@@ -0,0 +1,232 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: happi
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - John D'Agostino
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-04-28 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: faraday
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ! '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ! '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: faraday_middleware
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ! '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ! '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: activesupport
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ! '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: activemodel
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
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: oauth2
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ! '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :runtime
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: mime-types
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ! '>='
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ! '>='
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: multi_json
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ! '>='
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :runtime
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ! '>='
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: bundler
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ~>
116
+ - !ruby/object:Gem::Version
117
+ version: '1.3'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ~>
123
+ - !ruby/object:Gem::Version
124
+ version: '1.3'
125
+ - !ruby/object:Gem::Dependency
126
+ name: simplecov
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ! '>='
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ! '>='
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ - !ruby/object:Gem::Dependency
140
+ name: cane
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - ! '>='
144
+ - !ruby/object:Gem::Version
145
+ version: '0'
146
+ type: :development
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - ! '>='
151
+ - !ruby/object:Gem::Version
152
+ version: '0'
153
+ - !ruby/object:Gem::Dependency
154
+ name: rake
155
+ requirement: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - ! '>='
158
+ - !ruby/object:Gem::Version
159
+ version: '0'
160
+ type: :development
161
+ prerelease: false
162
+ version_requirements: !ruby/object:Gem::Requirement
163
+ requirements:
164
+ - - ! '>='
165
+ - !ruby/object:Gem::Version
166
+ version: '0'
167
+ - !ruby/object:Gem::Dependency
168
+ name: rspec
169
+ requirement: !ruby/object:Gem::Requirement
170
+ requirements:
171
+ - - ! '>='
172
+ - !ruby/object:Gem::Version
173
+ version: '0'
174
+ type: :development
175
+ prerelease: false
176
+ version_requirements: !ruby/object:Gem::Requirement
177
+ requirements:
178
+ - - ! '>='
179
+ - !ruby/object:Gem::Version
180
+ version: '0'
181
+ description: Simple faraday client preconfigured
182
+ email:
183
+ - john.dagostino@gmail.com
184
+ executables: []
185
+ extensions: []
186
+ extra_rdoc_files: []
187
+ files:
188
+ - .gitignore
189
+ - Gemfile
190
+ - LICENSE.txt
191
+ - README.md
192
+ - Rakefile
193
+ - happi.gemspec
194
+ - lib/happi.rb
195
+ - lib/happi/client.rb
196
+ - lib/happi/error.rb
197
+ - lib/happi/file.rb
198
+ - lib/happi/version.rb
199
+ - spec/client_spec.rb
200
+ - spec/file_spec.rb
201
+ - spec/fixtures/award.docx
202
+ - spec/spec_helper.rb
203
+ homepage: http://github.com/jobready/happi
204
+ licenses:
205
+ - MIT
206
+ metadata: {}
207
+ post_install_message:
208
+ rdoc_options: []
209
+ require_paths:
210
+ - lib
211
+ required_ruby_version: !ruby/object:Gem::Requirement
212
+ requirements:
213
+ - - ! '>='
214
+ - !ruby/object:Gem::Version
215
+ version: '0'
216
+ required_rubygems_version: !ruby/object:Gem::Requirement
217
+ requirements:
218
+ - - ! '>='
219
+ - !ruby/object:Gem::Version
220
+ version: '0'
221
+ requirements: []
222
+ rubyforge_project:
223
+ rubygems_version: 2.2.2
224
+ signing_key:
225
+ specification_version: 4
226
+ summary: Simple faraday client wrapper preconfigured for specific usecase
227
+ test_files:
228
+ - spec/client_spec.rb
229
+ - spec/file_spec.rb
230
+ - spec/fixtures/award.docx
231
+ - spec/spec_helper.rb
232
+ has_rdoc: