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.



25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/percy/capybara/client.rb', line 25

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

  @client = options[:client] || Percy.client
  @enabled = options[:enabled]

  @loader_options = options[:loader_options] || {}

  return unless defined?(Rails)

  @sprockets_environment = options[:sprockets_environment] || Rails.application.assets
  @sprockets_options = options[:sprockets_options] || Rails.application.config.assets
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



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

def client
  @client
end

#loaderObject

Returns the value of attribute loader.



22
23
24
# File 'lib/percy/capybara/client.rb', line 22

def loader
  @loader
end

#loader_optionsObject

Returns the value of attribute loader_options.



23
24
25
# File 'lib/percy/capybara/client.rb', line 23

def loader_options
  @loader_options
end

#sprockets_environmentObject

Returns the value of attribute sprockets_environment.



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

def sprockets_environment
  @sprockets_environment
end

#sprockets_optionsObject

Returns the value of attribute sprockets_options.



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

def sprockets_options
  @sprockets_options
end

Instance Method Details

#disable!Object



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

def disable!
  @enabled = false
end

#enabled?Boolean

Returns:

  • (Boolean)


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

def enabled?
  return @enabled unless @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
  @enabled = false
end

#failed?Boolean

Returns:

  • (Boolean)


81
82
83
# File 'lib/percy/capybara/client.rb', line 81

def failed?
  !!@failed
end

#initialize_loader(options = {}) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/percy/capybara/client.rb', line 85

def initialize_loader(options = {})
  merged_options = loader_options.merge(options)

  is_sprockets = sprockets_environment && sprockets_options

  if is_sprockets
    merged_options[:sprockets_environment] = sprockets_environment
    merged_options[:sprockets_options]     = sprockets_options
  end

  if loader
    case loader
    when :filesystem
      Percy.logger.debug { 'Using filesystem_loader to discover assets.' }
      Percy::Capybara::Loaders::FilesystemLoader.new(merged_options)
    when :native
      Percy.logger.debug { 'Using native_loader to discover assets (slow).' }
      Percy::Capybara::Loaders::NativeLoader.new(merged_options)
    when :ember_cli_rails
      Percy.logger.debug { 'Using ember_cli_rails_loader to discover assets.' }
      mounted_apps = merged_options.delete(:mounted_apps)
      Percy::Capybara::Loaders::EmberCliRailsLoader.new(mounted_apps, merged_options)
    else
      Percy.logger.debug { 'Using a custom loader to discover assets.' }
      loader.new(merged_options)
    end
  elsif is_sprockets
    Percy.logger.debug { 'Using sprockets_loader to discover assets.' }
    Percy::Capybara::Loaders::SprocketsLoader.new(merged_options)
  else
    unless @warned_about_native_loader
      Percy.logger.warn \
        '[DEPRECATED] The native_loader is deprecated and will be opt-in in a future ' \
        'release. You should move to the faster, more reliable filesystem_loader. See the ' \
        'docs for Non-Rails frameworks: https://percy.io/docs/clients/ruby/capybara '
      @warned_about_native_loader = true
    end
    Percy.logger.debug { 'Using native_loader to discover assets (slower).' }
    Percy::Capybara::Loaders::NativeLoader.new(merged_options)
  end
end

#required_environment_variables_set?Boolean

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

Returns:

  • (Boolean)


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

def required_environment_variables_set?
  if !ENV['PERCY_TOKEN'].nil? && ENV['PERCY_PROJECT'].nil?
    raise '[percy] It looks like you were trying to enable Percy because PERCY_TOKEN ' \
      'is set, but you are missing the PERCY_PROJECT environment variable!'
  end

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

#rescue_connection_failuresObject

Raises:

  • (ArgumentError)


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

def rescue_connection_failures
  raise ArgumentError, 'block is required' unless block_given?
  begin
    yield
  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