Class: Amaranth::Request

Inherits:
Object
  • Object
show all
Defined in:
lib/amaranth/request.rb

Class Method Summary collapse

Class Method Details

.delete(path) ⇒ Object



24
25
26
27
# File 'lib/amaranth/request.rb', line 24

def self.delete path
  result = request(Net::HTTP::Delete.new(path))
  result.code == "200" or raise Amaranth::RequestError, result.body
end

.get(path) ⇒ Object



9
10
11
12
# File 'lib/amaranth/request.rb', line 9

def self.get path
  result = request(Net::HTTP::Get.new(path))
  JSON.parse(result.body)
end

.post(path, body) ⇒ Object



14
15
16
17
# File 'lib/amaranth/request.rb', line 14

def self.post path, body
  result = request(Net::HTTP::Post.new(path), JSON.dump(body))
  result.code == "201" or raise Amaranth::RequestError, result.body
end

.put(path, body) ⇒ Object



19
20
21
22
# File 'lib/amaranth/request.rb', line 19

def self.put path, body
  result = request(Net::HTTP::Put.new(path), JSON.dump(body))
  result.code == "200" or raise Amaranth::RequestError, result.body
end

.request(req, body = nil) ⇒ Object



29
30
31
32
33
34
35
36
37
# File 'lib/amaranth/request.rb', line 29

def self.request req, body = nil
  Net::HTTP.start("amara.org", use_ssl: true) do |http|
    req["Content-Type"] = "application/json"
    req["X-api-username"] = Amaranth.api_username
    req["X-api-key"] = Amaranth.api_key
    req.body = body
    http.request(req)
  end
end