Class: Bitstamper::Utilities

Inherits:
Object
  • Object
show all
Defined in:
lib/bitstamper/utilities.rb

Class Method Summary collapse

Class Method Details

.convert_value(value, type) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/bitstamper/utilities.rb', line 9

def convert_value(value, type)
  return case type
    when :string
      value.to_s
    when :integer
      value.to_i
    when :float
      value.to_f
    when :boolean
      value.to_s.downcase.eql?("true")
    when :datetime
      DateTime.parse(value)
    when :time
      epoch_to_time(value)
    when :hash
      value.symbolize_keys
    else
      value
  end
end

.epoch_to_time(epoch) ⇒ Object



30
31
32
# File 'lib/bitstamper/utilities.rb', line 30

def epoch_to_time(epoch)
  ::Time.at(epoch.to_i).utc
end

.fix_currency_pair(currency_pair) ⇒ Object



5
6
7
# File 'lib/bitstamper/utilities.rb', line 5

def fix_currency_pair(currency_pair)
  currency_pair&.to_s&.strip&.downcase
end

.numeric?(string) ⇒ Boolean

Returns:

  • (Boolean)


34
35
36
37
38
# File 'lib/bitstamper/utilities.rb', line 34

def numeric?(string)
  !!Kernel.Float(string) 
rescue TypeError, ArgumentError
  false
end

.parse_object!(object, klass) ⇒ Object



50
51
52
53
54
55
56
57
# File 'lib/bitstamper/utilities.rb', line 50

def parse_object!(object, klass)
  object = JSON.parse(object) if object.is_a? String
  if object["status"] == "error"
    raise ArgumentError.new(object["reason"])
  end

  klass.new(object)
end

.parse_objects!(string, klass) ⇒ Object



40
41
42
43
44
45
46
47
48
# File 'lib/bitstamper/utilities.rb', line 40

def parse_objects!(string, klass)
  # If Bitstamp returned nothing (which it does if the results yield empty) 'cast' it to an array
  string = "[]" if string == ""

  objects = JSON.parse(string)
  objects.collect do |t_json|
    parse_object!(t_json, klass)
  end
end