Class: Bartender::Request

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

Class Method Summary collapse

Class Method Details

.delete(path, options = {}) ⇒ Object


7
8
9
10
11
12
# File 'lib/bartender/request.rb', line 7

def self.delete(path, options = {})
  options[:token] ||= Bartender.configuration.private_token

  response = request.delete(uri(path, options))
  response.code.to_i == 200
end

.get(path, options = {}) ⇒ Object


14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/bartender/request.rb', line 14

def self.get(path, options = {})
  token = Bartender.configuration.public_token

  unless token.nil?
    options[:token] ||= token
  end

  response = request.get(uri(path, options))

  case response.code.to_i
  when 200
    MultiJson.load(response.body)
  else
    false
  end
end

.hash_to_query_string(hash) ⇒ Object


57
58
59
60
61
62
63
64
65
# File 'lib/bartender/request.rb', line 57

def self.hash_to_query_string(hash)
  hash.sort_by { |key, value|
    key.to_s
  }.delete_if { |key, value|
    value.to_s.empty?
  }.collect { |key, value|
    "#{CGI.escape(key.to_s).gsub(/%(5B|5D)/n) { [$1].pack("H*") }}=#{CGI.escape(value.to_s)}"
  }.join("&")
end

.post(path, attributes = {}, options = {}) ⇒ Object


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

def self.post(path, attributes = {}, options = {})
  options[:token] ||= Bartender.configuration.private_token

  response = request.post(uri(path, options), MultiJson.dump(attributes), "Content-Type" => "application/json")

  case response.code.to_i
  when 201
    response["Location"]
  when 400
    MultiJson.load(response.body)["errors"]
  else
    false
  end
end

.requestObject


46
47
48
49
50
51
# File 'lib/bartender/request.rb', line 46

def self.request
  http = Net::HTTP.new(Bartender.configuration.host, Bartender.configuration.port)
  http.open_timeout = Bartender.configuration.http_open_timeout
  http.read_timeout = Bartender.configuration.http_read_timeout
  http
end

.uri(path, options = {}) ⇒ Object


53
54
55
# File 'lib/bartender/request.rb', line 53

def self.uri(path, options = {})
  "/v#{API_VERSION}#{path}.json?#{hash_to_query_string(options)}"
end