Class: Steem::BlockApi

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

Overview

BlockApi is used to query values related to the block plugin. It can also be used to access a range of multiple blocks by using JSON-RPC 2.0 batch requests.

Also see: Block API Definitions

Constant Summary collapse

MAX_RANGE_SIZE =
50

Constants inherited from Api

Api::DEFAULT_RPC_CLIENT_CLASS

Instance Attribute Summary

Attributes inherited from Api

#chain, #methods

Instance Method Summary collapse

Methods inherited from Api

api_class_name, api_name, api_name=, args_keys_to_s, default_rpc_client_class, #inspect, jsonrpc, jsonrpc=, raise_error_response, signature

Constructor Details

#initialize(options = {}) ⇒ BlockApi

Returns a new instance of BlockApi.



10
11
12
13
# File 'lib/steem/block_api.rb', line 10

def initialize(options = {})
  self.class.api_name = :block_api
  super
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Steem::Api

Instance Method Details

#get_blocks(options = {block_range: (0..0)}, &block) ⇒ Object

Uses a batched requst on a range of blocks.

Parameters:

  • options (Hash) (defaults to: {block_range: (0..0)})

    The attributes to get a block range with.

Options Hash (options):

  • :block_range (Range)

    starting on one block number and ending on an higher block number.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/steem/block_api.rb', line 19

def get_blocks(options = {block_range: (0..0)}, &block)
  block_range = options[:block_range] || (0..0)
  
  if (start = block_range.first) < 1
    raise Steem::ArgumentError, "Invalid starting block: #{start}"
  end
  
  chunks = if block_range.size > MAX_RANGE_SIZE
    block_range.each_slice(MAX_RANGE_SIZE)
  else
    [block_range]
  end
  
  for sub_range in chunks do
    request_object = []
    
    for i in sub_range do
      @rpc_client.put(self.class.api_name, :get_block, block_num: i, request_object: request_object)
    end
    
    if !!block
      index = 0
      @rpc_client.rpc_batch_execute(request_object: request_object) do |result, error, id|
        block_num = sub_range.to_a[index]
        index = index += 1
        yield(result.nil? ? nil : result.block, block_num)
      end
    else
      blocks = []
      
      @rpc_client.rpc_batch_execute(request_object: request_object) do |result, error, id|
        blocks << result
      end
    end
  end
  
  blocks
end