Class: StraightServer::GatewayOnConfig

Inherits:
Object
  • Object
show all
Includes:
Straight::GatewayModule, GatewayModule
Defined in:
lib/straight-server/gateway.rb

Overview

Uses a config file to load attributes and a special _last_keychain_id file to store last_keychain_id

Constant Summary collapse

@@gateways =

This will later be used in the #find_by_id. Because we don’t use a DB, the id will actually be the index of an element in this Array. Thus, the order in which gateways follow in the config file is important.

[]

Constants included from GatewayModule

StraightServer::GatewayModule::CALLBACK_URL_ATTEMPT_TIMEFRAME

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from GatewayModule

#add_websocket_for_order, #create_order, #fetch_transactions_for, #find_reusable_order, #get_next_last_keychain_id, #get_order_counter, #increment_order_counter!, #initialize_blockchain_adapters, #initialize_callbacks, #initialize_exchange_rate_adapters, #initialize_network, #initialize_status_check_schedule, #order_counters, #order_status_changed, #send_order_to_websocket_client, #sign_with_secret, #update_last_keychain_id, #websockets

Constructor Details

#initializeGatewayOnConfig

Returns a new instance of GatewayOnConfig.



520
521
522
523
524
525
526
# File 'lib/straight-server/gateway.rb', line 520

def initialize
  initialize_callbacks
  initialize_exchange_rate_adapters
  initialize_blockchain_adapters
  initialize_status_check_schedule
  initialize_network
end

Instance Attribute Details

#activeObject

This affects whether it is possible to create a new order with the gateway. If it’s set to false, then it won’t be possible to create a new order, but it will keep checking on the existing ones.



514
515
516
# File 'lib/straight-server/gateway.rb', line 514

def active
  @active
end

#callback_urlObject

A url to which the gateway will send an HTTP request with the status of the order data (in JSON) when the status of the order is changed. The response should always be 200, otherwise the gateway will awesome something went wrong and will keep trying to send requests to this url according to a specific shedule.



502
503
504
# File 'lib/straight-server/gateway.rb', line 502

def callback_url
  @callback_url
end

#check_signatureObject

If set to false, doesn’t require an unique id of the order along with the signed md5 hash of that id + secret to be passed into the #create_order method.



496
497
498
# File 'lib/straight-server/gateway.rb', line 496

def check_signature
  @check_signature
end

#exchange_rate_adapter_namesObject

Returns the value of attribute exchange_rate_adapter_names.



508
509
510
# File 'lib/straight-server/gateway.rb', line 508

def exchange_rate_adapter_names
  @exchange_rate_adapter_names
end

#idObject

This will be assigned the number that is the order in which this gateway follows in the config file.



506
507
508
# File 'lib/straight-server/gateway.rb', line 506

def id
  @id
end

#last_keychain_idObject

This is used to generate the next address to accept payments



492
493
494
# File 'lib/straight-server/gateway.rb', line 492

def last_keychain_id
  @last_keychain_id
end

#orders_expiration_periodObject

Returns the value of attribute orders_expiration_period.



509
510
511
# File 'lib/straight-server/gateway.rb', line 509

def orders_expiration_period
  @orders_expiration_period
end

#secretObject

This is the key that allows users (those, who use the gateway, online stores, for instance) to connect and create orders. It is not used directly, but is mixed with all the params being sent and a MD5 hash is calculted. Then the gateway checks whether the MD5 hash is correct.



489
490
491
# File 'lib/straight-server/gateway.rb', line 489

def secret
  @secret
end

#test_last_keychain_idObject

This is used to generate the next address to accept payments



492
493
494
# File 'lib/straight-server/gateway.rb', line 492

def test_last_keychain_id
  @test_last_keychain_id
end

Class Method Details

.find_by_hashed_id(s) ⇒ Object



516
517
518
# File 'lib/straight-server/gateway.rb', line 516

def self.find_by_hashed_id(s)
  self.find_by_id(s)
end

.find_by_id(id) ⇒ Object

This method is a replacement for the Sequel’s model one used in DB version of the gateway and it finds gateways using the index of @@gateways Array.



572
573
574
# File 'lib/straight-server/gateway.rb', line 572

def self.find_by_id(id)
  @@gateways[id.to_i-1]
end

Instance Method Details

#address_providerObject



566
567
568
# File 'lib/straight-server/gateway.rb', line 566

def address_provider
  Kernel.const_get("Straight::AddressProvider::#{address_provider_type}").new(self)
end

#address_provider_typeObject



562
563
564
# File 'lib/straight-server/gateway.rb', line 562

def address_provider_type
  @address_provider ? @address_provider.to_sym : :Bip32
end

#build_keychain_pathObject



557
558
559
560
# File 'lib/straight-server/gateway.rb', line 557

def build_keychain_path
  filename = self.test_mode ? "/#{name}_test_last_keychain_id" : "/#{name}_last_keychain_id"
  StraightServer::Initializer::ConfigDir.path + filename
end

#load_last_keychain_id!Object

Loads last_keychain_id from a file in the .straight dir. If the file doesn’t exist, we create it. Later, whenever an attribute is updated, we save it to the file.



542
543
544
545
546
547
548
549
550
# File 'lib/straight-server/gateway.rb', line 542

def load_last_keychain_id!
  @last_keychain_id_file ||= build_keychain_path
  if File.exists?(@last_keychain_id_file)
    self.last_keychain_id = File.read(@last_keychain_id_file).to_i
  else
    self.last_keychain_id = 0
    save
  end
end

#saveObject

Because this is a config based gateway, we only save last_keychain_id and nothing more.



535
536
537
# File 'lib/straight-server/gateway.rb', line 535

def save
  save_last_keychain_id!
end

#save_last_keychain_id!Object



552
553
554
555
# File 'lib/straight-server/gateway.rb', line 552

def save_last_keychain_id!
  @last_keychain_id_file ||= build_keychain_path
  File.open(@last_keychain_id_file, 'w') {|f| f.write(last_keychain_id) }
end

#validate_configObject

Raises:



528
529
530
531
# File 'lib/straight-server/gateway.rb', line 528

def validate_config
  raise NoPubkey if pubkey_missing?
  raise NoTestPubkey if test_pubkey_missing?
end