Module: Vermillion::Helper::ApiCommunication

Included in:
Controller::Change, Controller::Create, Controller::Update
Defined in:
lib/client/helper/apicommunication.rb

Instance Method Summary collapse

Instance Method Details

#send_to_all(endpoint, args = {}) ⇒ Object

Send HTTP requests to all servers in the local manifest Params:

endpoint

The REST endpoint you’d like to send a request to

args

Any specific configuration data to send along with the request



41
42
43
44
45
# File 'lib/client/helper/apicommunication.rb', line 41

def send_to_all(endpoint, args = {})
  @config.get(:servers).each do |server|
    send_to_one(server[:name], endpoint, args) if server[:name]
  end
end

#send_to_one(input, endpoint, args = {}) ⇒ Object

Send an HTTP request to one server Params:

input

The server you want to connect to

endpoint

The REST endpoint you’d like to send a request to

args

Any specific configuration data to send along with the request



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/client/helper/apicommunication.rb', line 9

def send_to_one(input, endpoint, args = {})
  server, endpoint = setup(input, endpoint, args)

  begin
    resp = @network.post(endpoint.to_s, server[:key])

    Notify.spit(endpoint.to_s) if DEBUG
    Notify.spit(resp.body) if DEBUG

    # generic failure for invalid response type
    raise Errno::ECONNREFUSED if resp["Content-Type"] != "application/json"

    # handle JSON response
    response_data = @format.symbolize(JSON.parse(resp.body))

    if response_data[:_code] == 200
      Notify.success("#{server[:name]} (#{server[:address]}) update succeeded")
    elsif !response_data[:message].nil?
      # work around a messaging issue with vermillion-server
      Notify.warning("Error: #{response_data[:message]}")
    else
      Notify.warning("#{response_data[:_title]}: #{response_data[:_message]}")
    end
  rescue Errno::ECONNREFUSED
    Notify.warning("Request failed for #{server[:name]} (#{server[:address]})")
  end
end