Class: Chillout::HttpClient

Inherits:
Object
  • Object
show all
Defined in:
lib/chillout/server_side/http_client.rb

Defined Under Namespace

Classes: NotReceived, NotSent

Constant Summary collapse

MEDIA_TYPE =
"application/vnd.chillout.v1+json"

Instance Method Summary collapse

Constructor Details

#initialize(config, logger) ⇒ HttpClient

Returns a new instance of HttpClient.



24
25
26
27
# File 'lib/chillout/server_side/http_client.rb', line 24

def initialize(config, logger)
  @config = config
  @logger = logger
end

Instance Method Details

#get(path) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/chillout/server_side/http_client.rb', line 44

def get(path)
  http = Net::HTTP.new(@config.hostname, @config.port)
  http.use_ssl = @config.ssl
  request_spec = Net::HTTP::Get.new(path)
  request_spec.content_type = MEDIA_TYPE
  request_spec.basic_auth @config.authentication_user, @config.authentication_password
  http.start do
    http.request(request_spec)
  end
rescue => e
  raise NotReceived.new(e)
end

#post(path, data) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/chillout/server_side/http_client.rb', line 29

def post(path, data)
  http = Net::HTTP.new(@config.hostname, @config.port)
  http.use_ssl = @config.ssl
  request_spec = Net::HTTP::Post.new(path)
  request_spec.body = MultiJson.dump(data)
  request_spec.content_type = MEDIA_TYPE
  request_spec.basic_auth @config.authentication_user, @config.authentication_password
  http.start do
    http.request(request_spec)
  end
rescue => e
  @logger.error("#{e.class}: #{e.message}")
  raise NotSent.new(e)
end