Class: Steem::Api

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

Overview

This ruby API works with steemd-0.19.4 and other Appbase compatible upstreams. To access different API namespaces, use the following:

api = Steem::Api.new
api.get_dynamic_global_properties

The above example will make an instance that can access the condenser_api namespace. Alternatively, you may also create a direct instances with its full name, if you prefer:

api = Steem::CondenserApi.new
api.get_dynamic_global_properties

If you know the name of another API that is supported by the remote node, you can create an instance to that instead, for example:

api = Steem::MarketHistoryApi.new
api.get_volume

All known API by namespace:

  • AccountByKeyApi

  • AccountHistoryApi

  • BlockApi

  • DatabaseApi

  • FollowApi

  • Jsonrpc

  • MarketHistoryApi

  • NetworkBroadcastApi

  • TagsApi

  • WitnessApi

Also see: Complete API Definitions

Direct Known Subclasses

BlockApi, Jsonrpc

Constant Summary collapse

DEFAULT_RPC_CLIENT =

Use this for debugging naive thread handler. DEFAULT_RPC_CLIENT = RPC::BaseClient

RPC::ThreadSafeClient

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Api

Returns a new instance of Api.



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/steem/api.rb', line 68

def initialize(options = {})
  @chain = options[:chain] || :steem
  @error_pipe = options[:error_pipe] || STDERR
  @api_name = self.class.api_name ||= :condenser_api
  @rpc_client = options[:rpc_client] || DEFAULT_RPC_CLIENT.new(options.merge(api_name: @api_name))
  
  if @api_name == :jsonrpc
    Api::jsonrpc = self
  else
    # Note, we have to wait until initialize to check this because we don't
    # have access to instance options until now.
    
    Api::jsonrpc = Jsonrpc.new(options)
    @methods = Api::jsonrpc.get_api_methods
    
    unless !!@methods[@api_name]
      raise UnknownApiError, "#{@api_name} (known APIs: #{@methods.keys.join(' ')})"
    end
    
    @methods = @methods[@api_name]
  end
    
  @try_count = 0
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/steem/api.rb', line 132

def method_missing(m, *args, &block)
  super unless respond_to_missing?(m)
  
  rpc_method_name = "#{@api_name}.#{m}"
  rpc_args = case @api_name
  when :condenser_api then args
  when :jsonrpc then args.first
  else
    expected_args = Api::signature(rpc_method_name).args || []
    expected_args_key_string = if expected_args.size > 0
      " (#{Api::args_keys_to_s(rpc_method_name)})"
    end
    expected_args_size = expected_args.size
    
    begin
      args = args.first.to_h
      args_size = args.size
      
      # Some argument are optional, but if the arguments passed are greater
      # than the expected arguments size, we can warn.
      if args_size > expected_args_size
        error_pipe.puts "Warning #{rpc_method_name} expects arguments: #{expected_args_size}, got: #{args_size}"
      end
    rescue NoMethodError => e
      error = Steem::ArgumentError.new("#{rpc_method_name} expects arguments: #{expected_args_size}", e)
      raise error
    rescue
      raise Steem::ArgumentError, "#{rpc_method_name} expects arguments: #{expected_args_size}"
    end
    
    args
  end
  
  response = @rpc_client.rpc_post(@api_name, m, rpc_args)
  
  if defined?(response.error) && !!response.error
    if !!response.error.message
      Api::raise_error_response rpc_method_name, rpc_args, response
    else
      raise Steem::ArgumentError, response.error.inspect
    end
  end
  
  if !!block
    case response
    when Hashie::Mash then yield response.result, response.error, response.id
    when Hashie::Array
      response.each do |r|
        r = Hashie::Mash.new(r)
        yield r.result, r.error, r.id
      end
    else; yield response
    end
  else
    return response
  end
end

Instance Attribute Details

#chainObject

Returns the value of attribute chain.



39
40
41
# File 'lib/steem/api.rb', line 39

def chain
  @chain
end

#methodsObject

Returns the value of attribute methods.



39
40
41
# File 'lib/steem/api.rb', line 39

def methods
  @methods
end

Class Method Details

.api_class_nameObject



56
57
58
# File 'lib/steem/api.rb', line 56

def self.api_class_name
  @api_name.to_s.split('_').map(&:capitalize).join
end

.api_nameObject



52
53
54
# File 'lib/steem/api.rb', line 52

def self.api_name
  @api_name
end

.api_name=(api_name) ⇒ Object



45
46
47
48
49
50
# File 'lib/steem/api.rb', line 45

def self.api_name=(api_name)
  @api_name = api_name.to_s.
    gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
    gsub(/([a-z\d])([A-Z])/,'\1_\2').
    tr('-', '_').downcase.to_sym
end

.jsonrpcObject



64
65
66
# File 'lib/steem/api.rb', line 64

def self.jsonrpc
  @jsonrpc
end

.jsonrpc=(jsonrpc) ⇒ Object



60
61
62
# File 'lib/steem/api.rb', line 60

def self.jsonrpc=(jsonrpc)
  @jsonrpc = jsonrpc
end

Instance Method Details

#inspectObject



93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/steem/api.rb', line 93

def inspect
  properties = %w(chain methods).map do |prop|
    if !!(v = instance_variable_get("@#{prop}"))
      case v
      when Array then "@#{prop}=<#{v.size} #{v.size == 1 ? 'element' : 'elements'}>" 
      else; "@#{prop}=#{v}" 
      end
    end
  end.compact.join(', ')
  
  "#<#{self.class.api_class_name} [#{properties}]>"
end