class NewlineHw::Api

Constants

DEFAULT_HOST

Attributes

host[R]

Public Class Methods

auth(data) click to toggle source
# File lib/newline_hw/api.rb, line 41
def self.auth(data)
  response = Excon.post("#{host}/api/auth", headers: {
    "Content-Type" => "application/json"
  }, body: data)
  raise NewlineHw::AuthenticationError, "Invalid email or password" if response.status != 200
  JSON.parse(response.body)
end
new() click to toggle source
# File lib/newline_hw/api.rb, line 12
def initialize
  @host = Api.host
  token = NewlineHw::Token.get_for_user
  @connection = Excon.new(@host, headers: {
    "Authorization" => "token #{token}"
  })
end

Private Class Methods

host() click to toggle source
# File lib/newline_hw/api.rb, line 51
def self.host
  ENV["NEWLINE_API_HOST"] || DEFAULT_HOST
end

Public Instance Methods

get(path) click to toggle source
# File lib/newline_hw/api.rb, line 36
def get(path)
  response = @connection.get(path: "/api/#{path}", expects: 200)
  JSON.parse(response.body)
end
post(path, data) click to toggle source
# File lib/newline_hw/api.rb, line 20
def post(path, data)
  response = @connection.post(path: "/api/#{path}", body: data.to_json, headers: {
    "Accept"       => "application/json",
    "Content-Type" => "application/json"
  }, expects: [200, 201])
  JSON.parse(response.body)
end
put(path, data) click to toggle source
# File lib/newline_hw/api.rb, line 28
def put(path, data)
  response = @connection.put(path: "/api/#{path}", body: data.to_json, headers: {
    "Accept"       => "application/json",
    "Content-Type" => "application/json"
  }, expects: [200, 201])
  JSON.parse(response.body)
end