Sha256: f7edbd11edcb2020409ccdbfcc0e5cd2c2d4ddff7aa750f43debc1b5f876725e

Contents?: true

Size: 1.36 KB

Versions: 6

Compression:

Stored size: 1.36 KB

Contents

module Zinc
  class Resource < Model
    class << self
      protected

      def get(path:, params: {})
        Zinc.logger.debug url(path, params)
        response = RestClient::Request.new(
          method: :get,
          url: url(path, params),
          user: Zinc.access_token,
          headers: headers
        ).execute
        # Zinc.logger.debug response.headers.inspect
        Zinc.logger.debug response.code
        Zinc.logger.debug response.body
        JSON.parse(response.body)
      end

      def post(path:, params: {})
        Zinc.logger.debug url(path)
        Zinc.logger.debug params.to_json
        response = RestClient::Request.new(
          method: :post,
          url: url(path),
          user: Zinc.access_token,
          payload: params.to_json,
          headers: headers
        ).execute
        # Zinc.logger.debug response.headers.inspect
        Zinc.logger.debug response.code
        Zinc.logger.debug response.body
        JSON.parse(response.body)
      end

      private

      def url(path, params = {})
        url = "#{Zinc.api_path}/#{path}"
        url += "?#{URI.encode_www_form(params)}" unless params.empty?
        url
      end

      def headers
        {
          accept: :json,
          content_type: :json,
          user_agent: 'Zinc API [Ruby] (https://github.com/tophatter/zinc-api)'
        }
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
zinc-api-0.0.6 lib/zinc/resource.rb
zinc-api-0.0.5 lib/zinc/resource.rb
zinc-api-0.0.4 lib/zinc/resource.rb
zinc-api-0.0.3 lib/zinc/resource.rb
zinc-api-0.0.2 lib/zinc/resource.rb
zinc-api-0.0.1 lib/zinc/resource.rb