Class: Fastlane::Helper::TestingbotHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/fastlane/plugin/testingbot/helper/testingbot_helper.rb

Constant Summary collapse

API_BASE_URL =
'https://api.testingbot.com'.freeze
UPLOAD_TIMEOUT =

App binaries can be large; mirror the official TestingBot Ruby client's generous read timeout.

600
OPEN_TIMEOUT =
30

Class Method Summary collapse

Class Method Details

.error_reason(response) ⇒ Object



55
56
57
58
59
# File 'lib/fastlane/plugin/testingbot/helper/testingbot_helper.rb', line 55

def self.error_reason(response)
  body = response ? parse_json(response.body) : nil
  message = body.is_a?(Hash) && (body['error'] || body['message'])
  message || (response && "HTTP #{response.code}") || 'unknown error'
end

.parse_json(raw) ⇒ Object



61
62
63
64
65
# File 'lib/fastlane/plugin/testingbot/helper/testingbot_helper.rb', line 61

def self.parse_json(raw)
  JSON.parse(raw.to_s)
rescue JSON::ParserError
  nil
end

.parse_success(response) ⇒ Object



48
49
50
51
52
53
# File 'lib/fastlane/plugin/testingbot/helper/testingbot_helper.rb', line 48

def self.parse_success(response)
  body = parse_json(response.body)
  UI.user_error!('App upload to TestingBot failed: could not parse the server response') unless body.is_a?(Hash)

  body
end

.upload(key, secret, path, file_path: nil, remote_url: nil) ⇒ Hash

Uploads (or replaces) an app on TestingBot Storage.

Parameters:

  • key (String)

    TestingBot API key (used as the HTTP Basic auth username)

  • secret (String)

    TestingBot API secret (used as the HTTP Basic auth password)

  • path (String)

    the request path, e.g. "/v1/storage" or "/v1/storage/"

  • file_path (String, nil) (defaults to: nil)

    a local app file to upload via multipart/form-data

  • remote_url (String, nil) (defaults to: nil)

    a publicly reachable URL for TestingBot to fetch server-side

Returns:

  • (Hash)

    the parsed JSON response, e.g. => "tb://"



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/fastlane/plugin/testingbot/helper/testingbot_helper.rb', line 24

def self.upload(key, secret, path, file_path: nil, remote_url: nil)
  file = file_path ? File.new(file_path, 'rb') : nil
  payload = file ? { multipart: true, file: file } : { url: remote_url }

  response = RestClient::Request.execute(
    method: :post,
    url: "#{API_BASE_URL}#{path}",
    user: key,
    password: secret,
    payload: payload,
    headers: { 'User-Agent' => "fastlane-plugin-testingbot/#{Fastlane::Testingbot::VERSION}" },
    timeout: UPLOAD_TIMEOUT,
    open_timeout: OPEN_TIMEOUT
  )

  parse_success(response)
rescue RestClient::ExceptionWithResponse => e
  UI.user_error!("App upload to TestingBot failed: #{error_reason(e.response)}")
rescue StandardError => e
  UI.user_error!("App upload to TestingBot failed: #{e.message}")
ensure
  file&.close
end