Sha256: af6af0b878fa7347446a821c6cceae18edc403116596afce2f135df162e84795

Contents?: true

Size: 1.28 KB

Versions: 2

Compression:

Stored size: 1.28 KB

Contents

module Charger

  class Client
    attr_accessor :api_key, :subdomain

    def initialize api_key, subdomain
      @api_key = api_key
      @subdomain = subdomain
    end

    def get resource, headers={}
      JSON.parse(RestClient.get(resource_url(resource), rest_headers(headers)))
    rescue RestClient::Exception => e
      raise Charger::Exception.new(e.response)
    end

    def post resource, payload, headers={}
      JSON.parse(RestClient.post(resource_url(resource), payload, rest_headers(headers)))
    rescue RestClient::Exception => e
      raise Charger::Exception.new(e.response)
    end

    def put resource, payload, headers={}
      JSON.parse(RestClient.put(resource_url(resource), payload, rest_headers(headers)))
    rescue RestClient::Exception => e
      raise Charger::Exception.new(e.response)
    end

    def delete resource, headers={}
      JSON.parse(RestClient.delete(resource_url(resource), rest_headers(headers)))
    rescue RestClient::Exception => e
      raise Charger::Exception.new(e.response)
    end

    private

    def rest_headers options={}
      { :content_type => :json, :accept => :json }.merge(options)
    end

    def resource_url resource
      req = resource.gsub(/^\//,'')
      "https://#{@api_key}:x@#{@subdomain}.chargify.com/#{req}"
    end
  end

end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
charger-0.2.0 lib/charger/client.rb
charger-0.1.1 lib/charger/client.rb