class HttpHelper

Public Class Methods

new(api_key, access_token) click to toggle source
# File lib/HttpHelper.rb, line 14
def initialize(api_key, access_token)
        @api_key = api_key
        @access_token = access_token
end

Public Instance Methods

delete(endpoint_path, query_params, headers = nil) click to toggle source
# File lib/HttpHelper.rb, line 66
def delete(endpoint_path, query_params, headers = nil)

        uri = get_uri(endpoint_path)
        if !query_params.nil?
                uri.query = URI.encode_www_form query_params
        end   
        req = Net::HTTP::Delete.new uri.request_uri
        return send uri, req, @api_key, @access_token, headers

end
get(endpoint_path, query_params, headers = nil) click to toggle source
# File lib/HttpHelper.rb, line 23
def get(endpoint_path, query_params, headers = nil)
        
        uri = get_uri(endpoint_path)
        #puts uri
        if !query_params.nil?
                uri.query = URI.encode_www_form query_params
        end
        #puts "REQUEST URI: #{uri.request_uri}"
        req = Net::HTTP::Get.new uri.request_uri
        return send uri, req, @api_key, @access_token, headers

end
get_uri(path) click to toggle source
# File lib/HttpHelper.rb, line 19
def get_uri(path)
        return URI.parse "#{Api_Host::API_BASE_URL}#{path}"
end
post(endpoint_path, query_params, body, headers = nil) click to toggle source
# File lib/HttpHelper.rb, line 36
def post(endpoint_path, query_params, body, headers = nil)

        uri = get_uri(endpoint_path)
        if !query_params.nil?
                uri.query = URI.encode_www_form query_params
        end   
        req = Net::HTTP::Post.new uri.request_uri
        if !body.nil?
                req["Content-Type"] = "application/json"
                req.body = body.to_json
        end
        return send uri, req, @api_key, @access_token, headers

end
put(endpoint_path, query_params, body, headers = nil) click to toggle source
# File lib/HttpHelper.rb, line 51
def put(endpoint_path, query_params, body, headers = nil)

        uri = get_uri(endpoint_path)
        if !query_params.nil?
                uri.query = URI.encode_www_form query_params
        end   
        req = Net::HTTP::Put.new uri.request_uri
        if !body.nil?
                req["Content-Type"] = "application/json"
                req.body = body.to_json
        end
        return send uri, req, @api_key, @access_token, headers

end

Private Instance Methods

os() click to toggle source
# File lib/HttpHelper.rb, line 78
def os
        @os ||= (
        host_os = RbConfig::CONFIG['host_os']
        case host_os
                when /mswin|msys|mingw|cygwin|bccwin|wince|emc/
                        :windows
                when /darwin|mac os/
                        :macosx
                when /linux/
                        :linux
                when /solaris|bsd/
                        :unix
                else
                        raise Error::WebDriverError, "unknown os: #{host_os.inspect}"
                end)
end
send(uri, request, api_key, bearer_token = "", headers) click to toggle source
# File lib/HttpHelper.rb, line 96
def send(uri, request, api_key, bearer_token = "", headers)

        # define HTTPS connection
        https = Net::HTTP.new(uri.host, uri.port)
        https.use_ssl = true
        https.verify_mode = OpenSSL::SSL::VERIFY_NONE

        # define headers
        request.initialize_http_header(headers)
        request["User-Agent"] = "GettImagesApiSdk/#{GettyImagesApi::VERSION} (#{os} ; Ruby #{RUBY_VERSION})"
        request["Api-Key"] = api_key
        request["Authorization"] = "Bearer #{bearer_token}" unless bearer_token.empty?
        
        # send request
        resp = https.request request

        if !resp.is_a?(Net::HTTPSuccess)
                data = JSON.parse(resp.body)                 
                raise "HTTP RESPONSE: #{data}" 
        end

        if resp.code == '204'
                return Hash.new
        end

        return JSON.parse(resp.body)

end