passworks 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.
Files changed (38) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +22 -0
  3. data/CHANGELOG.md +7 -0
  4. data/Gemfile +4 -0
  5. data/README.md +319 -0
  6. data/Rakefile +2 -0
  7. data/lib/passworks.rb +49 -0
  8. data/lib/passworks/asset_resource.rb +11 -0
  9. data/lib/passworks/campaign_resource.rb +40 -0
  10. data/lib/passworks/client.rb +53 -0
  11. data/lib/passworks/collection_proxy.rb +46 -0
  12. data/lib/passworks/configuration.rb +58 -0
  13. data/lib/passworks/exception.rb +5 -0
  14. data/lib/passworks/exceptions/bad_gateway.rb +8 -0
  15. data/lib/passworks/exceptions/bad_request.rb +8 -0
  16. data/lib/passworks/exceptions/enhance_your_calm.rb +9 -0
  17. data/lib/passworks/exceptions/file_not_found.rb +8 -0
  18. data/lib/passworks/exceptions/forbidden.rb +8 -0
  19. data/lib/passworks/exceptions/gateway_timeout.rb +8 -0
  20. data/lib/passworks/exceptions/internal_server_error.rb +8 -0
  21. data/lib/passworks/exceptions/method_not_allowed.rb +8 -0
  22. data/lib/passworks/exceptions/not_found.rb +8 -0
  23. data/lib/passworks/exceptions/not_implemented.rb +8 -0
  24. data/lib/passworks/exceptions/payment_required.rb +8 -0
  25. data/lib/passworks/exceptions/precondition_failed.rb +8 -0
  26. data/lib/passworks/exceptions/service_unavailable.rb +8 -0
  27. data/lib/passworks/exceptions/unauthorized.rb +8 -0
  28. data/lib/passworks/exceptions/unprocessable_entity.rb +8 -0
  29. data/lib/passworks/faraday/http_exception_middleware.rb +73 -0
  30. data/lib/passworks/inflector.rb +38 -0
  31. data/lib/passworks/pass_resource.rb +40 -0
  32. data/lib/passworks/request.rb +49 -0
  33. data/lib/passworks/request_proxy.rb +115 -0
  34. data/lib/passworks/resource.rb +18 -0
  35. data/lib/passworks/response.rb +56 -0
  36. data/lib/passworks/version.rb +3 -0
  37. data/passworks.gemspec +34 -0
  38. metadata +208 -0
@@ -0,0 +1,49 @@
1
+ module Passworks
2
+ module Request
3
+
4
+ def request(method, path, options={})
5
+ response = agent.send(method) do |request|
6
+ case method
7
+ when :get, :delete
8
+ request.url(path, options[:query])
9
+ when :post, :put, :patch
10
+ request.path = path
11
+ request.body = options.fetch(:body, {})
12
+ end
13
+ end
14
+ Response.new(self, response)
15
+ end
16
+
17
+ def agent
18
+ @agent ||= ::Faraday.new(endpoint) do |connection|
19
+ connection.basic_auth(@api_username, @api_secret)
20
+ connection.request :multipart
21
+ connection.request :json
22
+ connection.response :logger
23
+ connection.response :json, :content_type => /\bjson$/
24
+ connection.use Passworks::Faraday::HttpExceptionMiddleware
25
+ connection.adapter ::Faraday.default_adapter
26
+ end
27
+ @agent.headers[:user_agent] = @user_agent
28
+ # always return the {@agent}
29
+ @agent
30
+ end
31
+
32
+ def get(url, options={})
33
+ request(:get, url, options)
34
+ end
35
+
36
+ def post(url, options={})
37
+ request(:post, url, options)
38
+ end
39
+
40
+ def patch(url, options={})
41
+ request(:patch, url, options)
42
+ end
43
+
44
+ def delete(url, options={})
45
+ request(:delete, url, options)
46
+ end
47
+
48
+ end
49
+ end
@@ -0,0 +1,115 @@
1
+ module Passworks
2
+ class RequestProxy
3
+
4
+ include Passworks::Inflector
5
+
6
+ # @return [Passworks::Client] fsdfsd fsf sdfsd fsdf
7
+ attr_reader :client
8
+
9
+ # @return [String] Current collection name (ex: assets, campaigns, boarding_passes, coupons, event_tickets)
10
+ attr_reader :collection_name
11
+
12
+ # @return [String] Collection UUID
13
+ attr_reader :collection_uuid
14
+
15
+ def initialize(client, collection_name, collection_uuid=nil, options={})
16
+ @collection_name = collection_name
17
+ @collection_uuid = collection_uuid
18
+ @client = client
19
+ @options = options
20
+ end
21
+
22
+ # Fetch all instances of a given collection (Assests, Boarding Passes, Coupons, Generics and Store Cards)
23
+ # @param options [Hash] options
24
+ # @option options [Integer] :per_page Changes the number of results per page
25
+ # @option options [Integer] :offset 0 The offset to start from
26
+ # @example Fetching the very first coupon (campaign) without having to pull all the elements first from the server
27
+ # client.coupons.all(per_page: 1).first
28
+ # @example Fetching the very first coupon pass from the first coupon (campaign) without having to pull all the elements first from the server
29
+ # client.coupons.all(per_page: 1).first.passes.all(per_page: 1).first
30
+ # @return [Passworks::CollectionProxy]
31
+ def all(options={})
32
+ options = { query: options } unless options.empty?
33
+ CollectionProxy.new(client, collection_name, collection_uuid, options)
34
+ end
35
+
36
+
37
+ # Finds a given campaing or passe given the context
38
+ # @param uuid [String] The campaign or pass UUID
39
+ # @example Fetching a coupon campaign
40
+ # coupon_campaign = client.coupons.find('c3d5fc64-3a43-4d3a-a167-473dfeb1edd3')
41
+ # @example Fetching a pass for the first coupon campaign
42
+ # pass = client.coupons.all(per_page: 1).first.passes.find('c3d5fc64-3a43-4d3a-a167-473dfeb1edd3')
43
+ # @return [PassResource.new or CampaignResource]
44
+ def find(uuid)
45
+ if collection_uuid
46
+ fetch_url = "#{collection_name}/#{collection_uuid}/passes/#{uuid}"
47
+ else
48
+ fetch_url = "#{collection_name}/#{uuid}"
49
+ end
50
+ response = client.get(fetch_url)
51
+ resource_class.new(client, collection_name, response.data)
52
+ end
53
+
54
+ # Creates a campaing instance (Assests, Boarding Passes, Coupons, Generics and Store Cards) or a passe depending of the caller context
55
+ # @param campaing_or_pass_data [Hash] campaing_or_pass_data Campaign or pass data
56
+ # @param extra_args [Hash] extra_args Extra arguments to send with the request
57
+ # @option extra_args [Boolean] merge: (true) Merge passed pass data with the campaign incase you are creating a passe instance.
58
+ # @example Create a coupon (campaign)
59
+ # client.coupons.create({
60
+ # name: 'My First Coupon',
61
+ # icon_id: 'c3d5fc64-3a43-4d3a-a167-473dfeb1edd3'
62
+ # })
63
+ # @example Create a "empty" passe for the first coupon campaign
64
+ # client.coupons.all(per_page: 1).first.passes.create()
65
+ # @return [Passworks::CampaignResource or Passworks::PassResource] depending of the calling a {Passworks::CampaignResource} or {Passworks::PassResource} is returned.
66
+ def create(campaing_or_pass_data={}, extra_args={})
67
+
68
+ if collection_name.to_s == 'assets' && collection_uuid.nil?
69
+ raise Passworks::Exceptions::FileNotFound.new("Can't find file #{hash[:file]}") unless hash.has_key?(:file) && File.exists?(hash[:file])
70
+ hash[:file] = ::Faraday::UploadIO.new(hash[:file], "image/#{hash[:file].split('.').last.downcase}")
71
+ end
72
+
73
+ if collection_uuid
74
+ content = {
75
+ body: {
76
+ 'pass' => campaing_or_pass_data
77
+ }.merge(extra_args)
78
+ }
79
+ fetch_url = "#{collection_url}/passes"
80
+ else
81
+ content = {
82
+ body: {
83
+ single_name.to_sym => campaing_or_pass_data
84
+ }.merge(extra_args)
85
+ }
86
+ fetch_url = collection_url
87
+ end
88
+
89
+ response = client.post(fetch_url, content)
90
+ resource_class.new(client, collection_name, response.data)
91
+
92
+ end
93
+
94
+ # Delets a campaing (Assests, Boarding Passes, Coupons, Generics and Store Cards campaign) or a pass instance
95
+ # @param uuid [String] the UUID of the campaign or pass to delete
96
+ # @return [Boolean]
97
+ def delete(uuid)
98
+ if collection_uuid
99
+ client.delete("#{collection_name}/#{collection_uuid}/passes/#{uuid}").ok?
100
+ else
101
+ client.delete("#{collection_name}/#{uuid}").ok?
102
+ end
103
+ end
104
+
105
+ # @!visibility private
106
+ def collection_url
107
+ if collection_uuid
108
+ "#{collection_name}/#{collection_uuid}"
109
+ else
110
+ collection_name
111
+ end
112
+ end
113
+
114
+ end
115
+ end
@@ -0,0 +1,18 @@
1
+ require 'passworks/inflector'
2
+ require 'ostruct'
3
+
4
+ module Passworks
5
+ class Resource < OpenStruct
6
+
7
+ include Passworks::Inflector
8
+
9
+ attr_reader :collection_name, :client
10
+
11
+ def initialize(client, collection_name, data)
12
+ @client = client
13
+ @collection_name = collection_name
14
+ super(data)
15
+ end
16
+
17
+ end
18
+ end
@@ -0,0 +1,56 @@
1
+ module Passworks
2
+ class Response
3
+
4
+ attr_reader :client, :response, :data
5
+
6
+ def initialize(client, response)
7
+ @client = client
8
+ @response = response
9
+ @data = response.body
10
+ end
11
+
12
+ def paginated?
13
+ (!headers['x-total-pages'].nil?) && (headers['x-total-pages'].to_i > 1)
14
+ end
15
+
16
+ def next_page?
17
+ !headers['x-next-page'].nil?
18
+ end
19
+
20
+ def next_page
21
+ headers['x-next-page'].to_i if next_page?
22
+ end
23
+
24
+ def previous_page?
25
+ !headers['x-prev-page'].nil?
26
+ end
27
+
28
+ def previous_page
29
+ headers['x-prev-page'].to_i if previous_page?
30
+ end
31
+
32
+ def http_status
33
+ @response[:status].to_i
34
+ end
35
+
36
+ def ok?
37
+ (http_status >= 200) && (http_status < 300)
38
+ end
39
+
40
+ def headers
41
+ @response.env[:response_headers] || {}
42
+ end
43
+
44
+ def size
45
+ return headers['x-total'].to_i unless headers['x-total'].nil?
46
+ return data.size if data.is_a?(Array)
47
+ return 0
48
+ end
49
+
50
+ def next_page_url
51
+ return nil unless next_page?
52
+ params = { page: next_page }
53
+ end
54
+
55
+ end
56
+ end
@@ -0,0 +1,3 @@
1
+ module Passworks
2
+ VERSION = "0.0.1"
3
+ end
data/passworks.gemspec ADDED
@@ -0,0 +1,34 @@
1
+ # coding: utf-8
2
+
3
+ lib = File.expand_path('../lib', __FILE__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+
6
+ require 'passworks/version'
7
+
8
+ Gem::Specification.new do |spec|
9
+ spec.name = "passworks"
10
+ spec.version = Passworks::VERSION
11
+ spec.authors = ["Luis Mendes", "Miguel Verissimo"]
12
+ spec.email = ["luis@passworks.io", "miguel@passworks.io"]
13
+ spec.summary = %q{Passworks API client}
14
+ spec.description = %q{Provides a simple interface to Passworks API}
15
+ spec.homepage = "https://www.passworks.io"
16
+ spec.license = "MIT"
17
+
18
+ spec.files = `git ls-files -z`.split("\x0")
19
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
20
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
21
+ spec.require_paths = ["lib"]
22
+
23
+ spec.add_dependency 'faraday', '~> 0.9.0'
24
+ spec.add_dependency 'faraday_middleware', '~> 0.9.1'
25
+
26
+ spec.add_development_dependency "bundler", "~> 1.6"
27
+ spec.add_development_dependency "rake"
28
+ spec.add_development_dependency "pry"
29
+ spec.add_development_dependency "pry-byebug"
30
+
31
+ spec.add_development_dependency "minitest"
32
+ spec.add_development_dependency "webmock"
33
+ spec.add_development_dependency "vcr"
34
+ end
metadata ADDED
@@ -0,0 +1,208 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: passworks
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Luis Mendes
8
+ - Miguel Verissimo
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2014-10-30 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: faraday
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - ~>
19
+ - !ruby/object:Gem::Version
20
+ version: 0.9.0
21
+ type: :runtime
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ~>
26
+ - !ruby/object:Gem::Version
27
+ version: 0.9.0
28
+ - !ruby/object:Gem::Dependency
29
+ name: faraday_middleware
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - ~>
33
+ - !ruby/object:Gem::Version
34
+ version: 0.9.1
35
+ type: :runtime
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ~>
40
+ - !ruby/object:Gem::Version
41
+ version: 0.9.1
42
+ - !ruby/object:Gem::Dependency
43
+ name: bundler
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ~>
47
+ - !ruby/object:Gem::Version
48
+ version: '1.6'
49
+ type: :development
50
+ prerelease: false
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ~>
54
+ - !ruby/object:Gem::Version
55
+ version: '1.6'
56
+ - !ruby/object:Gem::Dependency
57
+ name: rake
58
+ requirement: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - '>='
61
+ - !ruby/object:Gem::Version
62
+ version: '0'
63
+ type: :development
64
+ prerelease: false
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ - !ruby/object:Gem::Dependency
71
+ name: pry
72
+ requirement: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - '>='
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ type: :development
78
+ prerelease: false
79
+ version_requirements: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - '>='
82
+ - !ruby/object:Gem::Version
83
+ version: '0'
84
+ - !ruby/object:Gem::Dependency
85
+ name: pry-byebug
86
+ requirement: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - '>='
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ type: :development
92
+ prerelease: false
93
+ version_requirements: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - '>='
96
+ - !ruby/object:Gem::Version
97
+ version: '0'
98
+ - !ruby/object:Gem::Dependency
99
+ name: minitest
100
+ requirement: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - '>='
103
+ - !ruby/object:Gem::Version
104
+ version: '0'
105
+ type: :development
106
+ prerelease: false
107
+ version_requirements: !ruby/object:Gem::Requirement
108
+ requirements:
109
+ - - '>='
110
+ - !ruby/object:Gem::Version
111
+ version: '0'
112
+ - !ruby/object:Gem::Dependency
113
+ name: webmock
114
+ requirement: !ruby/object:Gem::Requirement
115
+ requirements:
116
+ - - '>='
117
+ - !ruby/object:Gem::Version
118
+ version: '0'
119
+ type: :development
120
+ prerelease: false
121
+ version_requirements: !ruby/object:Gem::Requirement
122
+ requirements:
123
+ - - '>='
124
+ - !ruby/object:Gem::Version
125
+ version: '0'
126
+ - !ruby/object:Gem::Dependency
127
+ name: vcr
128
+ requirement: !ruby/object:Gem::Requirement
129
+ requirements:
130
+ - - '>='
131
+ - !ruby/object:Gem::Version
132
+ version: '0'
133
+ type: :development
134
+ prerelease: false
135
+ version_requirements: !ruby/object:Gem::Requirement
136
+ requirements:
137
+ - - '>='
138
+ - !ruby/object:Gem::Version
139
+ version: '0'
140
+ description: Provides a simple interface to Passworks API
141
+ email:
142
+ - luis@passworks.io
143
+ - miguel@passworks.io
144
+ executables: []
145
+ extensions: []
146
+ extra_rdoc_files: []
147
+ files:
148
+ - .gitignore
149
+ - CHANGELOG.md
150
+ - Gemfile
151
+ - README.md
152
+ - Rakefile
153
+ - lib/passworks.rb
154
+ - lib/passworks/asset_resource.rb
155
+ - lib/passworks/campaign_resource.rb
156
+ - lib/passworks/client.rb
157
+ - lib/passworks/collection_proxy.rb
158
+ - lib/passworks/configuration.rb
159
+ - lib/passworks/exception.rb
160
+ - lib/passworks/exceptions/bad_gateway.rb
161
+ - lib/passworks/exceptions/bad_request.rb
162
+ - lib/passworks/exceptions/enhance_your_calm.rb
163
+ - lib/passworks/exceptions/file_not_found.rb
164
+ - lib/passworks/exceptions/forbidden.rb
165
+ - lib/passworks/exceptions/gateway_timeout.rb
166
+ - lib/passworks/exceptions/internal_server_error.rb
167
+ - lib/passworks/exceptions/method_not_allowed.rb
168
+ - lib/passworks/exceptions/not_found.rb
169
+ - lib/passworks/exceptions/not_implemented.rb
170
+ - lib/passworks/exceptions/payment_required.rb
171
+ - lib/passworks/exceptions/precondition_failed.rb
172
+ - lib/passworks/exceptions/service_unavailable.rb
173
+ - lib/passworks/exceptions/unauthorized.rb
174
+ - lib/passworks/exceptions/unprocessable_entity.rb
175
+ - lib/passworks/faraday/http_exception_middleware.rb
176
+ - lib/passworks/inflector.rb
177
+ - lib/passworks/pass_resource.rb
178
+ - lib/passworks/request.rb
179
+ - lib/passworks/request_proxy.rb
180
+ - lib/passworks/resource.rb
181
+ - lib/passworks/response.rb
182
+ - lib/passworks/version.rb
183
+ - passworks.gemspec
184
+ homepage: https://www.passworks.io
185
+ licenses:
186
+ - MIT
187
+ metadata: {}
188
+ post_install_message:
189
+ rdoc_options: []
190
+ require_paths:
191
+ - lib
192
+ required_ruby_version: !ruby/object:Gem::Requirement
193
+ requirements:
194
+ - - '>='
195
+ - !ruby/object:Gem::Version
196
+ version: '0'
197
+ required_rubygems_version: !ruby/object:Gem::Requirement
198
+ requirements:
199
+ - - '>='
200
+ - !ruby/object:Gem::Version
201
+ version: '0'
202
+ requirements: []
203
+ rubyforge_project:
204
+ rubygems_version: 2.2.2
205
+ signing_key:
206
+ specification_version: 4
207
+ summary: Passworks API client
208
+ test_files: []