Sha256: e335d5e33d24f1b6da3384b499c1c01dfc96db93a06188a183833d8887d2238c

Contents?: true

Size: 1.08 KB

Versions: 1

Compression:

Stored size: 1.08 KB

Contents

require "httparty"

module Tsks
  class Request
    #@base_uri = "https://tsks-api.herokuapp.com/v1"
    # TODO: tmp for dev purposes
    @base_uri = "http://localhost:3000/v1"

    def self.base_uri
      @base_uri
    end

    def self.post endpoint, token=nil, body
      uri = URI "#{Request.base_uri}#{endpoint}"

      if token
        res = HTTParty.post uri, body: body,
                                 headers: {authorization: "Bearer #{token}"}
      else
        res = HTTParty.post uri, body: body
      end

      parsed_res = parse_response res.body
    end

    def self.get endpoint, token
      uri = URI "#{Request.base_uri}#{endpoint}"
      res = HTTParty.get uri, headers: {authorization: "Bearer #{token}"}
      parsed_res = parse_response res.body
    end

    def self.delete endpoint, token
      uri = URI "#{Request.base_uri}#{endpoint}"
      res = HTTParty.delete uri, headers: {authorization: "Bearer #{token}"}
      parsed_res = parse_response res.body
    end

    private

    def self.parse_response body
      JSON.parse body, symbolize_names: true
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
tsks-0.0.9 lib/tsks/request.rb