place-ruby 0.5.5

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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: e5129d2663f57d69df1959b6af34d5c2be289eef57551f982bf3a22bfdbb0e71
4
+ data.tar.gz: b7240a849b674d17995cc70c28cefc5c00b7b8dbd329c93c7200e084f29a6ecf
5
+ SHA512:
6
+ metadata.gz: 5f32d48027dad2b3352750cbb6dae800440d0908e67c5eb1121b2ad3cf528f9fe3a477bc84f3ba3a48860034dc8421e648ebdc6aa3fb933539e38125d991e122
7
+ data.tar.gz: caa19e67ee9f07d015f123bc409d5ad946bbc01123b4e4c0d05d390d4fc09ce334aa9b540ad64fa0b9e794393e85506d21da1b626c6214fda26c017020048759
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License
2
+
3
+ Copyright (c) 2017 RentShare (http://rentshare.com)
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,38 @@
1
+ # Place RubyGem
2
+
3
+ A RubyGem for interfacing with the Place API
4
+
5
+ ## Installation
6
+
7
+ To install using [Bundler](https://bundler.io):
8
+
9
+ ```ruby
10
+ gem 'place', :git => 'git://github.com/placepay/place-ruby.git'
11
+ ```
12
+
13
+ To manually install `place-ruby` from github use gem specific_install:
14
+
15
+ ```bash
16
+ gem install specific_install
17
+ gem specific_install https://github.com/placepay/place-ruby.git
18
+ ```
19
+
20
+
21
+ ## Basic usage
22
+
23
+ ```ruby
24
+ require 'place'
25
+
26
+ # set your api key
27
+ Place.api_key = 'private_key_6fsMi3GDxXg1XXSluNx1sLEd'
28
+
29
+ # create an account
30
+ account = Place::Account.create(
31
+ :email => 'joe.schmoe@example.com',
32
+ :full_name => 'Joe Schmoe',
33
+ :user_type => 'payer'
34
+ )
35
+ ```
36
+
37
+ ## Documentation
38
+ Read the [docs](https://developer.placepay.com/?ruby)
@@ -0,0 +1,21 @@
1
+ require "place/version"
2
+ require "place/client"
3
+ require "place/exceptions"
4
+ require "place/api_resource"
5
+ require "place/resources"
6
+
7
+ module Place
8
+
9
+ @PROD_URL = "https://api.placepay.com"
10
+ @TEST_URL = "https://test-api.placepay.com"
11
+
12
+ @api_key = nil
13
+ @api_url = @PROD_URL
14
+
15
+ @default_client = self::Client.new
16
+
17
+ class << self
18
+ attr_accessor :PROD_URL, :TEST_URL, :api_key, :api_url, :default_client
19
+ end
20
+
21
+ end
@@ -0,0 +1,227 @@
1
+ require "net/http"
2
+ require "uri"
3
+ require "json"
4
+
5
+ module Place
6
+
7
+ def self.merge_obj( obj: nil, **params )
8
+ params = params.collect{|k,v| [k.to_s, v]}.to_h
9
+ if obj
10
+ obj = obj.merge(params)
11
+ else
12
+ obj = params
13
+ end
14
+ return obj
15
+ end
16
+
17
+ def self.conv_object(obj, client: nil, inverse: false)
18
+ obj = obj.clone
19
+ if obj.class == Array
20
+ iter = obj.map.with_index{|a,i| [i,a] }
21
+ else
22
+ iter = obj
23
+ end
24
+
25
+ iter.each do |key, val|
26
+ if inverse
27
+ if val.is_a?(Place::APIResource)
28
+ obj[key] = val._obj
29
+ val = obj[key]
30
+ end
31
+ elsif val.is_a?(Hash) and val['object']
32
+ for resource in Place::APIResource.descendants
33
+ if val['object'] != resource.object_type
34
+ next
35
+ end
36
+
37
+ obj[key] = resource.new(client: client, obj: val)
38
+ val = obj[key]
39
+ break
40
+ end
41
+ end
42
+ if [Hash, Array].member? val.class
43
+ obj[key] = Place::conv_object(val, client:client, inverse: inverse)
44
+ end
45
+ end
46
+
47
+ return obj
48
+ end
49
+
50
+ class APIResource
51
+ attr_accessor :_obj
52
+
53
+ def self.descendants
54
+ ObjectSpace.each_object(Class).select { |klass| klass < self }
55
+ end
56
+
57
+ @resource = nil
58
+ @object_type = nil
59
+ @@object_index = {}
60
+
61
+ @default_params = nil
62
+
63
+ class << self
64
+ attr_reader :resource, :object_type
65
+ attr_accessor :default_params
66
+ end
67
+
68
+ def self.new(client: nil, obj: nil, **params)
69
+ obj = Place::merge_obj( obj: obj, **params )
70
+
71
+ if obj["id"]
72
+ if @@object_index[obj["id"]]
73
+ @@object_index[obj["id"]]._set_obj(obj)
74
+ return @@object_index[obj["id"]]
75
+ end
76
+ end
77
+ super
78
+ end
79
+
80
+ def initialize(client: nil, obj: nil, **params)
81
+ obj = Place::merge_obj( obj: obj, **params )
82
+ @client = client || Place.default_client
83
+ self._set_obj(obj)
84
+ end
85
+
86
+ def _set_obj(obj)
87
+ @_obj = obj
88
+ @_obj = Place::conv_object(@_obj, client: @_client)
89
+ if obj["id"]
90
+ @@object_index[obj["id"]] = self
91
+ end
92
+ end
93
+
94
+ def method_missing(name, *args)
95
+ attr = name.to_s
96
+ if @_obj.key?(attr)
97
+ return @_obj[attr]
98
+ end
99
+ super
100
+ end
101
+
102
+ def json()
103
+ return JSON.pretty_generate(Place::conv_object(@_obj, inverse: true))
104
+ end
105
+
106
+ def self.request(method, path: nil, id: nil, client: nil, json: nil, params: nil)
107
+ path = path || @resource
108
+ client = client || Place.default_client
109
+
110
+ if id; path = File.join(path, id) end
111
+ if path[0] == '/'; path = path[1..-1] end
112
+
113
+ uri = URI.join(client.api_url, path)
114
+ if @default_params
115
+ if !params; params = {} end
116
+ @default_params.each do |key, param|
117
+ if !params.key?(key.to_sym); params[key.to_sym] = param end
118
+ end
119
+ end
120
+ if params; uri.query = URI.encode_www_form(params) end
121
+
122
+ http = Net::HTTP.new(uri.host, uri.port)
123
+ if uri.port == 443; http.use_ssl = true end
124
+ request = Net::HTTP.const_get(method).new(uri.request_uri)
125
+ request.basic_auth(client.api_key, "")
126
+ request.add_field("X-API-Version", "v2.5")
127
+
128
+ if json
129
+ request.body = json.to_json
130
+ request.add_field("Content-Type", "application/json")
131
+ end
132
+
133
+ response = http.request(request)
134
+ status_code = response.code
135
+
136
+ begin
137
+ obj = JSON.parse(response.body)
138
+ rescue JSON::ParserError
139
+ if status_code == '500'
140
+ raise Place::InternalError.new
141
+ end
142
+
143
+ raise Place::InvalidResponse.new
144
+ end
145
+
146
+ if obj.class != Hash
147
+ raise Place::InvalidResponse.new
148
+ end
149
+
150
+ object_type = obj["object"]
151
+ if !object_type
152
+ raise Place::InvalidResponse.new('Response missing "object" attribute')
153
+ end
154
+
155
+ if status_code != '200'
156
+ if object_type != 'error'
157
+ raise Place::InvalidResponse.new('Expected error object')
158
+ end
159
+
160
+ for exc in Place::APIException.descendants
161
+ if exc.status_code != status_code; next end
162
+
163
+ if exc.error_type && exc.error_type != obj["error_type"]; next end
164
+
165
+ raise exc.new(obj["error_description"],obj)
166
+ end
167
+
168
+ raise Place::APIException.new(obj["error_description"],obj)
169
+ end
170
+
171
+ if object_type == 'list'
172
+ return obj["values"].map {|o| self.new(:client=>client, :obj=>o) }
173
+ end
174
+
175
+ return self.new(:client=>client, :obj=>obj)
176
+ end
177
+
178
+ def update(**updates)
179
+ self.class.request('Put', id: self.id, json: updates)
180
+ end
181
+
182
+ def delete()
183
+ self.class.request('Delete', id: self.id)
184
+ end
185
+
186
+ def self.get(id,update:nil, **params)
187
+ if id.empty? || id.nil?
188
+ raise Place::APIException.new('id cannot be blank')
189
+ end
190
+ if update
191
+ return self.request('Put', id: id, json: update, params: params )
192
+ end
193
+ return self.request('Get', id: id, params: params)
194
+ end
195
+
196
+ def self.select( update_all: nil, delete_all: false, **filter_by)
197
+ if update_all
198
+ return self.request('Put', params: filter_by, json: update_all)
199
+ elsif delete_all
200
+ return self.request('Delete', params: filter_by)
201
+ else
202
+ return self.request('Get', params: filter_by)
203
+ end
204
+ end
205
+
206
+ def self.create( obj, **params)
207
+ if obj.class == Array
208
+ obj = {"object"=>"list", "values"=>obj}
209
+ else
210
+ obj = Place::merge_obj( obj: obj, **params )
211
+ end
212
+ obj = Place::conv_object(obj, inverse: true)
213
+ return self.request('Post', json: obj)
214
+ end
215
+
216
+ def self.update_all(updates, **params)
217
+ updates = updates.map {|o, upd| Hash(id: o.id, **upd) }
218
+ return self.request('Put',
219
+ json: {"object"=>"list", "values"=>updates}, params: params)
220
+ end
221
+
222
+ def self.delete_all(objects)
223
+ deletes = objects.map {|o| o.id }.join('|')
224
+ return self.request('Delete', params: {'id'=>deletes})
225
+ end
226
+ end
227
+ end
@@ -0,0 +1,25 @@
1
+
2
+ module Place
3
+ class Client
4
+ attr_accessor :api_key, :api_url
5
+
6
+ def initialize(api_key: nil, api_url: nil)
7
+ @api_key = api_key
8
+ @api_url = api_url
9
+ end
10
+
11
+ def api_key()
12
+ @api_key || Place.api_key
13
+ end
14
+
15
+ def api_url()
16
+ if @api_key
17
+ return @api_key
18
+ end
19
+ if self.api_key && self.api_key.start_with?('test_') and Place.api_url == Place.PROD_URL
20
+ return Place.TEST_URL
21
+ end
22
+ return Place.api_url
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,66 @@
1
+ module Place
2
+ class APIException < StandardError
3
+ attr_accessor :error_details
4
+ @error_type = nil
5
+ @status_code = nil
6
+
7
+ def initialize( msg = nil, error_details = nil )
8
+ super(msg)
9
+ @error_details = error_details
10
+ end
11
+
12
+ def self.descendants
13
+ ObjectSpace.each_object(Class).select { |klass| klass < self }
14
+ end
15
+
16
+ class << self
17
+ attr_reader :error_type, :status_code
18
+ end
19
+ end
20
+
21
+
22
+ class InvalidArguments < APIException
23
+ @error_type = 'InvalidArguments'
24
+ @status_code = '400'
25
+ end
26
+
27
+
28
+ class InvalidRequest < APIException
29
+ @error_type = 'Error'
30
+ @status_code = '400'
31
+ end
32
+
33
+
34
+ class Unauthorized < APIException
35
+ @status_code = '401'
36
+ end
37
+
38
+
39
+ class Forbidden < APIException
40
+ @status_code = '403'
41
+ end
42
+
43
+
44
+ class NotFound < APIException
45
+ @status_code = '404'
46
+ end
47
+
48
+
49
+ class MethodNotAllowed < APIException
50
+ @status_code = '405'
51
+ end
52
+
53
+
54
+ class TooManyRequests < APIException
55
+ @status_code = '429'
56
+ end
57
+
58
+
59
+ class InternalError < APIException
60
+ @status_code = '500'
61
+ end
62
+
63
+
64
+ class InvalidResponse < APIException
65
+ end
66
+ end
@@ -0,0 +1,84 @@
1
+ module Place
2
+ class AccessToken < APIResource
3
+ @resource = '/access_tokens'
4
+ @object_type = 'access_token'
5
+ end
6
+
7
+
8
+ class AutopayEnrollment < APIResource
9
+ @resource = '/autopay_enrollments'
10
+ @object_type = 'autopay_enrollment'
11
+ end
12
+
13
+
14
+ class Event < APIResource
15
+ @resource = '/events'
16
+ @object_type = 'event'
17
+ end
18
+
19
+
20
+ class Account < APIResource
21
+ @resource = '/accounts'
22
+ @object_type = 'account'
23
+ end
24
+
25
+
26
+ class DepositAccount < APIResource
27
+ @resource = '/deposit_accounts'
28
+ @object_type = 'deposit_account'
29
+ end
30
+
31
+
32
+ class Transaction < APIResource
33
+ @resource = '/transactions'
34
+ @object_type = 'transaction'
35
+ end
36
+
37
+
38
+ class TransactionAllocation < APIResource
39
+ @resource = '/transaction_allocations'
40
+ @object_type = 'transaction_allocation'
41
+ end
42
+
43
+
44
+ class PaymentMethod < APIResource
45
+ @resource = '/payment_methods'
46
+ @object_type = 'payment_method'
47
+ end
48
+
49
+
50
+ class Address < APIResource
51
+ @resource = '/addresses'
52
+ @object_type = 'address'
53
+ end
54
+
55
+
56
+ class RecurringInvoice < APIResource
57
+ @resource = '/recurring_invoices'
58
+ @object_type = 'recurring_invoice'
59
+ end
60
+
61
+
62
+ class Invoice < APIResource
63
+ @resource = '/invoices'
64
+ @object_type = 'invoice'
65
+ end
66
+
67
+
68
+ class InvoiceItem < APIResource
69
+ @resource = '/invoice_items'
70
+ @object_type = 'invoice_item'
71
+ end
72
+
73
+
74
+ class InvoicePayer < APIResource
75
+ @resource = '/invoice_payers'
76
+ @object_type = 'invoice_payer'
77
+ end
78
+
79
+
80
+ class InvoiceItemAllocation < APIResource
81
+ @resource = '/invoice_item_allocations'
82
+ @object_type = 'invoice_item_allocation'
83
+ end
84
+ end
@@ -0,0 +1,3 @@
1
+ module Place
2
+ VERSION = '0.5.5'
3
+ end
@@ -0,0 +1,21 @@
1
+ lib = File.join(File.dirname(__FILE__), "lib")
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+
4
+ require 'place/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "place-ruby"
8
+ spec.version = Place::VERSION
9
+ spec.required_ruby_version = ">= 2.0.0"
10
+ spec.summary = "Place ruby library"
11
+ spec.description = "A simple library to interface with the Place REST API. See https://developer.placepay.com for details."
12
+ spec.author = "Place"
13
+ spec.email = "help@placepay.com"
14
+ spec.homepage = "https://developer.placepay.com"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files`.split("\n")
18
+ spec.test_files = `git ls-files -- test/*`.split("\n")
19
+ spec.executables = `git ls-files -- bin/*`.split("\n").map { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+ end
metadata ADDED
@@ -0,0 +1,53 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: place-ruby
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.5.5
5
+ platform: ruby
6
+ authors:
7
+ - Place
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-08-14 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: A simple library to interface with the Place REST API. See https://developer.placepay.com
14
+ for details.
15
+ email: help@placepay.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - LICENSE
21
+ - README.md
22
+ - lib/place.rb
23
+ - lib/place/api_resource.rb
24
+ - lib/place/client.rb
25
+ - lib/place/exceptions.rb
26
+ - lib/place/resources.rb
27
+ - lib/place/version.rb
28
+ - place.gemspec
29
+ homepage: https://developer.placepay.com
30
+ licenses:
31
+ - MIT
32
+ metadata: {}
33
+ post_install_message:
34
+ rdoc_options: []
35
+ require_paths:
36
+ - lib
37
+ required_ruby_version: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ version: 2.0.0
42
+ required_rubygems_version: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ requirements: []
48
+ rubyforge_project:
49
+ rubygems_version: 2.7.6
50
+ signing_key:
51
+ specification_version: 4
52
+ summary: Place ruby library
53
+ test_files: []