Sha256: 3f440a991e037769b886fcc1e6affed9532a3626824ca42483c90e97165e5e1a

Contents?: true

Size: 1.07 KB

Versions: 7

Compression:

Stored size: 1.07 KB

Contents

require 'active_remote/serializers/json'

module ActiveRemote
  module Serialization
    def self.included(klass)
      klass.class_eval do
        include ::ActiveRemote::Serializers::JSON
      end
    end

    # Examine the given response and add any errors to our internal errors
    # list. If no response is given, use the last response.
    #
    def add_errors_from_response(response=self.last_response)
      return unless response.respond_to?(:errors)

      response.errors.each do |error|
        if error.respond_to?(:message)
          errors.add(error.field, error.message)
        elsif error.respond_to?(:messages)
          error.messages.each do |message|
            errors.add(error.field, message)
          end
        end
      end
    end

    # Examine the last response and serialize any records returned into Active
    # Remote objects.
    #
    def serialize_records
      return nil unless last_response.respond_to?(:records)

      last_response.records.map do |record|
        remote = self.class.new(record.to_hash)
        remote
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
active_remote-1.7.0 lib/active_remote/serialization.rb
active_remote-1.6.1 lib/active_remote/serialization.rb
active_remote-1.6.0 lib/active_remote/serialization.rb
active_remote-1.5.9 lib/active_remote/serialization.rb
active_remote-1.5.8 lib/active_remote/serialization.rb
active_remote-1.5.7 lib/active_remote/serialization.rb
active_remote-1.5.6 lib/active_remote/serialization.rb