Sha256: 4a741e74cbcdcd0866d46eb9253acfe4d9616ef3b689e23be6cd77dd79834f17

Contents?: true

Size: 1.3 KB

Versions: 1

Compression:

Stored size: 1.3 KB

Contents

require 'rest_client'
require 'json'

module CobotClient
  class ApiClient
    include UrlHelper

    def initialize(access_token)
      @access_token = access_token
    end

    def get_resources(subdomain)
      get subdomain, '/resources'
    end

    def create_booking(subdomain, resource_id, attributes)
      post subdomain, "/resources/#{resource_id}/bookings", attributes
    end

    def update_booking(subdomain, id, attributes)
      put subdomain, "/bookings/#{id}", attributes
    end

    def delete_booking(subdomain, id)
      delete subdomain, "/bookings/#{id}"
    end

    private

    def put(subdomain, path, params)
      JSON.parse RestClient.put(cobot_url(subdomain, "/api#{path}"), params, auth_headers).body,
        symbolize_names: true
    end

    def delete(subdomain, path)
      RestClient.delete(cobot_url(subdomain, "/api#{path}"), auth_headers)
    end

    def post(subdomain, path, params)
      JSON.parse RestClient.post(cobot_url(subdomain, "/api#{path}"), params, auth_headers).body,
        symbolize_names: true
    end

    def get(subdomain, path)
      JSON.parse(
        RestClient.get(cobot_url(subdomain, "/api#{path}"), auth_headers).body,
        symbolize_names: true
      )
    end

    def auth_headers
      {'Authorization' => "Bearer #{@access_token}"}
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
cobot_client-0.5.0 lib/cobot_client/api_client.rb