Sha256: e00242b5cf1e76073d2f523ddbffe0908f9246db0bceb168fcd2491e95f62d33

Contents?: true

Size: 1.44 KB

Versions: 29

Compression:

Stored size: 1.44 KB

Contents

require 'date'

module HaveAPI::Client
  class Parameters::Typed
    module Boolean
      def self.to_b(str)
        return true if str === true
        return true if str =~ /^(true|t|yes|y|1)$/i

        return false if str === false
        return false if str =~ /^(false|f|no|n|0)$/i

        false
      end
    end

    attr_reader :errors, :value

    def initialize(params, desc, value)
      @params = params
      @desc = desc
      @errors = []
      @value = coerce(value)
    end

    def valid?
      ret = Validator.validate(@desc[:validators], @value, @params)

      @errors.concat(ret) unless ret === true
      ret === true
    end

    def to_api
      if @desc[:type] == 'Datetime' && value.is_a?(Time)
        value.iso8601

      else
        value
      end
    end

    protected
    def coerce(raw)
      type = @desc[:type]

      val = if type == 'Integer'
        raw.to_i

      elsif type == 'Float'
        raw.to_f

      elsif type == 'Boolean'
        Boolean.to_b(raw)

      elsif type == 'Datetime'
        if raw.is_a?(::Time)
          raw

        elsif raw.is_a?(::Date) || raw.is_a?(::DateTime)
          raw.to_time

        else
          begin
            DateTime.iso8601(raw).to_time

          rescue ArgumentError
            @errors << 'not in ISO 8601 format'
            nil
          end
        end

      elsif %w(String Text).include?(type)
        raw.to_s

      else
        raw
      end

      val
    end
  end
end

Version data entries

29 entries across 29 versions & 1 rubygems

Version Path
haveapi-client-0.20.0 lib/haveapi/client/parameters/typed.rb
haveapi-client-0.19.3 lib/haveapi/client/parameters/typed.rb
haveapi-client-0.19.2 lib/haveapi/client/parameters/typed.rb
haveapi-client-0.19.1 lib/haveapi/client/parameters/typed.rb
haveapi-client-0.19.0 lib/haveapi/client/parameters/typed.rb
haveapi-client-0.18.2 lib/haveapi/client/parameters/typed.rb
haveapi-client-0.18.1 lib/haveapi/client/parameters/typed.rb
haveapi-client-0.18.0 lib/haveapi/client/parameters/typed.rb
haveapi-client-0.17.0 lib/haveapi/client/parameters/typed.rb
haveapi-client-0.16.3 lib/haveapi/client/parameters/typed.rb
haveapi-client-0.16.2 lib/haveapi/client/parameters/typed.rb
haveapi-client-0.16.1 lib/haveapi/client/parameters/typed.rb
haveapi-client-0.16.0 lib/haveapi/client/parameters/typed.rb
haveapi-client-0.15.1 lib/haveapi/client/parameters/typed.rb
haveapi-client-0.15.0 lib/haveapi/client/parameters/typed.rb
haveapi-client-0.14.2 lib/haveapi/client/parameters/typed.rb
haveapi-client-0.14.1 lib/haveapi/client/parameters/typed.rb
haveapi-client-0.14.0 lib/haveapi/client/parameters/typed.rb
haveapi-client-0.13.3 lib/haveapi/client/parameters/typed.rb
haveapi-client-0.13.2 lib/haveapi/client/parameters/typed.rb