Class: API

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/peas/api.rb

Constant Summary collapse

LONG_POLL_TIMEOUT =
10 * 60
LONG_POLL_INTERVAL =
0.5

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeAPI

This allows base_uri to be dynamically set. Useful for testing



11
12
13
# File 'lib/peas/api.rb', line 11

def initialize
  self.class.base_uri Peas.api_domain
end

Class Method Details

.duplex_socket(socket) ⇒ Object

Create 2 threads to allow raw TTY to be sent at the same time as outputting data from the socket.



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/peas/api.rb', line 89

def self.duplex_socket socket
  threads = []

  # Copy STDIN to socket
  threads << Thread.start do
    STDIN.raw do |stdin|
      IO.copy_stream stdin, socket
    end
    socket.close_write
  end

  # Write response to STDOUT
  threads << Thread.start do
    begin
      while (chunk = socket.readpartial(512))
        print chunk
      end
    rescue EOFError
    end
    threads.first.kill
  end

  threads.each(&:join)
end

.stream_job(job) ⇒ Object

Stream the output of a Switchboard job



58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/peas/api.rb', line 58

def self.stream_job(job)
  API.stream_output "subscribe.job_progress.#{job}" do |line|
    if line.key? 'status'
      if line['status'] == 'failed'
        raise line['body']
      elsif line['status'] == 'complete'
        break
      end
    end
    puts line['body'] if line['body']
  end
end

.stream_output(switchboard_command) ⇒ Object

Stream data from the Switchboard server



72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/peas/api.rb', line 72

def self.stream_output(switchboard_command)
  socket = API.switchboard_connection
  socket.puts switchboard_command
  begin
    while (line = socket.gets)
      if block_given?
        yield JSON.parse line
      else
        puts line
      end
    end
  rescue Interrupt, Errno::ECONNRESET
  end
end

.switchboard_connectionObject



53
54
55
# File 'lib/peas/api.rb', line 53

def self.switchboard_connection
  TCPSocket.new Peas.host, Peas::SWITCHBOARD_PORT
end

Instance Method Details

#check_versions(json) ⇒ Object

Check CLI client is up to date.



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/peas/api.rb', line 37

def check_versions(json)
  # Only check major and minor versions
  version_mismatch = false
  api_version = json['version'].split('.')
  client_version = Peas::VERSION.split('.')
  if api_version[0] != client_version[0]
    version_mismatch = true
  else
    version_mismatch = true if api_version[1] != client_version[1]
  end
  return unless version_mismatch
  Peas.warning_message "Your version of the CLI client is out of date " \
    "(Client: #{Peas::VERSION}, API: #{json['version']}). " \
    "Please update using `gem install peas-cli`."
end

#request(verb, method, params = nil) ⇒ Object

Generic wrapper to the Peas API



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/peas/api.rb', line 16

def request(verb, method, params = nil)
  response = self.class.send(verb, "#{method}", query: params).body
  json = response ? JSON.parse(response) : {}
  # If there was an HTTP-level error
  raise json['error'].color(:red) if json.key? 'error'
  # Successful responses
  if json.key? 'job'
    # Long-running jobs need to stream from the Switchboard server
    API.stream_job json['job']
  else
    check_versions(json)
    if block_given?
      yield json['message']
    else
      puts json['message']
    end
    json
  end
end