shipay 0.1.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,134 @@
1
+ module Shipay
2
+
3
+ #
4
+ # Class to manage Tokens with singleton structure
5
+ #
6
+ class TokenManager
7
+ attr_reader :authenticators, :mutex
8
+
9
+ # private_class_method :new
10
+
11
+ #
12
+ # Initializes TokenManager
13
+ #
14
+ # This class builds authentication array with a mutex to share tokens
15
+ def initialize()
16
+ tokens = nil
17
+ if Shipay.credentials
18
+ case Shipay.credentials
19
+ when Array
20
+ tokens = Shipay.credentials
21
+ when Hash
22
+ tokens = [ShiShipay.credentials]
23
+ end
24
+ else
25
+ tokens = [{
26
+ secret_key: Shipay.secret_key,
27
+ access_key: Shipay.access_key,
28
+ client_id: Shipay.client_id,
29
+ key: :default,
30
+ default: true
31
+ }]
32
+ end
33
+
34
+ @mutex = Mutex.new
35
+ @authenticators = nil
36
+ setup_autenticators tokens
37
+ end
38
+
39
+ #
40
+ # Sets authenticators based on tokens passed by constructor
41
+ #
42
+ # @param [Array] tokens Array of tokens to be registered as clients
43
+ #
44
+ # @return [Array] Authenticators array
45
+ #
46
+ def setup_autenticators tokens
47
+ return @authenticators if @authenticators
48
+ tokens = tokens.map{|t| Shipay::Client.new(**t)}
49
+ @mutex.synchronize do
50
+ @authenticators = []
51
+ tokens.each do |client|
52
+ @authenticators << Authenticator.new(client)
53
+ end
54
+ end
55
+ end
56
+
57
+ #
58
+ # Find a token for a specific Client Key
59
+ #
60
+ # @param [Symbol] key Client Key to be found in Authenticators Array
61
+ #
62
+ # @return [String] Auth token
63
+ #
64
+ def self.token_for(key = Shipay.default_client_key)
65
+ self.instance unless @instance
66
+ k = Shipay::Util.to_sym(key)
67
+ raise MissingCredentialsError.new("Missing credentials for key: '#{key}'") unless @instance.authenticators
68
+
69
+ @instance.mutex.synchronize do
70
+ auth = @instance.authenticators.find { |obj| obj.key == k}
71
+
72
+ raise MissingCredentialsError.new("Missing credentials for key: '#{key}'") if auth.blank?
73
+ auth.token
74
+ end
75
+ end
76
+
77
+ #
78
+ # Registers a new client to be used
79
+ #
80
+ # @param [Shipay::Client] client Client instance to be registered in TokenManager
81
+ #
82
+ # @return [Array] Authenticators array
83
+ # @example Ads a new client to be used in calls to Shipay Api
84
+ # Shipay::TokenManager.add_client Client.new(client_id: <CLIENT_KEY>, key: :<CLIENT_ALIAS>)
85
+ def self.add_client client
86
+ self.instance unless @instance
87
+ client = (client.is_a? Shipay::Client)? client : Shipay::Client.new(**client)
88
+
89
+ raise ParamError.new("Client key '#{client.key}' already exists", 'Key', '') if self.client_for client.key
90
+
91
+ @instance.mutex.synchronize do
92
+ @instance.authenticators << Authenticator.new(client)
93
+ end
94
+ end
95
+
96
+
97
+
98
+ #
99
+ # Find a Client for a specific Key
100
+ #
101
+ # @param [Symbol] key Client Key to be found in Authenticators Array ( Defaults to Shipay.default_client_key)
102
+ #
103
+ # @return [Shipay::Client] Client instance registed with the key passed
104
+ #
105
+ def self.client_for(key = Shipay.default_client_key)
106
+ k = Shipay::Util.to_sym(key)
107
+ self.instance unless @instance
108
+ return nil unless @instance.authenticators.present?
109
+
110
+ @instance.mutex.synchronize do
111
+ auth = @instance.authenticators.find { |obj| obj.key == k}
112
+ auth&.client
113
+ end
114
+ end
115
+
116
+ #
117
+ # Find a Client Type for a specific Key
118
+ #
119
+ # @param [Symbol] key Client Key to be found in Authenticators Array ( Defaults to Shipay.default_client_key)
120
+ #
121
+ # @return [Symbol] Return the cleint type ( :pdv or :e_commerce) ( Defaults to :pdv if not found)
122
+ #
123
+ def self.client_type_for key = Shipay.default_client_key
124
+ client_for(key)&.type || :pdv
125
+ end
126
+
127
+ def self.instance
128
+ return @instance if @instance
129
+
130
+ @instance = TokenManager.new
131
+ end
132
+ end
133
+ end
134
+
@@ -0,0 +1,67 @@
1
+ module Shipay
2
+ class Util
3
+ class << self
4
+
5
+ SINGULARS = {
6
+ '/s$/i' => "",
7
+ '/(ss)$/i' => '\1',
8
+ '/(n)ews$/i' => '\1ews',
9
+ '/([ti])a$/i' => '\1um',
10
+ '/((a)naly|(b)a|(d)iagno|(p)arenthe|(p)rogno|(s)ynop|(t)he)(sis|ses)$/i' => '\1sis',
11
+ '/(^analy)(sis|ses)$/i' => '\1sis',
12
+ '/([^f])ves$/i' => '\1fe',
13
+ '/(hive)s$/i' => '\1',
14
+ '/(tive)s$/i' => '\1',
15
+ '/([lr])ves$/i' => '\1f',
16
+ '/([^aeiouy]|qu)ies$/i' => '\1y',
17
+ '/(s)eries$/i' => '\1eries',
18
+ '/(m)ovies$/i' => '\1ovie',
19
+ '/(x|ch|ss|sh)es$/i' => '\1',
20
+ '/^(m|l)ice$/i' => '\1ouse',-
21
+ '/(bus)(es)?$/i' => '\1',
22
+ '/(o)es$/i' => '\1',
23
+ '/(shoe)s$/i' => '\1',
24
+ '/(cris|test)(is|es)$/i' => '\1is',
25
+ '/^(a)x[ie]s$/i' => '\1xis',
26
+ '/(octop|vir)(us|i)$/i' => '\1us',
27
+ '/(alias|status)(es)?$/i' => '\1',
28
+ '/^(ox)en/i' => '\1',
29
+ '/(vert|ind)ices$/i' => '\1ex',
30
+ '/(matr)ices$/i' => '\1ix',
31
+ '/(quiz)zes$/i' => '\1',
32
+ '/(database)s$/i' => '\1'}
33
+
34
+ def singularize resource
35
+ out = ''
36
+ SINGULARS.keys.each do |key|
37
+ out = resource.to_s.gsub(/s$/,SINGULARS[key])
38
+ break out if out != resource
39
+ end
40
+ case resource.class
41
+ when Symbol
42
+ return out.to_sym
43
+ end
44
+ out
45
+ end
46
+
47
+ def to_sym string
48
+ string.to_s.strip.gsub(' -', '_').to_sym
49
+ end
50
+ end
51
+ end
52
+ end
53
+
54
+ class Hash
55
+ def except_nested(key)
56
+ r = Marshal.load(Marshal.dump(self))
57
+ r.except_nested!(key)
58
+ end
59
+
60
+ def except_nested!(key)
61
+ self.reject!{|k, _| k == key || k.to_s == key }
62
+ self.each do |_, v|
63
+ v.except_nested!(key) if v.is_a?(Hash)
64
+ v.map!{|obj| obj.except_nested!(key) if obj.is_a?(Hash)} if v.is_a?(Array)
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Shipay
4
+ VERSION = "0.1.5"
5
+ end
data/lib/shipay.rb ADDED
@@ -0,0 +1,43 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "shipay/version"
4
+ require_relative "shipay/authenticator"
5
+ require_relative "shipay/request"
6
+ require_relative "shipay/object"
7
+ require_relative "shipay/model"
8
+ require_relative "shipay/core_ext"
9
+ require_relative "shipay/errors"
10
+ require_relative "shipay/util"
11
+ require_relative "shipay/token_manager"
12
+ require_relative "shipay/order_commom"
13
+
14
+
15
+ Dir[File.expand_path('../shipay/resources/*.rb', __FILE__)].map do |path|
16
+ require path
17
+ end
18
+
19
+ module Shipay
20
+ class Error < StandardError; end
21
+
22
+ class << self
23
+ attr_accessor :access_key, :secret_key, :client_id, :callback_url, :credentials, :default_client_key
24
+ attr_reader :api_endpoint
25
+
26
+ def production?
27
+ ENV["RACK_ENV"] == "production" ||
28
+ ENV["RAILS_ENV"] == "production" ||
29
+ ENV["PRODUCTION"] ||
30
+ ENV["production"] || (Rails.env.production? if Object.const_defined?('::Rails'))
31
+
32
+ rescue NameError => e
33
+ return false
34
+ end
35
+ end
36
+
37
+ @default_client_key = :default
38
+
39
+ @api_endpoint = ( Shipay.production? )? "https://api.shipay.com.br" : "https://api-staging.shipay.com.br"
40
+
41
+ puts "Running on production" if production?
42
+
43
+ end
data/main.rb ADDED
@@ -0,0 +1,45 @@
1
+ require_relative 'lib/shipay'
2
+
3
+ Shipay.secret_key = 'Q__g6ie850Pv4jVBQUeyAMA_mlJH3xG1Acg2FiPU_lYpVol9z69RHmOE4BZDMaHVGsMEg1s9BI5pvgKkCAWDdw'
4
+ Shipay.access_key='CyUqG2hSfQJbE8OTWfy1fQ'
5
+ Shipay.client_id='ubceuMCPeVygHW8xynrFYzbF3JKE1zlnpjNBReM6gDz0jzQ-5clVgRdtRRDJ5yWENkGMnC3VI2wQSyu_507g5c9ujS6D5eOSBVheKbMVbEW8qk8dYb41yqgl7o9xv-puarpHV7Dk3jv6n6_HZlAl4S8gHjNsayuj2RSqT8AbtY4'
6
+
7
+
8
+ # Shipay::Authenticator.instance()
9
+
10
+ # Shipay::Authenticator.headers
11
+ sh = Shipay::Order.new({
12
+ order_ref: "shipaypag-001",
13
+ wallet: "shipay-pagador",
14
+ total: 0.51,
15
+ items: [
16
+ {
17
+ item_title: "Item 1",
18
+ unit_price: 0.30,
19
+ quantity: 1
20
+ },
21
+ {
22
+ item_title: "Item 2",
23
+ unit_price: 0.20,
24
+ quantity: 1
25
+ },
26
+ {
27
+ item_title: "Item 3",
28
+ unit_price: 0.01,
29
+ quantity: 1
30
+ }
31
+ ],
32
+ buyer: {
33
+ name: "Shipay PDV",
34
+ cpf_cnpj: "121.191.870-02",
35
+ email: "shipay-pagador@shipay.com.br",
36
+ phone: "+55 11 99999-9999"
37
+ }
38
+ }).create
39
+
40
+ puts sh
41
+ # Shipay.api_endpoint='https://postman-echo.com/get'
42
+
43
+ ord = Shipay::Order.find_by_id(sh.order_id)
44
+
45
+ puts ord
metadata ADDED
@@ -0,0 +1,120 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: shipay
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.5
5
+ platform: ruby
6
+ authors:
7
+ - Guilherme Gazzinelli
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2023-11-08 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: jwt
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: rest-client
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: multi_json
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
+ description: Gem para integração de pagamento via pix e carteira digitais da Shipay
56
+ email:
57
+ - guilherme.gazzinelli@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".byebug_history"
63
+ - ".editorconfig"
64
+ - ".rspec"
65
+ - ".rubocop.yml"
66
+ - ".solargraph.yml"
67
+ - ".vscode/launch.json"
68
+ - CHANGELOG.md
69
+ - CODE_OF_CONDUCT.md
70
+ - Gemfile
71
+ - Gemfile.lock
72
+ - README.md
73
+ - Rakefile
74
+ - bin/console
75
+ - bin/setup
76
+ - lib/shipay.rb
77
+ - lib/shipay/authenticator.rb
78
+ - lib/shipay/core_ext.rb
79
+ - lib/shipay/errors.rb
80
+ - lib/shipay/generators/install_generator.rb
81
+ - lib/shipay/model.rb
82
+ - lib/shipay/object.rb
83
+ - lib/shipay/order_commom.rb
84
+ - lib/shipay/request.rb
85
+ - lib/shipay/resources/buyer_info.rb
86
+ - lib/shipay/resources/item.rb
87
+ - lib/shipay/resources/order.rb
88
+ - lib/shipay/resources/order_bacen.rb
89
+ - lib/shipay/resources/order_v.rb
90
+ - lib/shipay/resources/wallet.rb
91
+ - lib/shipay/resources/wallet_list.rb
92
+ - lib/shipay/token_manager.rb
93
+ - lib/shipay/util.rb
94
+ - lib/shipay/version.rb
95
+ - main.rb
96
+ homepage: https://github.com/guilhermegazzinelli/shipay
97
+ licenses:
98
+ - MIT
99
+ metadata:
100
+ homepage_uri: https://github.com/guilhermegazzinelli/shipay
101
+ post_install_message:
102
+ rdoc_options: []
103
+ require_paths:
104
+ - lib
105
+ required_ruby_version: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - ">="
108
+ - !ruby/object:Gem::Version
109
+ version: 2.6.0
110
+ required_rubygems_version: !ruby/object:Gem::Requirement
111
+ requirements:
112
+ - - ">="
113
+ - !ruby/object:Gem::Version
114
+ version: '0'
115
+ requirements: []
116
+ rubygems_version: 3.4.21
117
+ signing_key:
118
+ specification_version: 4
119
+ summary: Gem para integração com a Api da Shipay
120
+ test_files: []