Class: Chillout::PlainHttpClient

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

Defined Under Namespace

Classes: CommunicationError, NotCreated

Constant Summary collapse

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializePlainHttpClient

Returns a new instance of PlainHttpClient.



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

def initialize
  @host = if ENV['CHILLOUT_CLIENT_HOST']
    ENV['CHILLOUT_CLIENT_HOST']
  else
    "api.chillout.io"
  end
  @port = if ENV['CHILLOUT_CLIENT_PORT']
    ENV['CHILLOUT_CLIENT_PORT'].to_i
  else
    443
  end
  @ssl = if ENV['CHILLOUT_CLIENT_SSL']
    ENV['CHILLOUT_CLIENT_SSL'] == 'true'
  else
    true
  end
end

Instance Attribute Details

#hostObject (readonly)

Returns the value of attribute host.



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

def host
  @host
end

#portObject (readonly)

Returns the value of attribute port.



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

def port
  @port
end

#sslObject (readonly)

Returns the value of attribute ssl.



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

def ssl
  @ssl
end

Instance Method Details

#post(path, data) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/chillout/server_side/plain_http_client.rb', line 47

def post(path, data)
  begin
    http = Net::HTTP.new(host, port)
    http.use_ssl = ssl
    request_spec = Net::HTTP::Post.new(path)
    request_spec.body = MultiJson.dump(data)
    request_spec.content_type = MEDIA_TYPE
    response = http.start do
      http.request(request_spec)
    end
    raise NotCreated.new(response) if response.code != "201"
    MultiJson.load(response.body)
  rescue NotCreated
    raise
  rescue => e
    raise CommunicationError.new(e)
  end
end