Module: Brawne::Request

Defined in:
lib/brawne/request.rb

Class Method Summary collapse

Class Method Details

.get(request) ⇒ Object

simple get method call a simple get on host using request



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/brawne/request.rb', line 11

def self.get(request)
  http_r = Net::HTTP.new(Brawne.host, Brawne.port)
  http_r.use_ssl = Brawne.ssl
  response = nil
  begin
    http_r.start() do |http|
      req = Net::HTTP::Get.new(request)
      req.add_field("USERNAME", Brawne.user)
      req.add_field("TOKEN", Brawne.token)
      response = http.request(req)
    end
    return [response.code, response.body]
  rescue Errno::ECONNREFUSED
    return [503, "unavailable"]
  end
end

.post(request, payload) ⇒ Object

simple post method. send the payload to the host with request the payload is expected to be json



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/brawne/request.rb', line 31

def self.post(request,payload)
  http_r = Net::HTTP.new(Brawne.host, Brawne.port)
  http_r.use_ssl = Brawne.ssl
  response = nil
  begin
    http_r.start() do |http|
      req = Net::HTTP::Post.new(request, initheader = {'Content-Type' =>'application/json'})
      req.add_field("USERNAME", Brawne.user)
      req.add_field("TOKEN", Brawne.token)
      req.body = payload
      req.set_form_data(payload)
      response = http.request(req)
    end
    return [response.code, response.body]
  rescue Errno::ECONNREFUSED
    return [503, "unavailable"]
  end
end