class ElasticDot::API

Attributes

email[R]
host[R]

Public Class Methods

new(opts = {}) click to toggle source
# File lib/elasticdot/api.rb, line 7
def initialize(opts = {})
  @host  = opts[:host] || 'https://api.elasticdot.com'

  if opts[:email] and opts[:password]
    @email, @pass = opts[:email], opts[:password]
  else
    @email, @pass = ElasticDot::Command::Auth.credentials
  end
end

Public Instance Methods

login() click to toggle source
# File lib/elasticdot/api.rb, line 17
def login
  return unless (@email && @pass)

  @pass = RestClient.post(
    "#{@host}/auth",
    email: @email, password: @pass
  )
rescue => e
  puts e.response
  exit 1
end
method_missing(m, *args, &block) click to toggle source
# File lib/elasticdot/api.rb, line 29
def method_missing(m, *args, &block)
  unless ['get', 'post', 'put', 'delete'].include? m.to_s
    raise NoMethodError, "undefined method: #{m}"
  end

  res = self.send('req', m, args)
  JSON.parse res rescue ""
end

Private Instance Methods

req(method, *args) click to toggle source
# File lib/elasticdot/api.rb, line 39
def req(method, *args)
  raise if args.empty?
  args = args.shift
  path = args.shift

  resource = RestClient::Resource.new(
    "#{@host}#{path}", user: @email, password: @pass
  )

  begin
    resource.send method, (args.first || {})
  rescue => e
    if e.respond_to? :response
      puts e.response
    else
      puts 'Something went wrong, we have been notified.'
    end

    exit 1
  end
end