Sha256: 189613b3953c8208e5b06470f3485014d138fd992b3dde136d4197b4cde2dd94

Contents?: true

Size: 1.47 KB

Versions: 1

Compression:

Stored size: 1.47 KB

Contents

require 'net/http'

module RestAssured
  module Models
    class Double < ActiveRecord::Base
      serialize :response_headers, Hash

      VERBS = %w{GET POST PUT DELETE HEAD PATCH}
      STATUSES = Net::HTTPResponse::CODE_TO_OBJ.keys.map(&:to_i)

      validates_presence_of :fullpath
      validates_inclusion_of :verb, :in => VERBS
      validates_inclusion_of :status, :in => STATUSES

      after_initialize :set_status
      after_initialize :set_verb
      after_initialize :set_response_headers

      before_save :toggle_active
      after_destroy :set_active

      has_many :requests, :dependent => :destroy

      private

        def toggle_active
          ne = id ? '!=' : 'IS NOT'

          if active && Double.where("fullpath = ? AND verb = ? AND active = ? AND id #{ne} ?", fullpath, verb, true, id).exists?
            Double.where("fullpath = ? AND verb = ? AND id #{ne} ?", fullpath, verb, id).update_all :active => false
          end
        end

        def set_response_headers
          self.response_headers = {} unless response_headers.present? # present? protects against empty strings that may come in parameters
        end

        def set_verb
          self.verb = 'GET' unless verb.present?
        end

        def set_status
          self.status = 200 unless status.present?
        end

        def set_active
          if active && f = Double.where(:fullpath => fullpath).last
            f.active = true
            f.save
          end
        end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rest-assured-2.0.1 lib/rest-assured/models/double.rb