Sha256: 40bd5120a3da1d1bb8f6a0244e66d32ef7d242902b015aa44def4ce6c76c9b08

Contents?: true

Size: 1.18 KB

Versions: 1

Compression:

Stored size: 1.18 KB

Contents

module TimeCamp
  class Request
    BASE_URL = 'https://www.timecamp.com/third_party/api/'
    API_TOKEN = ENV['TIMECAMP_API_TOKEN']

    # RESTful METHODS
    def self.get(resource, opts={}, data={})
      # return instance of response
      return request(:get, resource, opts, data)
    end

    def self.post(resource, opts={}, data={})
      # return instance of response
      return request(:post, resource, opts, data)
    end

    def self.put(resource, opts={}, data={})
      # return instance of response
      return request(:put, resource, opts, data)
    end

    def self.delete(resource, opts={}, data={})
      # return instance of response
      return request(:delete, resource, opts, data)
    end

    private
      def self.url(resource, opts)
        return BASE_URL + "#{ resource }/format/json/api_token/#{ API_TOKEN }/" + opts.to_a.flatten.join('/')
      end

      def self.request(action, resource, opts, data={})
        # response = RestClient.send(action.to_sym, url(resource, opts), data)
        response = RestClient::Request.execute(method: action.to_sym, url: url(resource, opts), payload: data)
        return TimeCamp::Response.new(response).to_h
      end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ruby-timecamp-0.0.2 lib/ruby-timecamp/request.rb