Sha256: edd78484d454b647f4295490f35732b27300926f09585ef0d3d868150da4fbdc

Contents?: true

Size: 927 Bytes

Versions: 5

Compression:

Stored size: 927 Bytes

Contents

require "emites/resources/hooks"

module Emites
  module Resources
    class Base
      include Wisper::Publisher
      extend Hooks

      attr_accessor :http

      def initialize(http)
        @http = http
      end

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

      protected

      def respond_with_collection(response, naked_klass = entity_klass)
        hash = parsed_body(response)
        hash["collection"].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 base_klass
        @base_klass ||= self.class.name.split("::").last
      end

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

    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
emites-client-0.1.1 lib/emites/resources/base.rb
emites-client-0.1.0 lib/emites/resources/base.rb
emites-client-0.0.4 lib/emites/resources/base.rb
emites-client-0.0.3 lib/emites/resources/base.rb
emites-client-0.0.2 lib/emites/resources/base.rb