Class: ESHQ::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/eshq/client.rb

Instance Method Summary collapse

Constructor Details

#initializeClient

Returns a new instance of Client.



8
9
10
# File 'lib/eshq/client.rb', line 8

def initialize
  ESHQ.configure
end

Instance Method Details

#configObject



12
13
14
# File 'lib/eshq/client.rb', line 12

def config
  ESHQ.configuration
end

#credentialsObject



38
39
40
41
# File 'lib/eshq/client.rb', line 38

def credentials
  time = Time.now.to_i.to_s
  {:key => config.key, :timestamp => time, :token => token(time)}
end

#post(path, params) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/eshq/client.rb', line 20

def post(path, params)
  url = url_for(path)

  request = Net::HTTP::Post.new(url.path)
  request.set_form_data(params.merge(credentials))

  http = Net::HTTP.new(url.host, url.port)
  http.use_ssl = true if url.is_a?(URI::HTTPS)
  http.verify_mode = OpenSSL::SSL::VERIFY_NONE

  response = http.request(request)
  if response.code == "200"
    response.content_type == "application/json" ? MultiJson.load(response.body) : true
  else
    raise "Error #{response.body}"
  end
end

#token(time) ⇒ Object



43
44
45
# File 'lib/eshq/client.rb', line 43

def token(time)
  Digest::SHA1.hexdigest([config.key, config.secret, time].join(":"))
end

#urlObject



16
17
18
# File 'lib/eshq/client.rb', line 16

def url
  config.url
end

#url_for(path) ⇒ Object



47
48
49
50
51
# File 'lib/eshq/client.rb', line 47

def url_for(path)
  URI.parse(url).tap do |url|
    url.path = path
  end
end