Class: Reggora::ApiClient

Inherits:
Object
  • Object
show all
Defined in:
lib/reggora/Adapters/api_client.rb

Class Method Summary collapse

Class Method Details

.authenticate(username, password, type) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/reggora/Adapters/api_client.rb', line 8

def self.authenticate(username, password, type)
  body = {:username => username, :password => password}

  response = Net::HTTP.post URI("#{$base_api_uri}#{type}/auth"), body.to_json
  case response
  when Net::HTTPSuccess then
    JSON.parse(response.read_body)
  when Net::HTTPBadRequest then
    res = JSON.parse(response.read_body)
    raise res.inspect if res["error"].nil?
    print res
  when Net::HTTPUnauthorized then
    raise "Unauthorized."
  when Net::HTTPInternalServerError then
    raise "Internal server error"
  else
    raise "Unknown error #{response}: #{response.inspect}"
  end
end