class SiteWatcher::DSL::Page

Attributes

__sw_remove_on_fulfillment[R]
__sw_url[R]

Public Class Methods

new(url, method: :get, headers: {}, **opts) click to toggle source
# File lib/site_watcher.rb, line 111
def initialize(url, method: :get, headers: {}, **opts)
  @__sw_url = url
  @__sw_tests = []
  @__sw_method = method
  @__sw_headers = headers
  @__sw_http_opts = opts
  @__sw_fulfilled = nil
  @__sw_fetch = nil
  @__sw_remove_on_fulfillment = true
end

Public Instance Methods

__sw_run!(force=false) click to toggle source
# File lib/site_watcher.rb, line 150
def __sw_run!(force=false)
  if @__sw_fetch
    response = @__sw_fetch.call(@__sw_url)
  else
    response = ::HTTP
      .headers(@__sw_headers)
      .request(@__sw_method, @__sw_url, **@__sw_http_opts)

    case response.content_type.mime_type
    when /json/i
      response = ::JSON.parse(response.to_s)
    else
      response = ::Capybara::Node::Simple.new(response.to_s)
    end
  end

  begin
    @__sw_tests.each { |test| test.call(response) }
  rescue ::RSpec::Expectations::ExpectationNotMetError => err
    __sw_fulfilled! if force
    raise(err)
  end

  __sw_fulfilled!
end
body(body) click to toggle source
# File lib/site_watcher.rb, line 142
def body(body)
  @__sw_body = body
end
fetch(&block) click to toggle source
# File lib/site_watcher.rb, line 130
def fetch(&block)
  @__sw_fetch = block
end
fulfilled(&block) click to toggle source
# File lib/site_watcher.rb, line 126
def fulfilled(&block)
  @__sw_fulfilled = block
end
headers(hash) click to toggle source
# File lib/site_watcher.rb, line 134
def headers(hash)
  @__sw_headers = hash
end
http_method(http_method) click to toggle source
# File lib/site_watcher.rb, line 138
def http_method(http_method)
  @__sw_http_method = http_method
end
remove_on_fulfillment(bool) click to toggle source
# File lib/site_watcher.rb, line 146
def remove_on_fulfillment(bool)
  @__sw_remove_on_fulfillment = !!bool
end
test(&block) click to toggle source
# File lib/site_watcher.rb, line 122
def test(&block)
  @__sw_tests << block
end

Private Instance Methods

__sw_fulfilled!() click to toggle source
# File lib/site_watcher.rb, line 178
def __sw_fulfilled!
  @__sw_fulfilled.call(@__sw_url) if @__sw_fulfilled.respond_to?(:call)
end