Class: Veritrans::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/veritrans/config.rb

Instance Method Summary collapse

Constructor Details

#initialize(options = nil) ⇒ Config

Returns a new instance of Config.



9
10
11
12
# File 'lib/veritrans/config.rb', line 9

def initialize(options = nil)
  @api_host = "https://api.sandbox.midtrans.com"
  apply(options) if options
end

Instance Method Details

#api_hostObject



52
53
54
# File 'lib/veritrans/config.rb', line 52

def api_host
  @api_host
end

#api_host=(value) ⇒ Object

API Server hostname, this allow to switch between production and sandbox

Should be “api.sandbox.midtrans.com” or “api.midtrans.com

Default is “api.sandbox.midtrans.com



48
49
50
# File 'lib/veritrans/config.rb', line 48

def api_host=(value)
  @api_host = value
end

#client_keyObject



24
25
26
# File 'lib/veritrans/config.rb', line 24

def client_key
  @client_key
end

#client_key=(value) ⇒ Object

Merhcant’s Client key, used to make getToken request. (only for VT-Direct)

Can be found in merchant portal: Settings -> Access Keys



19
20
21
# File 'lib/veritrans/config.rb', line 19

def client_key=(value)
  @client_key = value
end

#http_optionsObject



82
83
84
# File 'lib/veritrans/config.rb', line 82

def http_options
  @http_options
end

#http_options=(options) ⇒ Object

This will override http request settings for api calls. http_options should be hash, it will be merged with connection options for every request.

Full list of options: github.com/excon/excon/blob/master/lib/excon/constants.rb

For unsupported key it will raise ArgumentError

Veritrans.config.http_options = {tcp_nodelay: true, ssl_version: 'TLSv1'}


66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/veritrans/config.rb', line 66

def http_options=(options)
  unless options.is_a?(Hash)
    raise ArgumentError, "http_options should be a hash"
  end

  # Validate allowed keys
  diff = options.keys.map(&:to_sym) - Excon::VALID_CONNECTION_KEYS
  if diff.size > 0
    raise ArgumentError,
      "http_options contain unsupported keys: #{diff.inspect}\n" +
      "Supported keys are: #{Excon::VALID_CONNECTION_KEYS.inspect}"
  end

  @http_options = options
end

#inspectObject



133
134
135
136
137
138
139
# File 'lib/veritrans/config.rb', line 133

def inspect
  "<Veritrans::Config " +
    "@api_host=#{@api_host.inspect} " +
    "@server_key=#{@server_key.inspect} " +
    "@client_key=#{@client_key.inspect} " +
    "@http_options=#{@http_options.inspect}>"
end

#load_config(filename, yml_section = nil) ⇒ Object Also known as: load_yml

Loads YAML file and assign config values

Supports #section in filename to choose one section. If you are using Rails, it will try to use Rails.env as a section name

Available config keys: server_key, client_key, api_host, http_options

Veritrans.setup do
  config.load_yml "#{Rails.root.to_s}/config/veritrans.yml#development"
  # or
  config.load_yml "#{Rails.root.to_s}/config/veritrans.yml", :development
end


100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/veritrans/config.rb', line 100

def load_config(filename, yml_section = nil)
  yml_file, file_yml_section = filename.to_s.split('#')
  config_data = YAML.load(ERB.new(File.read(yml_file)).result)

  yml_section ||= file_yml_section
  if defined?(Rails) && !yml_section
    yml_section = Rails.env.to_s
  end

  if yml_section && !config_data.has_key?(yml_section)
    STDERR.puts "Veritrans: Can not find section #{yml_section.inspect} in file #{yml_file}"
    STDERR.puts "           Available sections: #{config_data.keys}"

    if config_data['development'] && config_data['development']['server_key']
      new_section = 'development'
    end

    first_key = config_data.keys.first
    if config_data[first_key]['server_key']
      new_section = first_key
    end

    if new_section
      STDERR.puts "Veritrans: Using first section #{new_section.inspect}"
      yml_section = new_section
    end
  end

  apply(yml_section ? config_data[yml_section] : config_data)
end

#server_keyObject



37
38
39
# File 'lib/veritrans/config.rb', line 37

def server_key
  @server_key
end

#server_key=(value) ⇒ Object

Merhcant’s Server key, used to sign every http API call.

Can be found in merchant portal: Settings -> Access Keys



33
34
35
# File 'lib/veritrans/config.rb', line 33

def server_key=(value)
  @server_key = value
end