Class: Eywa::Client

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

Instance Method Summary collapse

Constructor Details

#initializeClient

Returns a new instance of Client.



16
17
18
19
20
# File 'lib/eywa.rb', line 16

def initialize
  @rpc_callbacks = {}
  @handlers = {}
  @mutex = Mutex.new
end

Instance Method Details

#close_task(status = SUCCESS) ⇒ Object



129
130
131
132
133
134
135
136
137
138
# File 'lib/eywa.rb', line 129

def close_task(status = SUCCESS)
  send_notification(
    'method' => 'task.close',
    'params' => {
      'status' => status
    }
  )

  exit(status == SUCCESS ? 0 : 1)
end

#debug(message, data = nil) ⇒ Object



106
107
108
# File 'lib/eywa.rb', line 106

def debug(message, data = nil)
  log(event: 'DEBUG', message: message, data: data)
end

#error(message, data = nil) ⇒ Object



98
99
100
# File 'lib/eywa.rb', line 98

def error(message, data = nil)
  log(event: 'ERROR', message: message, data: data)
end

#exception(message, data = nil) ⇒ Object



114
115
116
# File 'lib/eywa.rb', line 114

def exception(message, data = nil)
  log(event: 'EXCEPTION', message: message, data: data)
end

#get_taskObject



149
150
151
# File 'lib/eywa.rb', line 149

def get_task
  send_request('method' => 'task.get')
end

#graphql(query, variables = nil) ⇒ Object



158
159
160
161
162
163
164
165
166
# File 'lib/eywa.rb', line 158

def graphql(query, variables = nil)
  send_request(
    'method' => 'eywa.datasets.graphql',
    'params' => {
      'query' => query,
      'variables' => variables
    }
  )
end

#info(message, data = nil) ⇒ Object



94
95
96
# File 'lib/eywa.rb', line 94

def info(message, data = nil)
  log(event: 'INFO', message: message, data: data)
end

#log(event: 'INFO', message:, data: nil, duration: nil, coordinates: nil, time: Time.now) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/eywa.rb', line 80

def log(event: 'INFO', message:, data: nil, duration: nil, coordinates: nil, time: Time.now)
  send_notification(
    'method' => 'task.log',
    'params' => {
      'time' => time.iso8601,
      'event' => event,
      'message' => message,
      'data' => data,
      'coordinates' => coordinates,
      'duration' => duration
    }
  )
end

#open_pipeObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/eywa.rb', line 22

def open_pipe
  Thread.new do
    while (line = STDIN.gets)
      begin
        json = JSON.parse(line)
        handle_data(json)
      rescue JSON::ParserError => e
        STDERR.puts("Failed to parse JSON: #{e.message}")
      rescue => e
        STDERR.puts("Error handling data: #{e.message}")
      end
    end
  end
end

#register_handler(method, &handler) ⇒ Object



74
75
76
77
78
# File 'lib/eywa.rb', line 74

def register_handler(method, &handler)
  @mutex.synchronize do
    @handlers[method] = handler
  end
end

#report(message, data = nil, image = nil) ⇒ Object



118
119
120
121
122
123
124
125
126
127
# File 'lib/eywa.rb', line 118

def report(message, data = nil, image = nil)
  send_notification(
    'method' => 'task.report',
    'params' => {
      'message' => message,
      'data' => data,
      'image' => image
    }
  )
end

#return_taskObject



153
154
155
156
# File 'lib/eywa.rb', line 153

def return_task
  send_notification('method' => 'task.return')
  exit(0)
end

#send_notification(data) ⇒ Object



68
69
70
71
72
# File 'lib/eywa.rb', line 68

def send_notification(data)
  data['jsonrpc'] = '2.0'
  STDOUT.puts(data.to_json)
  STDOUT.flush
end

#send_request(data) ⇒ Object



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
# File 'lib/eywa.rb', line 37

def send_request(data)
  id = SecureRandom.uuid
  data['jsonrpc'] = '2.0'
  data['id'] = id

  # Create a queue for this request
  queue = Queue.new
  
  @mutex.synchronize do
    @rpc_callbacks[id] = queue
  end

  # Send the request
  STDOUT.puts(data.to_json)
  STDOUT.flush

  # Wait for response in a thread
  Thread.new do
    response = queue.pop
    @mutex.synchronize do
      @rpc_callbacks.delete(id)
    end
    
    if response['error']
      raise StandardError.new(response['error']['message'] || response['error'].to_s)
    else
      response['result']
    end
  end
end

#trace(message, data = nil) ⇒ Object



110
111
112
# File 'lib/eywa.rb', line 110

def trace(message, data = nil)
  log(event: 'TRACE', message: message, data: data)
end

#update_task(status = PROCESSING) ⇒ Object



140
141
142
143
144
145
146
147
# File 'lib/eywa.rb', line 140

def update_task(status = PROCESSING)
  send_notification(
    'method' => 'task.update',
    'params' => {
      'status' => status
    }
  )
end

#warn(message, data = nil) ⇒ Object



102
103
104
# File 'lib/eywa.rb', line 102

def warn(message, data = nil)
  log(event: 'WARN', message: message, data: data)
end