Sha256: c3bb3493187c2b13b433356d850570a259c96d88bce7bb2007c1f015a02a6b30

Contents?: true

Size: 1.09 KB

Versions: 5

Compression:

Stored size: 1.09 KB

Contents

require "clx_rest_api/response"
require "net/https"
require "net/http"
require "json"

module CLXRestAPI
  class Request
    attr_reader :uri

    def initialize(uri, method: :get, config: CLXRestAPI.config)
      @config = config
      @method = method
      @uri = URI(uri)
    end

    def execute(body = {})
      response = http.request http_request(body)
      Response.new(response)
    end

    private

    def http
      @http ||= Net::HTTP.new(uri.host, uri.port)
      @http.tap do |protocol|
        protocol.use_ssl = true
        protocol.verify_mode = OpenSSL::SSL::VERIFY_PEER
      end
    end

    def http_request(body)
      send "#{@method}_request", body
    end

    def get_request(body)
      @uri.query = URI.encode_www_form(body)
      Net::HTTP::Get.new(@uri).tap do |req|
        req["Authorization"] = "Bearer #{@config.api_token}"
      end
    end

    def post_request(body)
      Net::HTTP::Post.new(@uri).tap do |req|
        req["Authorization"] = "Bearer #{@config.api_token}"
        req.content_type = "application/json"
        req.body = body.to_json
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
clx_rest_api-0.1.4 lib/clx_rest_api/request.rb
clx_rest_api-0.1.3 lib/clx_rest_api/request.rb
clx_rest_api-0.1.2 lib/clx_rest_api/request.rb
clx_rest_api-0.1.1 lib/clx_rest_api/request.rb
clx_rest_api-0.1.0 lib/clx_rest_api/request.rb