Class: Meeseeker::SteemEngine::Agent

Inherits:
Mechanize
  • Object
show all
Defined in:
lib/meeseeker/steem_engine/agent.rb

Direct Known Subclasses

HiveEngine::Agent

Constant Summary collapse

POST_HEADERS =
{
  'Content-Type' => 'application/json; charset=utf-8',
  'User-Agent' => Meeseeker::AGENT_ID
}

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Agent

Returns a new instance of Agent.



10
11
12
13
14
15
16
17
18
# File 'lib/meeseeker/steem_engine/agent.rb', line 10

def initialize(options = {})
  super
  
  self.user_agent = Meeseeker::AGENT_ID
  self.max_history = 0
  self.default_encoding = 'UTF-8'
  
  @node_url = options[:url] || Meeseeker::steem_engine_node_url
end

Instance Method Details

#block(block_num) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/meeseeker/steem_engine/agent.rb', line 47

def block(block_num)
  5.times do
    request_body = {
      jsonrpc: "2.0",
      method: :getBlockInfo,
      params: {
        blockNumber: block_num.to_i
      },
      id: rpc_id
    }.to_json
    
    response = request_with_entity :post, blockchain_uri, request_body, POST_HEADERS
    block = JSON[response.body]["result"]
    
    return block if !!block
    
    sleep 3
  end
  
  return nil
end

#blockchain_http_postObject



24
25
26
# File 'lib/meeseeker/steem_engine/agent.rb', line 24

def blockchain_http_post
  @http_post ||= Net::HTTP::Post.new(blockchain_uri.request_uri, POST_HEADERS)
end

#blockchain_uriObject



20
21
22
# File 'lib/meeseeker/steem_engine/agent.rb', line 20

def blockchain_uri
  @blockchain_uri ||= URI.parse(@node_url + '/blockchain')
end

#latest_block_infoObject



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/meeseeker/steem_engine/agent.rb', line 28

def latest_block_info
  5.times do
    request_body = {
      jsonrpc: "2.0",
      method: :getLatestBlockInfo,
      id: rpc_id
    }.to_json
    
    response = request_with_entity :post, blockchain_uri, request_body, POST_HEADERS
    latest_block_info = JSON[response.body]["result"]
    
    return latest_block_info if !!latest_block_info
    
    sleep 3
  end
  
  return nil
end