Sha256: de2a9bafde8f46adcda0de06ccefcdb06a364ce293a19ec7318e0938ea1ee0fe

Contents?: true

Size: 1.07 KB

Versions: 3

Compression:

Stored size: 1.07 KB

Contents

# frozen_string_literal: true
require 'rest-client'
require 'forwardable'
require 'base64'

module FreshdeskApiclient
  module REST
    class Resources
      def initialize(base_url, options={})
        @options = {url: "#{base_url}/#{options[:path] || end_point}"}
        @headers = headers options[:credentials]
        RestClient.log = options[:logger]
      end

      def list
        execute(method: :get, headers: @headers.dup.reject! {|key| [:'Content-Type'].include?(key) })
      end

      def create(json_payload)
        execute(method: :post, headers: @headers, payload: json_payload)
      end

      protected

      def end_point
        self.class.name.split('::').last.downcase
      end

      private

      def execute(options)
        RestClient::Request.execute @options.merge(options)
      end

      def headers(credentials)
        {
          Authorization: "Basic #{Base64.encode64("#{credentials[:username]}:#{credentials[:password]}")}",
          'Content-Type': 'application/json',
          Accept: 'application/json'
        }
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
freshdesk_apiclient-0.1.3 lib/freshdesk_apiclient/rest/resources.rb
freshdesk_apiclient-0.1.2 lib/freshdesk_apiclient/rest/resources.rb
freshdesk_apiclient-0.1.1 lib/freshdesk_apiclient/rest/resources.rb