Class: Radiator::Api

Inherits:
Object
  • Object
show all
Defined in:
lib/radiator/api.rb

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Api

Returns a new instance of Api.



10
11
12
13
14
15
16
17
18
19
20
# File 'lib/radiator/api.rb', line 10

def initialize(options = {})
  @user = options[:user]
  @password = options[:password]
  @url = options[:url] || 'https://steemd.steemit.com'
  @debug = !!options[:debug]
  @net_http_persistent_enabled = true
  @logger = options[:logger] || Radiator.logger
  
  Hashie.logger = @logger
  @method_names = nil
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, *args, &block) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/radiator/api.rb', line 101

def method_missing(m, *args, &block)
  super unless respond_to_missing?(m)
  
  options = {
    jsonrpc: "2.0",
    params: [api_name, m, args],
    id: rpc_id,
    method: "call"
  }

  response = request(options)
  
  if !!response
    response = JSON[response.body]
    
    Hashie::Mash.new(response)
  end
end

Instance Method Details

#api_nameObject



30
31
32
# File 'lib/radiator/api.rb', line 30

def api_name
  :database_api
end

#find_account(id, &block) ⇒ Object



66
67
68
69
70
71
72
# File 'lib/radiator/api.rb', line 66

def (id, &block)
  if !!block
    yield get_accounts([id]).result.first
  else
    get_accounts([id]).result.first
  end
end

#find_block(block_number, &block) ⇒ Hash

Find a specific block

Parameters:

  • block_number (Fixnum)
  • block

    the block to execute for each result, optional.

Returns:

  • (Hash)


58
59
60
61
62
63
64
# File 'lib/radiator/api.rb', line 58

def find_block(block_number, &block)
  if !!block
    yield get_blocks(block_number).first
  else
    get_blocks(block_number).first
  end
end

#get_blocks(block_number, &block) ⇒ Array

Get a specific block or range of blocks.

Parameters:

  • block_number (Fixnum || Array<Fixnum>)
  • block

    the block to execute for each result, optional.

Returns:

  • (Array)


39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/radiator/api.rb', line 39

def get_blocks(block_number, &block)
  block_number = [*(block_number)].flatten
  
  if !!block
    block_number.each do |i|
      yield get_block(i).result, i
    end
  else
    block_number.map do |i|
      get_block(i).result
    end
  end
end

#method_namesObject



22
23
24
25
26
27
28
# File 'lib/radiator/api.rb', line 22

def method_names
  return @method_names if !!@method_names
  
  @method_names = Radiator::Api.methods(api_name).map do |e|
    e['method'].to_sym
  end
end

#respond_to_missing?(m, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


97
98
99
# File 'lib/radiator/api.rb', line 97

def respond_to_missing?(m, include_private = false)
  method_names.include?(m.to_sym)
end

#shutdownObject



120
121
122
123
# File 'lib/radiator/api.rb', line 120

def shutdown
  @http.shutdown if !!@http && defined?(@http.shutdown)
  @http = nil
end

#steem_per_mvestObject

TODO: Need to rename this to base_per_mvest and alias to steem_per_mvest



75
76
77
78
79
80
81
82
# File 'lib/radiator/api.rb', line 75

def steem_per_mvest
  properties = get_dynamic_global_properties.result
  
  total_vesting_fund_steem = properties.total_vesting_fund_steem.to_f
  total_vesting_shares_mvest = properties.total_vesting_shares.to_f / 1e6
  
  total_vesting_fund_steem / total_vesting_shares_mvest
end

#steem_per_usdObject

TODO: Need to rename this to base_per_debt and alias to steem_per_debt



85
86
87
88
89
90
91
92
93
94
95
# File 'lib/radiator/api.rb', line 85

def steem_per_usd
  feed_history = get_feed_history.result

  current_median_history = feed_history.current_median_history
  base = current_median_history.base
  base = base.split(' ').first.to_f
  quote = current_median_history.quote
  quote = quote.split(' ').first.to_f

  (base / quote) * steem_per_mvest
end