Class: Lisk::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/lisk/client.rb

Overview

A simple HTTP client connecting to a Lisk Core API node.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host = "127.0.0.1", port = 8000) ⇒ Client

Initializes the Lisk HTTP client and defaults to localhost port 8000.



15
16
17
18
19
20
21
22
23
24
# File 'lib/lisk/client.rb', line 15

def initialize host = "127.0.0.1", port = 8000
  @host = host
  @port = port
  @ssl = false
  if self.is_alive?
    return self
  else
    return nil
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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

Handles unimplemented methods



115
116
117
# File 'lib/lisk/client.rb', line 115

def method_missing(name, *args, &block)
  todo "#{self}::#{name} METHOD MISSING"
end

Instance Attribute Details

#hostObject

Host and port of the API endpoint.



12
13
14
# File 'lib/lisk/client.rb', line 12

def host
  @host
end

#portObject

Host and port of the API endpoint.



12
13
14
# File 'lib/lisk/client.rb', line 12

def port
  @port
end

#sslObject

Host and port of the API endpoint.



12
13
14
# File 'lib/lisk/client.rb', line 12

def ssl
  @ssl
end

Instance Method Details

#configure(host, port) ⇒ Object

Allows reconfiguring of the Lisk HTTP client’s host and port.



27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/lisk/client.rb', line 27

def configure host, port
  if not host.empty? or not port.empty?
    @host = host
    @port = port
    @ssl = false
    if self.is_alive?
      return self
    else
      return nil
    end
  else
    return nil
  end
end

#is_alive?Boolean

Get the status of last received block. Returns true if block was received in the past 120 seconds.

Returns:

  • (Boolean)


44
45
46
47
# File 'lib/lisk/client.rb', line 44

def is_alive?
  connected = self.query_get "loader/status/ping"
  connected["success"]
end

#query_get(endpoint, params = nil) ⇒ Object

Handles GET requests to the given Lisk Core API endpoint



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/lisk/client.rb', line 50

def query_get endpoint, params = nil
  if not @ssl
    # fixme "#{self}::#{__method__} Allow HTTPS requests"
    begin
      node = ::Net::HTTP.new @host, @port
      uri = URI.parse "http://#{host}:#{port}/api/#{endpoint}"
      if not params.nil?
        uri.query = URI.encode_www_form params
      end
      request = ::Net::HTTP::Get.new uri
      response = node.request request
      result = JSON::parse response.body
    rescue Timeout::Error => e
      p "Can't connect to the Lisk node: Timeout!"
    rescue Errno::EHOSTUNREACH => e
      p "Can't connect to the Lisk node: Host Unreachable!"
    rescue Errno::ECONNREFUSED => e
      p "Can't connect to the Lisk node: Connection Refused!"
    end
  end
end

#query_post(endpoint, params) ⇒ Object

Handles POST requests to the given Lisk Core API endpoint



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/lisk/client.rb', line 73

def query_post endpoint, params
  if not @ssl
    # fixme "#{self}::#{__method__} Allow HTTPS requests"
    begin
      node = ::Net::HTTP.new @host, @port
      uri = URI.parse "http://#{host}:#{port}/api/#{endpoint}"
      uri.query = URI.encode_www_form params
      request = ::Net::HTTP::POST.new uri
      response = node.request request
      result = JSON::parse response.body
    rescue Timeout::Error => e
      p "Can't connect to the Lisk node: Timeout!"
    rescue Errno::EHOSTUNREACH => e
      p "Can't connect to the Lisk node: Host Unreachable!"
    rescue Errno::ECONNREFUSED => e
      p "Can't connect to the Lisk node: Connection Refused!"
    end
  end
end

#query_put(endpoint, params) ⇒ Object

Handles PUT requests to the given Lisk Core API endpoint



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/lisk/client.rb', line 94

def query_put endpoint, params
  if not @ssl
    # fixme "#{self}::#{__method__} Allow HTTPS requests"
    begin
      node = ::Net::HTTP.new @host, @port
      uri = URI.parse "http://#{host}:#{port}/api/#{endpoint}"
      uri.query = URI.encode_www_form params
      request = ::Net::HTTP::Put.new uri
      response = node.request request
      result = JSON::parse response.body
    rescue Timeout::Error => e
      p "Can't connect to the Lisk node: Timeout!"
    rescue Errno::EHOSTUNREACH => e
      p "Can't connect to the Lisk node: Host Unreachable!"
    rescue Errno::ECONNREFUSED => e
      p "Can't connect to the Lisk node: Connection Refused!"
    end
  end
end