jurnal_api 0.1.0

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,92 @@
1
+ require 'faraday'
2
+ require File.expand_path('../version', __FILE__)
3
+
4
+ module JurnalApi
5
+ # Defines constants and methods related to configuration
6
+ module Configuration
7
+ # An array of valid keys in the options hash when configuring a {JurnalApi::API}
8
+ VALID_OPTIONS_KEYS = [
9
+ :access_token,
10
+ :adapter,
11
+ :api_version,
12
+ :base_url,
13
+ :connection_options,
14
+ :authorization_path,
15
+ :format,
16
+ :user_agent,
17
+ :no_response_wrapper
18
+ ].freeze
19
+
20
+ # By default, don't set a user access token
21
+ DEFAULT_ACCESS_TOKEN = nil
22
+
23
+ # The adapter that will be used to connect if none is set
24
+ #
25
+ # @note The default faraday adapter is Net::HTTP.
26
+ DEFAULT_ADAPTER = Faraday.default_adapter
27
+
28
+ # By default, don't set any connection options
29
+ DEFAULT_CONNECTION_OPTIONS = {}
30
+
31
+ # The endpoint that will be used to connect if none is set
32
+ #
33
+ # @note There is no reason to use any other endpoint at this time
34
+ DEFAULT_URL = 'https://sandbox-api.jurnal.id'.freeze
35
+
36
+ DEFAULT_AUTHORIZATION_PATH = 'core'.freeze
37
+
38
+ # By default, don't wrap responses with meta data (i.e. pagination)
39
+ DEFAULT_NO_RESPONSE_WRAPPER = false
40
+
41
+ # The response format appended to the path and sent in the 'Accept' header if none is set
42
+ #
43
+ # @note JSON is the only available format at this time
44
+ DEFAULT_FORMAT = :json
45
+
46
+ DEFAULT_VERSION = 'api/v1'
47
+
48
+ # By default, don't wrap responses with meta data (i.e. pagination)
49
+
50
+
51
+ # An array of valid request/response formats
52
+ #
53
+ # @note Not all methods support the XML format.
54
+ VALID_FORMATS = [:json].freeze
55
+
56
+ # The user agent that will be sent to the API endpoint if none is set
57
+ DEFAULT_USER_AGENT = "Jurnal Ruby Gem #{JurnalApi::VERSION}".freeze
58
+
59
+ # @private
60
+ attr_accessor *VALID_OPTIONS_KEYS
61
+
62
+ # When this module is extended, set all configuration options to their default values
63
+ def self.extended(base)
64
+ base.reset
65
+ end
66
+
67
+ # Convenience method to allow configuration options to be set in a block
68
+ def configure
69
+ yield self
70
+ end
71
+
72
+ # Create a hash of options and their values
73
+ def options
74
+ VALID_OPTIONS_KEYS.inject({}) do |option, key|
75
+ option.merge!(key => send(key))
76
+ end
77
+ end
78
+
79
+ # Reset all configuration options to defaults
80
+ def reset
81
+ self.access_token = ENV['JURNAL_COMPANY_API_KEY'] || DEFAULT_ACCESS_TOKEN
82
+ self.adapter = DEFAULT_ADAPTER
83
+ self.api_version = DEFAULT_VERSION
84
+ self.authorization_path = DEFAULT_AUTHORIZATION_PATH
85
+ self.connection_options = DEFAULT_CONNECTION_OPTIONS
86
+ self.base_url = ENV['JURNAL_API_BASE_URL'] || DEFAULT_URL
87
+ self.format = DEFAULT_FORMAT
88
+ self.no_response_wrapper = DEFAULT_NO_RESPONSE_WRAPPER
89
+ self.user_agent = DEFAULT_USER_AGENT
90
+ end
91
+ end
92
+ end
@@ -0,0 +1,43 @@
1
+ require 'faraday_middleware'
2
+
3
+ Dir[File.expand_path('../../faraday/*.rb', __FILE__)].each{|f| require f}
4
+
5
+ module JurnalApi
6
+ # @private
7
+ module Connection
8
+ private
9
+
10
+ def connection(raw = false)
11
+ basic_headers = {
12
+ 'Accept' => "application/#{format}; charset=utf-8",
13
+ 'User-Agent' => user_agent
14
+ }
15
+
16
+ options = { headers: basic_headers, url: endpoint }.merge(connection_options)
17
+
18
+ Faraday::Connection.new(options) do |connection|
19
+ # SET middlewares first
20
+ connection.response :logger
21
+ connection.use FaradayMiddleware::RaiseHttpException
22
+ connection.use Faraday::Request::UrlEncoded
23
+
24
+ unless raw
25
+ case format.to_s.downcase
26
+ when 'json' then connection.use FaradayMiddleware::ParseJson
27
+ end
28
+ end
29
+
30
+ # ...then the adapter
31
+ # this is to maintain the API of Faraday v1
32
+ connection.adapter adapter
33
+
34
+ case authorization_path
35
+ when 'partner/core'
36
+ connection.headers['Authorization'] = "Bearer #{access_token}"
37
+ when 'core'
38
+ connection.headers['apikey'] = "#{access_token}"
39
+ end
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,46 @@
1
+ module JurnalApi
2
+ # Custom error class for rescuing from all Jurnal errors
3
+ class Error < StandardError; end
4
+ class ErrorWithBody < Error
5
+ attr_reader :body
6
+
7
+ def initialize(msg = "", body = {})
8
+ @body = body
9
+
10
+ super(msg)
11
+ end
12
+ end
13
+
14
+ # Raised when Jurnal returns the HTTP status code 400
15
+ class BadRequest < Error; end
16
+
17
+ # Raised when Jurnal returns the HTTP status code 404
18
+ class NotFound < Error; end
19
+
20
+ # Raised when Jurnal returns the HTTP status code 409
21
+ class Conflict < ErrorWithBody; end
22
+
23
+ # Raised when Jurnal returns the HTTP status code 422
24
+ class UnprocessableEntity < ErrorWithBody; end
25
+
26
+ # Raised when Jurnal returns the HTTP status code 429
27
+ class TooManyRequests < Error; end
28
+
29
+ # Raised when Jurnal returns the HTTP status code 500
30
+ class InternalServerError < Error; end
31
+
32
+ # Raised when Jurnal returns the HTTP status code 502
33
+ class BadGateway < Error; end
34
+
35
+ # Raised when Jurnal returns the HTTP status code 503
36
+ class ServiceUnavailable < Error; end
37
+
38
+ # Raised when Jurnal returns the HTTP status code 504
39
+ class GatewayTimeout < Error; end
40
+
41
+ # Raised when a subscription payload hash is invalid
42
+ class InvalidSignature < Error; end
43
+
44
+ # Raised when Jurnal returns the HTTP status code 429
45
+ class RateLimitExceeded < Error; end
46
+ end
@@ -0,0 +1,54 @@
1
+ require 'openssl'
2
+ require 'base64'
3
+
4
+ module JurnalApi
5
+ # Defines HTTP request methods
6
+ module Request
7
+ # Perform an HTTP GET request
8
+ def get(path, options={}, raw=false, unformatted=false, no_response_wrapper=no_response_wrapper())
9
+ request(:get, path, options, raw, unformatted, no_response_wrapper)
10
+ end
11
+
12
+ # Perform an HTTP POST request
13
+ def post(path, options={}, raw=false, unformatted=false, no_response_wrapper=no_response_wrapper())
14
+ request(:post, path, options, raw, unformatted, no_response_wrapper)
15
+ end
16
+
17
+ # Perform an HTTP PUT request
18
+ def put(path, options={}, raw=false, unformatted=false, no_response_wrapper=no_response_wrapper())
19
+ request(:put, path, options, raw, unformatted, no_response_wrapper)
20
+ end
21
+
22
+ # Perform an HTTP DELETE request
23
+ def delete(path, options={}, raw=false, unformatted=false, no_response_wrapper=no_response_wrapper())
24
+ request(:delete, path, options, raw, unformatted, no_response_wrapper)
25
+ end
26
+
27
+ private
28
+
29
+ # Perform an HTTP request
30
+ def request(method, path, options, raw=false, unformatted=false, no_response_wrapper=false)
31
+ response = connection(raw).send(method) do |request|
32
+ path = formatted_path(path) unless unformatted
33
+
34
+ case method
35
+ when :get, :delete
36
+ request.url(URI.encode(path), options)
37
+ when :post, :put
38
+ request.path = URI.encode(path)
39
+ request.headers['Content-Type'] = 'application/json'
40
+ request.body = options unless options.empty?
41
+ end
42
+ end
43
+
44
+ return response if raw
45
+ return response.body if no_response_wrapper
46
+ return Response.create( response.body, {:limit => response.headers['x-ratelimit-limit'].to_i,
47
+ :remaining => response.headers['x-ratelimit-remaining'].to_i} )
48
+ end
49
+
50
+ def formatted_path(path)
51
+ [path, format].compact.join('.')
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,25 @@
1
+ module JurnalApi
2
+ module Response
3
+ def self.create( response_hash, ratelimit_hash )
4
+ return {} if response_hash.nil?
5
+
6
+ data = response_hash.data.dup rescue response_hash
7
+ data.extend( self )
8
+ data.instance_exec do
9
+ %w{pagination meta}.each do |k|
10
+ response_hash.public_send(k).tap do |v|
11
+ instance_variable_set("@#{k}", v) if v
12
+ end
13
+ end
14
+
15
+ @ratelimit = ratelimit_hash
16
+ end
17
+
18
+ data
19
+ end
20
+
21
+ attr_reader :pagination
22
+ attr_reader :meta
23
+ attr_reader :ratelimit
24
+ end
25
+ end
@@ -0,0 +1,3 @@
1
+ module JurnalApi
2
+ VERSION = '0.1.0'.freeze unless defined?(::JurnalApi::VERSION)
3
+ end
metadata ADDED
@@ -0,0 +1,254 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jurnal_api
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - mekari-devs
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2020-11-20 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: '2.0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: byebug
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '='
32
+ - !ruby/object:Gem::Version
33
+ version: 11.1.3
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '='
39
+ - !ruby/object:Gem::Version
40
+ version: 11.1.3
41
+ - !ruby/object:Gem::Dependency
42
+ name: capybara
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '='
46
+ - !ruby/object:Gem::Version
47
+ version: 2.5.0
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '='
53
+ - !ruby/object:Gem::Version
54
+ version: 2.5.0
55
+ - !ruby/object:Gem::Dependency
56
+ name: pry
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: rack
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '='
74
+ - !ruby/object:Gem::Version
75
+ version: 2.2.3
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '='
81
+ - !ruby/object:Gem::Version
82
+ version: 2.2.3
83
+ - !ruby/object:Gem::Dependency
84
+ name: rake
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '13.0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '13.0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rspec
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '3.0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '3.0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: shoulda-context
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - '='
116
+ - !ruby/object:Gem::Version
117
+ version: 1.2.2
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - '='
123
+ - !ruby/object:Gem::Version
124
+ version: 1.2.2
125
+ - !ruby/object:Gem::Dependency
126
+ name: shoulda-matchers
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - '='
130
+ - !ruby/object:Gem::Version
131
+ version: 3.1.1
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - '='
137
+ - !ruby/object:Gem::Version
138
+ version: 3.1.1
139
+ - !ruby/object:Gem::Dependency
140
+ name: webmock
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - '='
144
+ - !ruby/object:Gem::Version
145
+ version: 3.8.3
146
+ type: :development
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - '='
151
+ - !ruby/object:Gem::Version
152
+ version: 3.8.3
153
+ - !ruby/object:Gem::Dependency
154
+ name: faraday
155
+ requirement: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - '='
158
+ - !ruby/object:Gem::Version
159
+ version: 0.15.4
160
+ type: :runtime
161
+ prerelease: false
162
+ version_requirements: !ruby/object:Gem::Requirement
163
+ requirements:
164
+ - - '='
165
+ - !ruby/object:Gem::Version
166
+ version: 0.15.4
167
+ - !ruby/object:Gem::Dependency
168
+ name: faraday_middleware
169
+ requirement: !ruby/object:Gem::Requirement
170
+ requirements:
171
+ - - '='
172
+ - !ruby/object:Gem::Version
173
+ version: 0.13.1
174
+ type: :runtime
175
+ prerelease: false
176
+ version_requirements: !ruby/object:Gem::Requirement
177
+ requirements:
178
+ - - '='
179
+ - !ruby/object:Gem::Version
180
+ version: 0.13.1
181
+ - !ruby/object:Gem::Dependency
182
+ name: dotenv
183
+ requirement: !ruby/object:Gem::Requirement
184
+ requirements:
185
+ - - '='
186
+ - !ruby/object:Gem::Version
187
+ version: 2.7.5
188
+ type: :runtime
189
+ prerelease: false
190
+ version_requirements: !ruby/object:Gem::Requirement
191
+ requirements:
192
+ - - '='
193
+ - !ruby/object:Gem::Version
194
+ version: 2.7.5
195
+ description:
196
+ email:
197
+ - developer@mekari.com
198
+ executables: []
199
+ extensions: []
200
+ extra_rdoc_files: []
201
+ files:
202
+ - ".env.template"
203
+ - ".gitignore"
204
+ - ".rspec"
205
+ - ".travis.yml"
206
+ - Gemfile
207
+ - Gemfile.lock
208
+ - LICENSE
209
+ - README.md
210
+ - Rakefile
211
+ - bin/console
212
+ - bin/setup
213
+ - jurnal-api.gemspec
214
+ - lib/faraday/raise_http_exception.rb
215
+ - lib/jurnal_api.rb
216
+ - lib/jurnal_api/api.rb
217
+ - lib/jurnal_api/client.rb
218
+ - lib/jurnal_api/client/customers.rb
219
+ - lib/jurnal_api/client/journal_entries.rb
220
+ - lib/jurnal_api/client/products.rb
221
+ - lib/jurnal_api/client/receive_payments.rb
222
+ - lib/jurnal_api/client/recurring_schedules.rb
223
+ - lib/jurnal_api/client/sales_invoices.rb
224
+ - lib/jurnal_api/client/sales_order_payments.rb
225
+ - lib/jurnal_api/client/sales_orders.rb
226
+ - lib/jurnal_api/configuration.rb
227
+ - lib/jurnal_api/connection.rb
228
+ - lib/jurnal_api/error.rb
229
+ - lib/jurnal_api/request.rb
230
+ - lib/jurnal_api/response.rb
231
+ - lib/jurnal_api/version.rb
232
+ homepage: https://github.com/mekari-engineering/jurnal_api
233
+ licenses: []
234
+ metadata: {}
235
+ post_install_message:
236
+ rdoc_options: []
237
+ require_paths:
238
+ - lib
239
+ required_ruby_version: !ruby/object:Gem::Requirement
240
+ requirements:
241
+ - - ">="
242
+ - !ruby/object:Gem::Version
243
+ version: '0'
244
+ required_rubygems_version: !ruby/object:Gem::Requirement
245
+ requirements:
246
+ - - ">="
247
+ - !ruby/object:Gem::Version
248
+ version: '0'
249
+ requirements: []
250
+ rubygems_version: 3.0.4
251
+ signing_key:
252
+ specification_version: 4
253
+ summary: Ruby wrapper for the Jurnal API.
254
+ test_files: []