Class: Lisk::Client
- Inherits:
-
Object
- Object
- Lisk::Client
- Defined in:
- lib/lisk/client.rb
Overview
A simple HTTP client connecting to a Lisk Core API node.
Instance Attribute Summary collapse
-
#active ⇒ Object
Host and port of the API endpoint.
-
#host ⇒ Object
Host and port of the API endpoint.
-
#port ⇒ Object
Host and port of the API endpoint.
-
#ssl ⇒ Object
Host and port of the API endpoint.
Instance Method Summary collapse
-
#configure(host, port) ⇒ Object
Allows reconfiguring of the Lisk HTTP client’s host and port.
-
#initialize(host = "127.0.0.1", port = 7000) ⇒ Client
constructor
Initializes the Lisk HTTP client and defaults to localhost port 7000.
-
#is_alive? ⇒ Boolean
Get the status of last received block.
-
#method_missing(name, *args, &block) ⇒ Object
Handles unimplemented methods.
-
#query_get(endpoint, params = nil) ⇒ Object
Handles GET requests to the given Lisk Core API endpoint.
-
#query_post(endpoint, params) ⇒ Object
Handles POST requests to the given Lisk Core API endpoint.
-
#query_put(endpoint, params) ⇒ Object
Handles PUT requests to the given Lisk Core API endpoint.
Constructor Details
#initialize(host = "127.0.0.1", port = 7000) ⇒ Client
Initializes the Lisk HTTP client and defaults to localhost port 7000.
15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/lisk/client.rb', line 15 def initialize host = "127.0.0.1", port = 7000 @host = host @port = port @ssl = false if self.is_alive? @active = true return self else @active = false 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
135 136 137 |
# File 'lib/lisk/client.rb', line 135 def method_missing name, *args, &block todo "#{self}::#{name} METHOD MISSING" end |
Instance Attribute Details
#active ⇒ Object
Host and port of the API endpoint.
12 13 14 |
# File 'lib/lisk/client.rb', line 12 def active @active end |
#host ⇒ Object
Host and port of the API endpoint.
12 13 14 |
# File 'lib/lisk/client.rb', line 12 def host @host end |
#port ⇒ Object
Host and port of the API endpoint.
12 13 14 |
# File 'lib/lisk/client.rb', line 12 def port @port end |
#ssl ⇒ Object
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.
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/lisk/client.rb', line 29 def configure host, port if not host.empty? or not port.empty? @host = host @port = port @ssl = false if self.is_alive? @active = true return self else @active = false return nil end else @active = false 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.
49 50 51 52 |
# File 'lib/lisk/client.rb', line 49 def is_alive? connected = self.query_get "loader/status/ping" @active = connected["success"] end |
#query_get(endpoint, params = nil) ⇒ Object
Handles GET requests to the given Lisk Core API endpoint
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/lisk/client.rb', line 55 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 @active = true result = JSON::parse response.body rescue Timeout::Error => e @active = false p "Can't connect to the Lisk node: Timeout!" rescue Errno::EHOSTUNREACH => e @active = false p "Can't connect to the Lisk node: Host Unreachable!" rescue Errno::ECONNREFUSED => e @active = false 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
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/lisk/client.rb', line 82 def query_post endpoint, params if not @ssl # fixme "#{self}::#{__method__} Allow HTTPS requests" begin node = ::Net::HTTP.new @host, @port header = {'Content-Type': 'application/json'} uri = URI.parse "http://#{host}:#{port}/api/#{endpoint}" request = ::Net::HTTP::Post.new uri, header request.body = params.to_json response = node.request request @active = true result = JSON::parse response.body rescue Timeout::Error => e @active = false p "Can't connect to the Lisk node: Timeout!" rescue Errno::EHOSTUNREACH => e @active = false p "Can't connect to the Lisk node: Host Unreachable!" rescue Errno::ECONNREFUSED => e @active = false 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
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 |
# File 'lib/lisk/client.rb', line 108 def query_put endpoint, params if not @ssl # fixme "#{self}::#{__method__} Allow HTTPS requests" begin node = ::Net::HTTP.new @host, @port header = {'Content-Type': 'application/json'} uri = URI.parse "http://#{host}:#{port}/api/#{endpoint}" uri.query = URI.encode_www_form params request = ::Net::HTTP::Put.new uri, header request.body = params.to_json response = node.request request @active = true result = JSON::parse response.body rescue Timeout::Error => e @active = false p "Can't connect to the Lisk node: Timeout!" rescue Errno::EHOSTUNREACH => e @active = false p "Can't connect to the Lisk node: Host Unreachable!" rescue Errno::ECONNREFUSED => e @active = false p "Can't connect to the Lisk node: Connection Refused!" end end end |