class EepClient

Public Class Methods

new(api_token, options = { }) click to toggle source
# File lib/eep_client.rb, line 32
def initialize(api_token, options = { })
  @api_token = api_token
  @debug = options[:debug]
  @base_url = options[:base_url] || BASE_URL
  
  unless options[:no_send]
    uri = URI(@base_url)      
    @http = Net::HTTP.new(uri.host, uri.port)
    @http.use_ssl = true
    @http.verify_mode = OpenSSL::SSL::VERIFY_NONE if options[:no_verify]
    @http.set_debug_output($stdout) if @debug
  end
end

Public Instance Methods

send_clear(clear) click to toggle source
# File lib/eep_client.rb, line 65
def send_clear(clear)
  json = format_clear(clear)
  if @debug
    puts "sending clear json:"
    puts json
  end
  if @http
    headers = { ACC_HDR => APP_JSON, CON_HDR => APP_JSON }
    res = @http.send_request(POST, CLEAR_RESOURCE, json, headers)
    if res.is_a? Net::HTTPServerError
      res.error!
    else      
      Response.new_instance(JSON.parse(res.body))
    end
  else
    Response.mock_clear_ok      
  end
end
send_event(event) click to toggle source
# File lib/eep_client.rb, line 46
def send_event(event)
  json = format_event(event)
  if @debug
    puts "sending event json:"
    puts json
  end
  if @http
    headers = { ACC_HDR => APP_JSON, CON_HDR => APP_JSON }      
    res = @http.send_request(POST, EVENT_RESOURCE, json, headers)
    if res.is_a? Net::HTTPServerError
      res.error!
    else
      Response.new_instance(JSON.parse(res.body))
    end
  else
    Response.mock_event_ok
  end
end

Private Instance Methods

format_clear(data) click to toggle source
# File lib/eep_client.rb, line 115
def format_clear(data)
  h = { :local_instance_id => data[:local_instance_id], :source_location => data[:source_location] }
  format_data(:clear, h)
end
format_data(type, data) click to toggle source
# File lib/eep_client.rb, line 86
def format_data(type, data)
  packet = {
    api_token: @api_token,
    type => data
  }
  
  if @debug
    JSON.pretty_generate(packet)
  else
    JSON.generate(packet)
  end    
end
format_event(data) click to toggle source
# File lib/eep_client.rb, line 99
def format_event(data)
  # event time attrs can take strings (iso8601 format), ints (unix epoch) or Time objects
  [:creation_time, :last_time].each do |name|
    val = data[name]
    if val
      case
      when val.is_a?(Fixnum), val.is_a?(Bignum)
        data[name] = Time.at(val).iso8601
      when val.is_a?(Time)
        data[name] = val.iso8601
      end        
    end
  end
  format_data(:event, data)
end