Sha256: 1e9b0d71b56dc8ef0cf16d60daba0048e5f84c5500c5973c44e2b71278a0d47a

Contents?: true

Size: 1.87 KB

Versions: 7

Compression:

Stored size: 1.87 KB

Contents

require "cobrato/resources/hooks"

module Cobrato
  module Resources
    class Base
      include Wisper::Publisher
      extend Hooks
      attr_accessor :http

      def collection_name
        "#{base_klass.downcase}s"
      end

      def initialize(http)
        @http = http
      end

      def parsed_body(response)
        MultiJson.load(response.body)
      rescue MultiJson::ParseError
        {}
      end

      def create(params)
        http.post(resource_base_path, { body: params }) do |response|
          respond_with_entity(response)
        end
      end

      def show(id)
        http.get("#{resource_base_path}/#{id}") do |response|
          respond_with_entity(response)
        end
      end

      def list
        http.get(resource_base_path) do |response|
          respond_with_collection(response)
        end
      end

      def destroy(id)
        http.delete("#{resource_base_path}/#{id}") do |response|
          response.code == 204
        end
      end

      def update(id, params)
        http.put("#{resource_base_path}/#{id}", { body: params }) do |response|
          respond_with_entity(response)
        end
      end

      notify :create, :destroy

      protected
        def respond_with_collection(response, naked_klass = entity_klass)
          hash = parsed_body(response)
          hash[collection_name].map do |item|
            naked_klass.new(item)
          end
        end

        def respond_with_entity(response, naked_klass = entity_klass)
          item = parsed_body(response)
          naked_klass.new(item)
        end

        def resource_base_path
          @resource_base_path ||= "/#{collection_name}"
        end

        def base_klass
          @base_klass ||= self.class.name.split("::").last
        end

        def entity_klass
          @entity_klass ||= Cobrato::Entities.const_get(base_klass.to_sym)
        end

    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
cobrato-client-0.2.5 lib/cobrato/resources/base.rb
cobrato-client-0.2.4 lib/cobrato/resources/base.rb
cobrato-client-0.2.3 lib/cobrato/resources/base.rb
cobrato-client-0.2.3.pre lib/cobrato/resources/base.rb
cobrato-client-0.2.2 lib/cobrato/resources/base.rb
cobrato-client-0.2.1 lib/cobrato/resources/base.rb
cobrato-client-0.1.0 lib/cobrato/resources/base.rb