merchant-zip 0.0.1 → 0.0.2

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 373ec98199100d662c6529821f8b1ac5a207043095f2efac34d718594d366db7
4
- data.tar.gz: 15f780e751600c89143449ae71f157a7ff0bef6a800e745538af8c601ba48aca
3
+ metadata.gz: 1dc22e52da8cfb73cbe203767916bf171076fade8cb6f81f4b04095fd988736a
4
+ data.tar.gz: 9821f6cb85e1c6f1a8231efce7b6afaec1826f7826f38383bbcac034a00161c5
5
5
  SHA512:
6
- metadata.gz: 6bfc3215600572d464c94339a16096510c253f0001d5aeefb9f9bba2c6794f191322443d5e3e321a1224c9d73415064826cd70fb3705b660ab65f8186ab8728f
7
- data.tar.gz: ac51bf7fdff001a5b2343491c5783ddc517d489673e8ef7e1f92783c7dbfb4ade3f40e3e202511d46cb93daa9e66e33b1afc7da09774ce19043ad678662e5802
6
+ metadata.gz: dac62f52ab8d0fed9a5abe0aa3104efc76f1779bf8a0f15fc185d0fc9430230d6387c565fcd971b08d529b0b60dfe066e307a05a56b699396693326a61a92cad
7
+ data.tar.gz: 55415b5fc8cb0bc452111c903ce03b7ef3a414d028065724a7dba4c53937904b8d8932479493309ca1d1b97fe74d0bb6404014c2400aa45c1e359b36713713fa
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2022 DevHub
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.
data/bin/console ADDED
@@ -0,0 +1,13 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "irb"
4
+ require_relative "../lib/merchant-zip"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ IRB.start(__FILE__)
@@ -0,0 +1,9 @@
1
+ module MerchantZip
2
+ class ApiResource
3
+ include MerchantZip::Operations::Request
4
+
5
+ def self.class_name
6
+ self.name.split('::')[-1]
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,24 @@
1
+ module MerchantZip
2
+ class Charge < ApiResource
3
+ extend MerchantZip::Operations::Get
4
+
5
+ def self.resource_url
6
+ "/#{CGI.escape(class_name.tableize)}"
7
+ end
8
+
9
+ def self.create(params = {}, opts = {})
10
+ payload = {
11
+ authority: {
12
+ type: "checkout_id",
13
+ value: params[:checkout_id],
14
+ },
15
+ reference: params[:reference],
16
+ amount: params[:amount],
17
+ currency: params[:currency].to_s.upcase,
18
+ capture: true,
19
+ }
20
+
21
+ request(:post, resource_url, payload, opts)
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,31 @@
1
+ module MerchantZip
2
+ class Checkout < ApiResource
3
+ extend MerchantZip::Operations::Get
4
+
5
+ def self.resource_url
6
+ "/#{CGI.escape(class_name.tableize)}"
7
+ end
8
+
9
+ def self.create(params = {}, opts = {})
10
+ payload = {
11
+ shopper: shopper_params(params),
12
+ order: {
13
+ reference: params[:reference],
14
+ amount: params[:amount],
15
+ currency: params[:currency].to_s.upcase,
16
+ }.merge(params.slice(:items, :shipping)),
17
+ config: {
18
+ redirect_uri: params[:callback_url],
19
+ }
20
+ }
21
+
22
+ request(:post, resource_url, payload, opts)
23
+ end
24
+
25
+ private
26
+
27
+ def self.shopper_params(params = {})
28
+ params.slice(:title, :first_name, :last_name, :email, :phone, :billing_address)
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,76 @@
1
+ require "forwardable"
2
+
3
+ module MerchantZip
4
+ class Client
5
+ extend Forwardable
6
+
7
+ DEFAULT_API_MODE = :live
8
+ API_MODES = [:live, :sandbox]
9
+ DEFAULT_API_ENDPOINT = {
10
+ sandbox: "https://global-api.sand.au.edge.zip.co/merchant",
11
+ live: "https://global-api.prod.au.edge.zip.co/merchant"
12
+ }
13
+
14
+ attr_reader :config
15
+
16
+ def_delegators :@config, :mode, :mode=
17
+ def_delegators :@config, :api_key, :api_key=
18
+
19
+ def initialize(config = {})
20
+ @config = case config
21
+ when Hash
22
+ MerchantZip.config.reverse_duplicate_merge(config)
23
+ when MerchantZip::Configuration
24
+ config
25
+ else
26
+ MerchantZip.config.dup
27
+ end
28
+ end
29
+
30
+ def api_mode
31
+ if API_MODES.include?(mode&.to_sym)
32
+ mode.to_sym
33
+ else
34
+ DEFAULT_API_MODE
35
+ end
36
+ end
37
+
38
+ def api_base
39
+ DEFAULT_API_ENDPOINT[api_mode]
40
+ end
41
+
42
+ def execute_request(method, url, params = {}, opts = {})
43
+ request_opts = opts
44
+ headers = {
45
+ "Content-Type" => "application/json",
46
+ "Authorization" => "Bearer #{api_key}",
47
+ "idempotency-key" => SecureRandom.base64(18),
48
+ }
49
+
50
+ request_url = api_base + url.to_s
51
+
52
+ case method.to_s.downcase.to_sym
53
+ when :get, :head, :delete
54
+ # Make params into GET parameters
55
+ request_url += "#{URI.parse(url).query ? '&' : '?'}#{Util.encode_parameters(params)}" if params && params.any?
56
+ payload = nil
57
+ else
58
+ payload = params
59
+ end
60
+
61
+ payload = payload.to_json if payload.is_a?(Hash)
62
+
63
+ request_opts.update(method: method,
64
+ headers: headers,
65
+ payload: payload,
66
+ url: request_url)
67
+
68
+ response = RestClient::Request.execute(request_opts)
69
+
70
+ JSON.parse(response.body, object_class: OpenStruct)
71
+ rescue => e
72
+ puts e.response
73
+ raise
74
+ end
75
+ end
76
+ end
@@ -0,0 +1,26 @@
1
+ module MerchantZip
2
+ class Configuration
3
+ attr_accessor :mode
4
+ attr_accessor :api_key
5
+
6
+ def self.setup
7
+ new.tap do |instance|
8
+ yield(instance) if block_given?
9
+ end
10
+ end
11
+
12
+ # Create a new config based off an existing one. This is useful when the
13
+ # caller wants to override the global configuration
14
+ def reverse_duplicate_merge(hash)
15
+ dup.tap do |instance|
16
+ hash.each do |option, value|
17
+ instance.public_send("#{option}=", value)
18
+ end
19
+ end
20
+ end
21
+
22
+ def initialize
23
+ @mode = :live
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,9 @@
1
+ module MerchantZip
2
+ module Operations
3
+ module Create
4
+ def create(params = {}, opts = {})
5
+ request(:post, resource_url, params, opts)
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,9 @@
1
+ module MerchantZip
2
+ module Operations
3
+ module Get
4
+ def get(id, params = {})
5
+ request(:get, "#{resource_url}/#{CGI.escape(id)}", params)
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,22 @@
1
+ module MerchantZip
2
+ module Operations
3
+ module Request
4
+ module ClassMethods
5
+ def request(method, url, params = {}, opts = {})
6
+ MerchantZip::Client.new.execute_request(method, url, params, opts)
7
+ end
8
+ end
9
+
10
+ def self.included(base)
11
+ base.extend(ClassMethods)
12
+ end
13
+
14
+ protected
15
+
16
+ def request(method, url, params = {}, opts = {})
17
+ opts = @opts.merge(opts)
18
+ self.class.request(method, url, params, opts)
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,57 @@
1
+ module MerchantZip
2
+ module Util
3
+ # Encodes a hash of parameters in a way that's suitable for use as query
4
+ # parameters in a URI or as form parameters in a request body. This mainly
5
+ # involves escaping special characters from parameter keys and values (e.g.
6
+ # `&`).
7
+ def self.encode_parameters(params)
8
+ Util.flatten_params(params).
9
+ map { |k,v| "#{url_encode(k)}=#{url_encode(v)}" }.join('&')
10
+ end
11
+
12
+ # Encodes a string in a way that makes it suitable for use in a set of
13
+ # query parameters in a URI or in a set of form parameters in a request
14
+ # body.
15
+ def self.url_encode(key)
16
+ CGI.escape(key.to_s).
17
+ # Don't use strict form encoding by changing the square bracket control
18
+ # characters back to their literals. This is fine by the server, and
19
+ # makes these parameter strings easier to read.
20
+ gsub('%5B', '[').gsub('%5D', ']')
21
+ end
22
+
23
+ def self.flatten_params(params, parent_key=nil)
24
+ result = []
25
+
26
+ # do not sort the final output because arrays (and arrays of hashes
27
+ # especially) can be order sensitive, but do sort incoming parameters
28
+ params.each do |key, value|
29
+ calculated_key = parent_key ? "#{parent_key}[#{key}]" : "#{key}"
30
+ if value.is_a?(Hash)
31
+ result += flatten_params(value, calculated_key)
32
+ elsif value.is_a?(Array)
33
+ check_array_of_maps_start_keys!(value)
34
+ result += flatten_params_array(value, calculated_key)
35
+ else
36
+ result << [calculated_key, value]
37
+ end
38
+ end
39
+
40
+ result
41
+ end
42
+
43
+ def self.flatten_params_array(value, calculated_key)
44
+ result = []
45
+ value.each do |elem|
46
+ if elem.is_a?(Hash)
47
+ result += flatten_params(elem, "#{calculated_key}[]")
48
+ elsif elem.is_a?(Array)
49
+ result += flatten_params_array(elem, calculated_key)
50
+ else
51
+ result << ["#{calculated_key}[]", elem]
52
+ end
53
+ end
54
+ result
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,3 @@
1
+ module MerchantZip
2
+ VERSION = "0.0.2"
3
+ end
@@ -0,0 +1,34 @@
1
+ require "forwardable"
2
+
3
+ require "merchant-zip/operations/request"
4
+ require "merchant-zip/operations/create"
5
+ require "merchant-zip/operations/get"
6
+ require "merchant-zip/api_resource"
7
+ require "merchant-zip/configuration"
8
+ require "merchant-zip/version"
9
+ require "merchant-zip/util"
10
+
11
+ module MerchantZip
12
+ autoload :Client, "merchant-zip/client"
13
+ autoload :Checkout, "merchant-zip/checkout"
14
+ autoload :Charge, "merchant-zip/charge"
15
+
16
+ @config = Configuration.setup
17
+
18
+ class << self
19
+ extend Forwardable
20
+
21
+ attr_reader :config
22
+
23
+ # User configurable options
24
+ def_delegators :@config, :mode, :mode=
25
+ def_delegators :@config, :api_key, :api_key=
26
+
27
+ def configure(mode: :live, api_key: nil)
28
+ @config = Configuration.setup.reverse_duplicate_merge(
29
+ mode: mode,
30
+ api_key: api_key,
31
+ )
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,31 @@
1
+ $LOAD_PATH.unshift(::File.join(::File.dirname(__FILE__), "lib"))
2
+
3
+ require "merchant-zip/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "merchant-zip"
7
+ s.version = MerchantZip::VERSION
8
+ s.authors = ["Devhub"]
9
+ s.email = ["info@devhub.co"]
10
+ s.summary = "Ruby Library for Zip API"
11
+ s.description = "Merchant Zip provides Ruby APIs dealing with Zip transactions."
12
+ s.homepage = "https://github.com/DevHubCo/merchant-zip"
13
+ s.license = 'MIT'
14
+
15
+ ignored = Regexp.union(
16
+ /\A\.editorconfig/,
17
+ /\A\.git/,
18
+ /\A\.rubocop/,
19
+ /\A\.travis.yml/,
20
+ /\A\.vscode/,
21
+ /\Atest/
22
+ )
23
+
24
+ puts `git ls-files`.split("\n").reject { |f| ignored.match(f) }
25
+ s.files = `git ls-files`.split("\n").reject { |f| ignored.match(f) }
26
+ s.executables = `git ls-files -- bin/*`.split("\n")
27
+ .map { |f| ::File.basename(f) }
28
+ s.require_paths = ["lib"]
29
+
30
+ s.add_dependency 'rest-client', '~> 2.0', '>= 2.0.0'
31
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: merchant-zip
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Devhub
@@ -33,10 +33,25 @@ dependencies:
33
33
  description: Merchant Zip provides Ruby APIs dealing with Zip transactions.
34
34
  email:
35
35
  - info@devhub.co
36
- executables: []
36
+ executables:
37
+ - console
37
38
  extensions: []
38
39
  extra_rdoc_files: []
39
- files: []
40
+ files:
41
+ - LICENSE
42
+ - bin/console
43
+ - lib/merchant-zip.rb
44
+ - lib/merchant-zip/api_resource.rb
45
+ - lib/merchant-zip/charge.rb
46
+ - lib/merchant-zip/checkout.rb
47
+ - lib/merchant-zip/client.rb
48
+ - lib/merchant-zip/configuration.rb
49
+ - lib/merchant-zip/operations/create.rb
50
+ - lib/merchant-zip/operations/get.rb
51
+ - lib/merchant-zip/operations/request.rb
52
+ - lib/merchant-zip/util.rb
53
+ - lib/merchant-zip/version.rb
54
+ - merchant-zip.gemspec
40
55
  homepage: https://github.com/DevHubCo/merchant-zip
41
56
  licenses:
42
57
  - MIT