Sha256: 36b9b554d7f43d08656e15ba0ace377bddf6be5a873ded24e5b86b6a9b696e2c

Contents?: true

Size: 1.65 KB

Versions: 1

Compression:

Stored size: 1.65 KB

Contents

module Pina
  class RestAdapter
    class << self
      def get(resource, id = nil)
        fail ConfigurationNotSet unless Pina.configuration

        request = Typhoeus.get(url(resource, id), headers:  auth)
        Response.new(request.response_code, request.body)
      end

      def post(resource, payload)
        fail ConfigurationNotSet unless Pina.configuration

        request = Typhoeus.post(url(resource, nil), headers: auth
          .merge(content_type), body: ActiveSupport::JSON.encode(payload))

        Response.new(request.response_code, request.body)
      end

      def patch(resource, id, payload)
        fail ConfigurationNotSet unless Pina.configuration

        request = Typhoeus.patch(url(resource, id), headers: auth
          .merge(content_type), body: ActiveSupport::JSON.encode(payload))

        Response.new(request.response_code, request.body)
      end

      private

      def content_type
        {
          'Accept-Encoding' => 'application/json',
          'Content-Type' =>  'application/json'
        }
      end

      def auth
        { 'Authorization' => 'Basic ' + Base64
          .strict_encode64("#{Pina.configuration.email}:"\
                           "#{Pina.configuration.api_token}")
        }
      end

      def url(resource, id)
        Pina.configuration.base_url + "#{resource}/#{id}"
      end
    end

    class Response
      attr_accessor :body, :status_code

      def initialize(status_code, body)
        @status_code = status_code
        @body        = body
      end

      def ok?
        status_code == 200 || status_code == 201
      end

      def to_hash
        JSON.parse(body)
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
pina-0.1.0 lib/pina/rest_adapter.rb