Class: Square::BaseApi

Inherits:
Object
  • Object
show all
Defined in:
lib/square/api/base_api.rb

Overview

BaseApi.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config, http_call_back: nil) ⇒ BaseApi

Returns a new instance of BaseApi.



7
8
9
10
11
12
13
14
15
# File 'lib/square/api/base_api.rb', line 7

def initialize(config, http_call_back: nil)
  @config = config
  @http_call_back = http_call_back

  @global_headers = {
    'user-agent' => get_user_agent,
    'Square-Version' => config.square_version
  }
end

Instance Attribute Details

#configObject

Returns the value of attribute config.



5
6
7
# File 'lib/square/api/base_api.rb', line 5

def config
  @config
end

#http_call_backObject

Returns the value of attribute http_call_back.



5
6
7
# File 'lib/square/api/base_api.rb', line 5

def http_call_back
  @http_call_back
end

Instance Method Details

#execute_request(request, binary: false) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/square/api/base_api.rb', line 23

def execute_request(request, binary: false)
  @http_call_back&.on_before_request(request)

  APIHelper.clean_hash(request.headers)
  request.headers.merge!(@global_headers)
  request.headers.merge!(config.additional_headers) unless config.additional_headers.nil?

  response = if binary
               config.http_client.execute_as_binary(request)
             else
               config.http_client.execute_as_string(request)
             end
  @http_call_back&.on_after_response(response)

  response
end

#get_user_agentObject



40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/square/api/base_api.rb', line 40

def get_user_agent
  user_agent = 'Square-Ruby-SDK/20.1.0.20220616 ({api-version}) {engine}/{engine-version} ({os-info}) {detail}'
  user_agent['{engine}'] = RUBY_ENGINE
  user_agent['{engine-version}'] = RUBY_ENGINE_VERSION
  user_agent['{os-info}'] = RUBY_PLATFORM
  user_agent['{api-version}'] = config.square_version
  if config.user_agent_detail.nil? || config.user_agent_detail.empty?
    user_agent = user_agent.gsub('{detail}', '')
  else
    user_agent['{detail}'] = ERB::Util.url_encode(config.user_agent_detail.to_s)
  end
  user_agent
end

#validate_parameters(args) ⇒ Object



17
18
19
20
21
# File 'lib/square/api/base_api.rb', line 17

def validate_parameters(args)
  args.each do |_name, value|
    raise ArgumentError, "Required parameter #{_name} cannot be nil." if value.nil?
  end
end