Class: Percy::Capybara::Client

Inherits:
Object
  • Object
show all
Includes:
Builds, Snapshots
Defined in:
lib/percy/capybara/client.rb,
lib/percy/capybara/client/builds.rb,
lib/percy/capybara/client/snapshots.rb

Defined Under Namespace

Modules: Builds, Snapshots Classes: BuildNotInitializedError, Error, WebMockBlockingConnectionsError

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Snapshots

#snapshot

Methods included from Builds

#build_initialized?, #current_build, #finalize_current_build, #initialize_build

Constructor Details

#initialize(options = {}) ⇒ Client

Returns a new instance of Client.



22
23
24
25
26
27
28
29
30
# File 'lib/percy/capybara/client.rb', line 22

def initialize(options = {})
  @client = options[:client] || Percy.client
  @enabled = options[:enabled]

  if defined?(Rails)
    @sprockets_environment = options[:sprockets_environment] || Rails.application.assets
    @sprockets_options = options[:sprockets_options] || Rails.application.config.assets
  end
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



16
17
18
# File 'lib/percy/capybara/client.rb', line 16

def client
  @client
end

#custom_loaderObject

Returns the value of attribute custom_loader.



20
21
22
# File 'lib/percy/capybara/client.rb', line 20

def custom_loader
  @custom_loader
end

#sprockets_environmentObject

Returns the value of attribute sprockets_environment.



18
19
20
# File 'lib/percy/capybara/client.rb', line 18

def sprockets_environment
  @sprockets_environment
end

#sprockets_optionsObject

Returns the value of attribute sprockets_options.



19
20
21
# File 'lib/percy/capybara/client.rb', line 19

def sprockets_options
  @sprockets_options
end

Instance Method Details

#disable!Object



42
43
44
# File 'lib/percy/capybara/client.rb', line 42

def disable!
  @enabled = false
end

#enabled?Boolean

Returns:

  • (Boolean)


32
33
34
35
36
37
38
39
40
# File 'lib/percy/capybara/client.rb', line 32

def enabled?
  return @enabled if !@enabled.nil?

  # Enable if PERCY_ENABLE=1 in local dev (allow disabling if 0).
  return @enabled ||= ENV['PERCY_ENABLE'] == '1' if ENV['PERCY_ENABLE']

  # If in supported CI environment.
  @enabled ||= !Percy::Client::Environment.current_ci.nil?
end

#failed?Boolean

Returns:

  • (Boolean)


61
62
63
# File 'lib/percy/capybara/client.rb', line 61

def failed?
  return !!@failed
end

#initialize_loader(options = {}) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/percy/capybara/client.rb', line 65

def initialize_loader(options = {})
  if custom_loader
    Percy.logger.debug { 'Using a custom loader to discover assets.' }
    custom_loader.new(options)
  elsif sprockets_environment && sprockets_options
    Percy.logger.debug { 'Using sprockets_loader to discover assets.' }
    options[:sprockets_environment] = sprockets_environment
    options[:sprockets_options] = sprockets_options
    Percy::Capybara::Loaders::SprocketsLoader.new(options)
  else
    Percy.logger.debug { 'Using native_loader to discover assets (slower).' }
    Percy::Capybara::Loaders::NativeLoader.new(options)
  end
end

#rescue_connection_failures(&block) ⇒ Object

Raises:

  • (ArgumentError)


46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/percy/capybara/client.rb', line 46

def rescue_connection_failures(&block)
  raise ArgumentError.new('block is required') if !block_given?
  begin
    block.call
  rescue Percy::Client::ServerError,  # Rescue server errors.
      Percy::Client::PaymentRequiredError,  # Rescue quota exceeded errors.
      Percy::Client::ConnectionFailed,  # Rescue some networking errors.
      Percy::Client::TimeoutError => e
    Percy.logger.error(e)
    @enabled = false
    @failed = true
    nil
  end
end