Class: IOTA::Utils::Broker

Inherits:
Object
  • Object
show all
Defined in:
lib/iota/utils/broker.rb

Instance Method Summary collapse

Constructor Details

#initialize(provider, token, timeout = 120) ⇒ Broker

Returns a new instance of Broker.


8
9
10
11
# File 'lib/iota/utils/broker.rb', line 8

def initialize(provider, token, timeout = 120)
  @provider, @token = provider, token
  @timeout = timeout if timeout.to_i > 0
end

Instance Method Details

#sandboxSend(uri, &callback) ⇒ Object


35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/iota/utils/broker.rb', line 35

def sandboxSend(uri, &callback)
  processed = false
  success = false
  retValue = nil

  loop do
    uri, request, req_options = prepareRequest(nil, uri, Net::HTTP::Get)

    response = Net::HTTP.start(uri.hostname, uri.port, req_options) do |http|
      http.request(request)
    end

    begin
      data = JSON.parse(response.body)
      if data['status'] == 'FINISHED'
        processed = true
        success = true
        retValue = data['attachToTangleResponse']['trytes']
      elsif data['status'] == 'FAILED'
        processed = true
        success = false
        retValue = "Sandbox transaction processing failed. Please retry."
      end
    rescue JSON::ParserError
      processed = true
      success = false
      retValue = "Invalid response"
    end

    break if processed

    # Sleep for 15 seconds before making another request
    sleep 15
  end

  callback ? callback.call(success, retValue) : [success, retValue]
end

#send(command, &callback) ⇒ Object


13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/iota/utils/broker.rb', line 13

def send(command, &callback)
  uri, request, req_options = prepareRequest(command)
  response = Net::HTTP.start(uri.hostname, uri.port, req_options) do |http|
    http.request(request)
  end

  success = true
  begin
    data = JSON.parse(response.body)
    if data.key?('error')
      data = data['error']
      success = false
    else
      data = prepareResponse(data, command[:command])
    end
  rescue JSON::ParserError
    success = false
    data = "Invalid response"
  end
  callback ? callback.call(success, data) : [success, data]
end