Class: Percy::Capybara::Client

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

Defined Under Namespace

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from UserAgent

#_client_info, #_ember_cli_rails_version, #_environment_info, #_rails_version, #_sinatra_version

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.



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/percy/capybara/client.rb', line 27

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

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

  @client = options[:client] || \
    Percy.client(client_info: _client_info, environment_info: _environment_info)

  return unless defined?(Rails) && defined?(Sprockets::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.



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

def client
  @client
end

#loaderObject

Returns the value of attribute loader.



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

def loader
  @loader
end

#loader_optionsObject

Returns the value of attribute loader_options.



25
26
27
# File 'lib/percy/capybara/client.rb', line 25

def loader_options
  @loader_options
end

#sprockets_environmentObject

Returns the value of attribute sprockets_environment.



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

def sprockets_environment
  @sprockets_environment
end

#sprockets_optionsObject

Returns the value of attribute sprockets_options.



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

def sprockets_options
  @sprockets_options
end

Instance Method Details

#disable!Object



66
67
68
# File 'lib/percy/capybara/client.rb', line 66

def disable!
  @enabled = false
end

#enabled?Boolean

Returns:

  • (Boolean)


53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/percy/capybara/client.rb', line 53

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)


85
86
87
# File 'lib/percy/capybara/client.rb', line 85

def failed?
  !!@failed
end

#initialize_loader(options = {}) ⇒ Object



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
126
127
128
129
# File 'lib/percy/capybara/client.rb', line 89

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)


44
45
46
47
48
49
50
51
# File 'lib/percy/capybara/client.rb', line 44

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)


70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/percy/capybara/client.rb', line 70

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