Class: NewlineHw::Api

Inherits:
Object
  • Object
show all
Defined in:
lib/newline_hw/api.rb

Constant Summary collapse

DEFAULT_HOST =
"https://newline.theironyard.com"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeApi

Returns a new instance of Api.



12
13
14
15
16
17
18
# 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

Instance Attribute Details

#hostObject (readonly)

Returns the value of attribute host.



10
11
12
# File 'lib/newline_hw/api.rb', line 10

def host
  @host
end

Class Method Details

.auth(data) ⇒ Object



41
42
43
44
45
46
47
# 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

Instance Method Details

#get(path) ⇒ Object



36
37
38
39
# 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) ⇒ Object



20
21
22
23
24
25
26
# 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) ⇒ Object



28
29
30
31
32
33
34
# 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