module Agendor

  class Entity < Agendor::Base

    def create(params)
      body = process_hash(params)
      HTTParty.post(resource_path, body: body.to_json, headers: headers)
    end

    def get(query)
      HTTParty.get("#{resource_path}?q=#{query}", headers: headers)
    end

    def update(entity_id, params)
      body = process_hash(params)
      HTTParty.put("#{resource_path}/#{entity_id}", body: body.to_json, headers: headers)
    end

    def process_hash(params)
      params.select {|k, v| hash_keys.include?(k) }
    end

  end
end

module Agendor

  class Entity < Agendor::Base

    SUCCESS_RESPONSE_CODE = /2\d\d/

    def create(params)
      body = process_hash(params)
      response = HTTParty.post(resource_path, body: body.to_json, headers: headers)
      raise EntityProcessingError, response unless response =~ SUCCESS_RESPONSE_CODE
    end

    def get(query)
      HTTParty.get("#{resource_path}?q=#{query}", headers: headers)
      raise EntityProcessingError, response unless response =~ SUCCESS_RESPONSE_CODE
    end

    def update(entity_id, params)
      body = process_hash(params)
      HTTParty.put("#{resource_path}/#{entity_id}", body: body.to_json, headers: headers)
      raise EntityProcessingError, response unless response =~ SUCCESS_RESPONSE_CODE
    end

    def process_hash(params)
      params.select {|k, v| hash_keys.include?(k) }
    end

    # This response should be raised when an error occurs
    class EntityProcessingError < StandardError
      def initialize(response)
        @response = response
      end
    end
  end
end

Raise a default response when an error occurs
Raise a default response when an error occurs
