Sha256: cde69cc46c16c2dcaaf6a9166ae7f5d326f432c349e6920505b2acedb4f3ad2c

Contents?: true

Size: 1.08 KB

Versions: 1

Compression:

Stored size: 1.08 KB

Contents

require 'rest-client'
require 'base64'
require 'json'

module Remetric
  class HTTP
    def initialize(client)
      @client = client
    end

    def client
      @client
    end

    def root_url
      # return "http://localhost:3000"
      "https://api.remetric.com"
    end

    def rc
      @rc ||= RestClient
    end

    def headers
      {
        Authorization: "Basic #{Base64.encode64("#{client.api_token}:#{client.secret_key}")}"
      }
    end

    def opts
      {
        content_type: :json,
        accept: :json
      }
    end

    def parse(response)
      JSON.parse response.body
    end

    def get(path, args = {})
      response = rc.get "#{root_url}#{path}", headers.merge(params: args)
      parse response
    end

    def post(path, args = {})
      response = rc.post "#{root_url}#{path}", args, headers
      parse response
    end

    def patch(path, args = {})
      response = rc.patch "#{root_url}#{path}", args, headers
      parse response
    end

    def delete(path)
      response = rc.delete "#{root_url}#{path}", headers
      parse response
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
remetric-1.0.0 lib/remetric/http.rb