Class: Hive::Api
- Inherits:
-
Object
- Object
- Hive::Api
- Defined in:
- lib/hive/api.rb
Overview
This ruby API works with hived-0.23.00 and other AppBase compatible upstreams. To access different API namespaces, use the following:
api = Hive::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 = Hive::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 = Hive::MarketHistoryApi.new
api.get_volume
All known API by namespace:
-
AccountByKeyApi
-
AccountHistoryApi
-
DatabaseApi
-
FollowApi
-
MarketHistoryApi
-
NetworkBroadcastApi
-
TagsApi
-
WitnessApi
Also see: / Complete API Definitions
Constant Summary collapse
- DEFAULT_RPC_CLIENT_CLASS =
Use this for debugging naive thread handler. DEFAULT_RPC_CLIENT_CLASS = RPC::HttpClient
RPC::ThreadSafeHttpClient
Instance Attribute Summary collapse
-
#chain ⇒ Object
Returns the value of attribute chain.
-
#methods ⇒ Object
Returns the value of attribute methods.
-
#rpc_client ⇒ Object
Returns the value of attribute rpc_client.
Class Method Summary collapse
- .api_class_name ⇒ Object
- .api_name ⇒ Object
- .api_name=(api_name) ⇒ Object
-
.default_rpc_client_class ⇒ Object
Override this if you want to just use your own client.
- .jsonrpc(url = nil) ⇒ Object
- .jsonrpc=(jsonrpc, url = nil) ⇒ Object
-
.register(register) ⇒ Object
Used for dependency injection.
Instance Method Summary collapse
-
#initialize(options = {}) ⇒ Api
constructor
A new instance of Api.
- #inspect ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ Api
Returns a new instance of Api.
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/hive/api.rb', line 93 def initialize( = {}) @chain = [:chain] || :hive @error_pipe = [:error_pipe] || STDERR @api_name = self.class.api_name ||= :condenser_api @rpc_client = if !![:rpc_client] [:rpc_client] else rpc_client_class = self.class.default_rpc_client_class rpc_client_class.new(.merge(api_name: @api_name)) end 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() @methods = begin Api::jsonrpc(rpc_client.uri.to_s).get_api_methods rescue => e Fallback::API_METHODS end if Jsonrpc::UNLISTED_APIS.include? @api_name @methods ||= {} @methods[@api_name] ||= [] @methods[@api_name] += Fallback::API_METHODS[@api_name] @methods[@api_name] = @methods[@api_name].uniq end 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)
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 |
# File 'lib/hive/api.rb', line 175 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 = signature(rpc_method_name).args || [] expected_args_key_string = if expected_args.size > 0 " (#{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 if rpc_method_name == 'account_history_api.get_account_history' && expected_args_size == 3 && args_size == 6 # TODO Remove this condition if they ever fix this issue: # https://gitlab.syncad.com/hive/hive/-/issues/100 elsif rpc_method_name == 'account_history_api.get_ops_in_block' && expected_args_size == 2 && args_size == 3 # TODO Remove this condition if they ever fix this issue: # https://gitlab.syncad.com/hive/hive/-/issues/100 elsif rpc_method_name == 'account_history_api.enum_virtual_ops' && expected_args_size == 2 && args_size == 3 # TODO Remove this condition if they ever fix this issue: # https://gitlab.syncad.com/hive/hive/-/issues/100 else @error_pipe.puts "Warning #{rpc_method_name} expects arguments: #{expected_args_size}, got: #{args_size}" end end rescue NoMethodError => e error = Hive::ArgumentError.new("#{rpc_method_name} expects arguments: #{expected_args_size}", e) raise error rescue => e raise UnknownError.new("#{rpc_method_name} unknown error.", e) end args end response = rpc_client.rpc_execute(@api_name, m, rpc_args) 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
#chain ⇒ Object
Returns the value of attribute chain.
39 40 41 |
# File 'lib/hive/api.rb', line 39 def chain @chain end |
#methods ⇒ Object
Returns the value of attribute methods.
39 40 41 |
# File 'lib/hive/api.rb', line 39 def methods @methods end |
#rpc_client ⇒ Object
Returns the value of attribute rpc_client.
39 40 41 |
# File 'lib/hive/api.rb', line 39 def rpc_client @rpc_client end |
Class Method Details
.api_class_name ⇒ Object
56 57 58 |
# File 'lib/hive/api.rb', line 56 def self.api_class_name @api_name.to_s.split('_').map(&:capitalize).join end |
.api_name ⇒ Object
52 53 54 |
# File 'lib/hive/api.rb', line 52 def self.api_name @api_name end |
.api_name=(api_name) ⇒ Object
45 46 47 48 49 50 |
# File 'lib/hive/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 |
.default_rpc_client_class ⇒ Object
77 78 79 80 81 82 83 |
# File 'lib/hive/api.rb', line 77 def self.default_rpc_client_class if !!@injected_dependencies && !!@injected_dependencies[:default_rpc_client_class] @injected_dependencies[:default_rpc_client_class] else DEFAULT_RPC_CLIENT_CLASS end end |
.jsonrpc(url = nil) ⇒ Object
65 66 67 68 69 70 71 |
# File 'lib/hive/api.rb', line 65 def self.jsonrpc(url = nil) if @jsonrpc.size < 2 && url.nil? @jsonrpc.values.first else @jsonrpc[url] end end |
.jsonrpc=(jsonrpc, url = nil) ⇒ Object
60 61 62 63 |
# File 'lib/hive/api.rb', line 60 def self.jsonrpc=(jsonrpc, url = nil) @jsonrpc ||= {} @jsonrpc[url || jsonrpc.rpc_client.uri.to_s] = jsonrpc end |
.register(register) ⇒ Object
Used for dependency injection. Currently, the only key supported is:
‘default_rpc_client_class`
88 89 90 91 |
# File 'lib/hive/api.rb', line 88 def self.register(register) @injected_dependencies ||= {} @injected_dependencies = @injected_dependencies.merge register end |
Instance Method Details
#inspect ⇒ Object
136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/hive/api.rb', line 136 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 |