class Evostream::Client

Public Class Methods

new(options = {}) click to toggle source
# File lib/evostream/client.rb, line 11
def initialize(options = {})
  # Merge the config values from the module and those passed
  # to the client.
  merged_options = Evostream.options.merge(options)

  # Copy the merged values to this client and ignore those
  # not part of our configuration
  Configuration::VALID_CONFIG_KEYS.each do |key|
    send("#{key}=", merged_options[key])
  end
end

Private Instance Methods

api_call(method, params) click to toggle source
# File lib/evostream/client.rb, line 41
def api_call(method, params)
  uri = URI.parse(service_url(method, params))
  http = Net::HTTP.new(uri.host, uri.port)
  http.read_timeout = @timeout
  http.open_timeout = @timeout
  http.use_ssl = true if @protocol == "https"
  request = Net::HTTP::Get.new(uri.request_uri)
  request.basic_auth(@username, @password)
  http.request(request)
end
base_url() click to toggle source
# File lib/evostream/client.rb, line 61
def base_url
  "#{@protocol}://#{@host}:#{@port}#{@path_prefix}"
end
encode_params(params) click to toggle source
# File lib/evostream/client.rb, line 65
def encode_params(params)
  Base64.urlsafe_encode64(params.map {|k, v| "#{k}=#{v}" }.join(' ')).chomp
end
method_missing(method, *args) click to toggle source
Calls superclass method
# File lib/evostream/client.rb, line 24
def method_missing(method, *args)
  params = !args.empty? ? args.first : {}
  response = api_call(method, params)
  if response.code.to_i == 200
    json = parse(response.body)
    if json['status'] == 'SUCCESS'
      json['data']
    elsif json['status'] == 'FAIL' && json['description'] =~ /command .* not known/
      super
    else
      raise json['description']
    end
  else
    raise response.body
  end
end
parse(text) click to toggle source
# File lib/evostream/client.rb, line 69
def parse(text)
  JSON.parse(text)
rescue JSON::ParserError => ex
  raise "Invalid response: #{ex.message}"
end
service_url(service, params) click to toggle source
# File lib/evostream/client.rb, line 52
def service_url(service, params)
  url = "#{base_url}/#{service}"
  unless params.empty?
    url + "?params=#{encode_params(params)}"
  else
    url
  end
end