Sha256: ca929d5893ebfdb088d2bc8c2055ab3201c217fbd36163c0e0d02c7fb031a2cf

Contents?: true

Size: 1.58 KB

Versions: 87

Compression:

Stored size: 1.58 KB

Contents

require "date"

module Recurly
  class Schema
    # The purpose of this class is to turn JSON parsed Hashes
    # defined into Recurly ruby objects. It's to be used
    # by the Resource as an extension.
    module ResourceCaster

      # Gives the class the ability to initialize itself
      # given some json data.
      #
      # @example
      #   Recurly::Resources::Account.cast({"code" => "mycode"})
      #   #=> #<Recurly::Resources::Account @attributes={:code=>"mycode"}>
      #
      # @param attributes [Hash] A primitive Hash from JSON.parse of Recurly response.
      # @return [Resource] the {Resource} (ruby object) representing the passed in JSON data.
      def cast(attributes = {})
        resource = new()
        attributes.each do |attr_name, val|
          schema_attr = self.schema.get_attribute(attr_name)

          if schema_attr
            val = if val.nil?
                val
              elsif schema_attr.is_valid?(val)
                schema_attr.cast(val)
              else
                if Recurly::STRICT_MODE
                  msg = "#{self.class}##{attr_name} does not have the right type. Value: #{val.inspect} was expected to be a #{schema_attr}"
                  raise ArgumentError, msg
                end
              end

            writer = "#{attr_name}="
            resource.send(writer, val)
          elsif Recurly::STRICT_MODE
            raise ArgumentError, "#{resource.class.name} encountered json attribute #{attr_name.inspect}: #{val.inspect} but it's unknown to it's schema"
          end
        end
        resource
      end
    end
  end
end

Version data entries

87 entries across 87 versions & 1 rubygems

Version Path
recurly-4.56.0 lib/recurly/schema/resource_caster.rb
recurly-4.55.0 lib/recurly/schema/resource_caster.rb
recurly-4.54.0 lib/recurly/schema/resource_caster.rb
recurly-4.53.0 lib/recurly/schema/resource_caster.rb
recurly-4.52.0 lib/recurly/schema/resource_caster.rb
recurly-4.49.0 lib/recurly/schema/resource_caster.rb
recurly-4.48.1 lib/recurly/schema/resource_caster.rb
recurly-4.48.0 lib/recurly/schema/resource_caster.rb
recurly-4.47.0 lib/recurly/schema/resource_caster.rb
recurly-4.46.0 lib/recurly/schema/resource_caster.rb
recurly-4.45.0 lib/recurly/schema/resource_caster.rb
recurly-4.44.0 lib/recurly/schema/resource_caster.rb
recurly-4.43.0 lib/recurly/schema/resource_caster.rb
recurly-4.42.0 lib/recurly/schema/resource_caster.rb
recurly-4.41.0 lib/recurly/schema/resource_caster.rb
recurly-4.40.0 lib/recurly/schema/resource_caster.rb
recurly-4.39.0 lib/recurly/schema/resource_caster.rb
recurly-4.38.0 lib/recurly/schema/resource_caster.rb
recurly-4.37.0 lib/recurly/schema/resource_caster.rb
recurly-3.28.0 lib/recurly/schema/resource_caster.rb