Class: Bootpay::Commerce::CommerceResource

Inherits:
Object
  • Object
show all
Defined in:
lib/bootpay/commerce/commerce_resource.rb

Direct Known Subclasses

Api

Constant Summary collapse

API_ENTRYPOINTS =
{
  'development' => 'https://dev-api.bootapi.com/v1',
  'stage'       => 'https://stage-api.bootapi.com/v1',
  'production'  => 'https://api.bootapi.com/v1'
}.freeze
API_VERSION =
'1.0.0'
SDK_VERSION =
'1.0.0'
SDK_TYPE =

Ruby

'305'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCommerceResource

Returns a new instance of CommerceResource.



22
23
24
25
26
27
28
29
# File 'lib/bootpay/commerce/commerce_resource.rb', line 22

def initialize
  @mode       = 'production'
  @token      = nil
  @role       = 'user'
  @client_key = nil
  @secret_key = nil
  @timeout    = 60
end

Instance Attribute Details

#client_keyObject (readonly)

Returns the value of attribute client_key.



20
21
22
# File 'lib/bootpay/commerce/commerce_resource.rb', line 20

def client_key
  @client_key
end

#modeObject (readonly)

Returns the value of attribute mode.



20
21
22
# File 'lib/bootpay/commerce/commerce_resource.rb', line 20

def mode
  @mode
end

#secret_keyObject (readonly)

Returns the value of attribute secret_key.



20
21
22
# File 'lib/bootpay/commerce/commerce_resource.rb', line 20

def secret_key
  @secret_key
end

Instance Method Details

#delete(url, params = nil) ⇒ Object

DELETE request



132
133
134
135
136
137
138
139
140
# File 'lib/bootpay/commerce/commerce_resource.rb', line 132

def delete(url, params = nil)
  full_url = entrypoint(url)
  response = HTTP.headers(default_headers)
                 .timeout(@timeout)
                 .delete(full_url, params: params)
  parse_response(response)
rescue StandardError => e
  error_response(e)
end

#get(url, params = nil) ⇒ Object

GET request



55
56
57
58
59
60
61
62
63
# File 'lib/bootpay/commerce/commerce_resource.rb', line 55

def get(url, params = nil)
  full_url = entrypoint(url)
  response = HTTP.headers(default_headers)
                 .timeout(@timeout)
                 .get(full_url, params: params)
  parse_response(response)
rescue StandardError => e
  error_response(e)
end

#get_roleObject



50
51
52
# File 'lib/bootpay/commerce/commerce_resource.rb', line 50

def get_role
  @role || 'user'
end

#get_tokenObject



42
43
44
# File 'lib/bootpay/commerce/commerce_resource.rb', line 42

def get_token
  @token
end

#post(url, data = nil) ⇒ Object

POST request with JSON body



66
67
68
69
70
71
72
73
74
# File 'lib/bootpay/commerce/commerce_resource.rb', line 66

def post(url, data = nil)
  full_url = entrypoint(url)
  response = HTTP.headers(default_headers)
                 .timeout(@timeout)
                 .post(full_url, json: data || {})
  parse_response(response)
rescue StandardError => e
  error_response(e)
end

#post_multipart(url, data = {}, image_paths = nil) ⇒ Object

POST multipart/form-data (for product images)



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/bootpay/commerce/commerce_resource.rb', line 90

def post_multipart(url, data = {}, image_paths = nil)
  headers = multipart_headers
  full_url = entrypoint(url)

  form_data = {}
  data.each do |key, value|
    next if value.nil?
    if value.is_a?(Hash) || value.is_a?(Array)
      form_data[key.to_s] = JSON.generate(value)
    else
      form_data[key.to_s] = value.to_s
    end
  end

  if image_paths && !image_paths.empty?
    files = image_paths.map do |path|
      HTTP::FormData::File.new(path)
    end
    form_data['images'] = files.length == 1 ? files.first : files
  end

  form = HTTP::FormData::Multipart.new(form_data)
  response = HTTP.headers(headers)
                 .timeout(@timeout)
                 .post(full_url, form: form)
  parse_response(response)
rescue StandardError => e
  error_response(e)
end

#post_with_basic_auth(url, data = nil) ⇒ Object

POST with Basic Auth (for token endpoint)



77
78
79
80
81
82
83
84
85
86
87
# File 'lib/bootpay/commerce/commerce_resource.rb', line 77

def post_with_basic_auth(url, data = nil)
  headers = default_headers(include_auth: false)
  headers['Authorization'] = basic_auth_header
  full_url = entrypoint(url)
  response = HTTP.headers(headers)
                 .timeout(@timeout)
                 .post(full_url, json: data || {})
  parse_response(response)
rescue StandardError => e
  error_response(e)
end

#put(url, data = nil) ⇒ Object

PUT request



121
122
123
124
125
126
127
128
129
# File 'lib/bootpay/commerce/commerce_resource.rb', line 121

def put(url, data = nil)
  full_url = entrypoint(url)
  response = HTTP.headers(default_headers)
                 .timeout(@timeout)
                 .put(full_url, json: data || {})
  parse_response(response)
rescue StandardError => e
  error_response(e)
end

#set_configuration(client_key:, secret_key:, mode: 'production') ⇒ Object

Raises:

  • (ArgumentError)


31
32
33
34
35
36
# File 'lib/bootpay/commerce/commerce_resource.rb', line 31

def set_configuration(client_key:, secret_key:, mode: 'production')
  @client_key = client_key
  @secret_key = secret_key
  @mode       = mode || 'production'
  raise ArgumentError, "mode는 development, stage, production 중에서 선택이 가능합니다." unless API_ENTRYPOINTS.key?(@mode)
end

#set_role(role) ⇒ Object



46
47
48
# File 'lib/bootpay/commerce/commerce_resource.rb', line 46

def set_role(role)
  @role = role
end

#set_token(token) ⇒ Object



38
39
40
# File 'lib/bootpay/commerce/commerce_resource.rb', line 38

def set_token(token)
  @token = token
end