Module: JytPay::Utils

Defined in:
lib/jyt_pay/utils.rb

Class Method Summary collapse

Class Method Details

.gen_flow_id(time = Time.now) ⇒ String

通过时间,返回唯一一个24位flow id(支持分布) 同一秒,同一台机器,同一个进程,最多可以产生 16777214 个不一样的订单号

Examples:

JytPay::Utils.gen_flow_id

Parameters:

  • 时间(默认是 (Integer)

    now)

Returns:

  • (String)

    flow id



30
31
32
33
34
35
36
37
38
39
# File 'lib/jyt_pay/utils.rb', line 30

def self.gen_flow_id(time=Time.now)
  machine_id = Digest::MD5.digest(::Mac.addr).unpack("N")[0]
  process_id = Process.pid % 0xFFFF

  @counter ||= 0
  @counter += 1
  count = (@counter) % 0xFFFFFF

  return [ time.to_i, machine_id, process_id, count << 8 ].pack("N NX lXX NX").unpack("H*")[0].force_encoding('UTF-8')
end

.symbolize_keys(hash) ⇒ Hash

把 hash 中的 key,都转化为 symbol 类型

Parameters:

  • hash (Hash)

    需要更改的 hash

Returns:

  • (Hash)

    更改后的 hash



12
13
14
15
16
17
18
# File 'lib/jyt_pay/utils.rb', line 12

def self.symbolize_keys(hash)
  new_hash = {}
  hash.each do |key, value|
    new_hash[(key.to_sym rescue key) || key] = value
  end
  new_hash
end