Sha256: 16306ffb6e22eabf8b502cfc51090e32cea818e632c00e31d9555b2bd4c9c98c

Contents?: true

Size: 1.81 KB

Versions: 1

Compression:

Stored size: 1.81 KB

Contents

class Quandl::Client::Base
module Validation
  
  extend ActiveSupport::Concern

  included do
  
    before_save :halt_unless_valid!
    
    def valid_with_server?
      return false unless valid?
      return false unless errors_params.blank?
      return false unless errors_server.blank?
      true
    end
    
    def save!
      save
    end
    
    def blank?
      !present?
    end
    
    def exists?
      present?
    end
    
    def present?
      status >= 200 && status < 300
    end
    
    def saved?
      status >= 200 && status <= 210
    end
    
    def queried?
      status > 0
    end
    
    def human_status
      Quandl::Client::HTTP_STATUS_CODES[status]
    end
    
    def status
      metadata[:status].to_i
    end
    
    def parse_error
      error_messages[:response_errors].try( :[], :parse_error )
    end
    
    def error_messages
      valid?
      errors_client.deep_merge(errors_server).deep_merge(errors_params)
    end
    
    def errors_client
      errors.messages || {}
    end
    
    def errors_server
      messages = self.attributes[:errors] || {}
      messages[:message] = self.error if self.respond_to?(:error) && self.error.present?
      messages
    end
    
    def errors_params
      response_errors.present? ? { response_errors: response_errors } : {}
    end
    
    protected
  
    def halt_unless_valid!
      return false unless valid?
    end
    
    class UrlValidator < ActiveModel::EachValidator
      def validate_each(record, attribute, value)
        begin
          uri = URI.parse(value)
          resp = uri.kind_of?(URI::HTTP)
          
        rescue URI::InvalidURIError
          resp = false
        end
        unless resp == true
          record.errors[attribute] << (options[:message] || "is not an url")
        end
      end
    end
     
  end
end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
quandl_client-2.3.0 lib/quandl/client/base/validation.rb