Sha256: 96e6ee733cecbfc6b492a1c8a0b14816a637d9542bb4165766e872a1479c9d36

Contents?: true

Size: 1.49 KB

Versions: 5

Compression:

Stored size: 1.49 KB

Contents

require 'net/http'
require 'net/https'
require 'json'

module Chillout
  class HttpClient
    class NotSent < StandardError
      attr_reader :original_exception
      def initialize(original_exception)
        @original_exception = original_exception
      end
    end

    class NotReceived < StandardError
      attr_reader :original_exception

      def initialize(original_exception)
        @original_exception = original_exception
      end
    end

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

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

    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 = JSON.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

    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

  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
chillout-0.5.3 lib/chillout/server-side/http_client.rb
chillout-0.5.2 lib/chillout/server-side/http_client.rb
chillout-0.5.1 lib/chillout/server-side/http_client.rb
chillout-0.5.0 lib/chillout/http_client.rb
chillout-0.4.1 lib/chillout/http_client.rb