Sha256: dd683a0528def8578f5e138eabd01157590b02391d3e147d691de75c41717255

Contents?: true

Size: 1.21 KB

Versions: 1

Compression:

Stored size: 1.21 KB

Contents

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

module CodewarsApi
  module RequestHelper
    def self.add_api_key(request_options, api_key)
      request_options = request_options.dup
      request_options[:headers] = { 'Authorization' => api_key }
      request_options
    end

    def self.add_body_options(request_options, body_options)
      request_options = request_options.dup
      request_options[:body] = body_options
      request_options
    end

    def self.post(url, options = {})
      uri = URI(CodewarsApi::BASE_URL + url)
      headers = { 'Content-Type' => 'application/json' }
      headers.merge! options[:headers] if options
      request = Net::HTTP::Post.new(uri.path, headers)
      request.body = options[:body].to_json if options
      https(uri).request(request)
    end

    def self.get(url, options = nil)
      uri = URI(CodewarsApi::BASE_URL + url)
      headers = options[:headers] if options
      https(uri).request_get uri, headers
    end

    private

    def self.https(uri)
      https = Net::HTTP.new(uri.host, uri.port)
      https.use_ssl = true
      https
    end
  end
end

module Net
  class HTTPResponse
    def to_h
      JSON.parse(body).to_h
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
codewars_api-0.4.0 lib/codewars_api/helpers/request_helper.rb