Class: Barion::Config

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/barion/config.rb

Overview

Encloses all Barion related configuration

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#acronymString

The acronym set up in the initalizer

Returns:

  • (String)

    the acronym provided by Barion (default: ”)



36
37
38
# File 'lib/barion/config.rb', line 36

def acronym
  @acronym || ''
end

#default_payeeString?

Set this to the registered email address of your shop

Returns:

  • (String, nil)

    registered email address for your Barion shop (default: nil)



17
18
19
# File 'lib/barion/config.rb', line 17

def default_payee
  @default_payee
end

#pixel_idObject

Returns the value of attribute pixel_id.



25
26
27
# File 'lib/barion/config.rb', line 25

def pixel_id
  @pixel_id
end

#poskeyObject



30
31
32
# File 'lib/barion/config.rb', line 30

def poskey
  @_poskey || nil
end

#publickeyString?

Set this to the string provided by Barion registration

Returns:

  • (String, nil)

    the public key provided by Barion (default: nil)



13
14
15
# File 'lib/barion/config.rb', line 13

def publickey
  @publickey
end

Instance Method Details

#endpointObject

Returns the REST client endpoint based on the current environment.

Determines the environment by checking if the sandbox mode is enabled. If sandbox mode is enabled, the test environment URL is used; otherwise, the production environment URL is used.

Returns:

  • (Object)

    a new instance of the REST client class initialized with the appropriate base URL for the current environment.



109
110
111
112
# File 'lib/barion/config.rb', line 109

def endpoint
  env = sandbox? ? :test : :prod
  rest_client_class.new ::Barion::BASE_URL[env]
end

#item_classClass

Returns the item class set in the initializer.

This method returns nil until the initializer is installed.

Returns:

  • (Class)

    the class set in the initializer



154
155
156
157
158
159
# File 'lib/barion/config.rb', line 154

def item_class
  # This is nil before the initializer is installed.
  return nil if @_item_class.nil?

  @_item_class.constantize
end

#item_class=(class_name) ⇒ Object

Sets the class used for item representation.

Parameters:

  • val (Class)

    the class to use for item representation



141
142
143
144
145
146
147
# File 'lib/barion/config.rb', line 141

def item_class=(class_name)
  unless class_name.is_a?(String)
    raise ArgumentError, "::Barion.config.item_class must be set to a ::String, got #{class_name.inspect}"
  end

  @_item_class = class_name
end

#resetObject

Resets all configuration options to nil.

It is useful when you want to restore the default configuration as the class is a Singleton.



188
189
190
191
192
193
194
195
196
197
# File 'lib/barion/config.rb', line 188

def reset
  @_poskey = nil
  @_sandbox = nil
  @_user_class = nil
  @_item_class = nil
  @_rest_client_class = nil
  @acronym = nil
  @pixel_id = nil
  @publickey = nil
end

#rest_client_classClass

Returns the class used for making REST calls to the Barion API.

If it was not set explicitly, it defaults to ‘::RestClient::Resource`.

Returns:

  • (Class)

    the class used for making REST calls



166
167
168
# File 'lib/barion/config.rb', line 166

def rest_client_class
  (@_rest_client_class || '::RestClient::Resource').constantize
end

#rest_client_class=(class_name) ⇒ Object

Sets the class used for making REST calls to the Barion API.

The class set with this method is used for making REST calls to the Barion API. It should be set to a class that responds to the same methods as ‘::RestClient::Resource`.

Parameters:

  • class_name (Class, String)

    the class to use for making REST calls



177
178
179
180
181
182
183
# File 'lib/barion/config.rb', line 177

def rest_client_class=(class_name)
  unless class_name.is_a?(String)
    raise ArgumentError, "::Barion::Config.rest_client_class must be set to a ::String, got #{class_name.inspect}"
  end

  @_rest_client_class = class_name
end

#sandboxBoolean

Returns the current sandbox mode setting. Delegates to the sandbox? method to determine the current state.

Returns:

  • (Boolean)

    true if in sandbox mode, false otherwise



57
58
59
# File 'lib/barion/config.rb', line 57

def sandbox
  sandbox?
end

#sandbox=(val) ⇒ Object

Sets the sandbox mode.

Parameters:

  • val (Boolean)

    the desired state for sandbox mode true to enable sandbox mode, false to disable it



75
76
77
# File 'lib/barion/config.rb', line 75

def sandbox=(val)
  @_sandbox = !!val
end

#sandbox?Boolean

Returns the current sandbox mode setting.

If the sandbox mode was not set explicitly, it defaults to true.

Returns:

  • (Boolean)

    true if in sandbox mode, false otherwise



66
67
68
69
# File 'lib/barion/config.rb', line 66

def sandbox?
  @_sandbox = true if @_sandbox.nil?
  @_sandbox
end

#user_classClass

Returns the user class set in the initializer.

Returns:

  • (Class)

    the class set in the initializer



131
132
133
134
135
136
# File 'lib/barion/config.rb', line 131

def user_class
  # This is nil before the initializer is installed.
  return nil if @_user_class.nil?

  @_user_class.constantize
end

#user_class=(class_name) ⇒ Object

Sets the user class used for user authentication.

Validates that the provided user class is a string.

Parameters:

  • class_name (String)

    the user class to use for user authentication

Raises:

  • (ArgumentError)

    if the user class is not a string



120
121
122
123
124
125
126
# File 'lib/barion/config.rb', line 120

def user_class=(class_name)
  unless class_name.is_a?(String)
    raise ArgumentError, "::Barion:.config.user_class must be set to a ::String, got #{class_name.inspect}"
  end

  @_user_class = class_name
end