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
31
32
# File 'lib/percy/capybara/client.rb', line 22

def initialize(options = {})
  @failed = false

  @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



52
53
54
# File 'lib/percy/capybara/client.rb', line 52

def disable!
  @enabled = false
end

#enabled?Boolean

Returns:

  • (Boolean)


39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/percy/capybara/client.rb', line 39

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

  # Disable if PERCY_ENABLE is set to 0
  return @enabled = false if ENV['PERCY_ENABLE'] == '0'

  # Enable if required environment variables are set
  return @enabled = true if required_environment_variables_set?

  # Disable otherwise
  return @enabled = false
end

#failed?Boolean

Returns:

  • (Boolean)


71
72
73
# File 'lib/percy/capybara/client.rb', line 71

def failed?
  return !!@failed
end

#initialize_loader(options = {}) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/percy/capybara/client.rb', line 75

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

#required_environment_variables_set?Boolean

Check that environment variables required for Percy::Client are set

Returns:

  • (Boolean)


35
36
37
# File 'lib/percy/capybara/client.rb', line 35

def required_environment_variables_set?
  !(ENV['PERCY_PROJECT'].nil? || ENV['PERCY_TOKEN'].nil?)
end

#rescue_connection_failures(&block) ⇒ Object

Raises:

  • (ArgumentError)


56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/percy/capybara/client.rb', line 56

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