Class: Fastlane::Helper::MackerelApiHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/fastlane/plugin/mackerel_api/helper/mackerel_api_helper.rb

Class Method Summary collapse

Class Method Details

.call_endpoint(url, http_method, headers, body, secure) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/fastlane/plugin/mackerel_api/helper/mackerel_api_helper.rb', line 39

def self.call_endpoint(url, http_method, headers, body, secure)
  require 'excon'
  Excon.defaults[:ssl_verify_peer] = secure
  middlewares = Excon.defaults[:middlewares] + [Excon::Middleware::RedirectFollower] # allow redirect in case of repo renames
  UI.verbose("#{http_method} : #{url}")
  connection = Excon.new(url)
  connection.request(
    method: http_method,
    headers: headers,
    middlewares: middlewares,
    body: body,
    debug_request: FastlaneCore::Globals.verbose?,
    debug_response: FastlaneCore::Globals.verbose?
  )
end

.construct_body(body, raw_body) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/fastlane/plugin/mackerel_api/helper/mackerel_api_helper.rb', line 19

def self.construct_body(body, raw_body)
  body ||= {}
  if raw_body
    raw_body
  elsif body.kind_of?(Hash)
    body.to_json
  elsif body.kind_of?(Array)
    body.to_json
  else
    UI.user_error!("Please provide valid JSON, or a hash as request body") unless parse_json(body)
    body
  end
end

.construct_headers(api_key, overrides) ⇒ Object



4
5
6
7
8
9
10
11
# File 'lib/fastlane/plugin/mackerel_api/helper/mackerel_api_helper.rb', line 4

def self.construct_headers(api_key, overrides)
  headers = {
    'User-Agent' => 'fastlane-mackerel_api',
    'Content-Type' => 'application/json'
  }
  headers['X-Api-Key'] = api_key if api_key
  headers.merge(overrides || {})
end

.construct_url(server_url, path, url) ⇒ Object



13
14
15
16
17
# File 'lib/fastlane/plugin/mackerel_api/helper/mackerel_api_helper.rb', line 13

def self.construct_url(server_url, path, url)
  return_url = (server_url && path) ? File.join(server_url, path) : url
  UI.user_error!("Please provide either `server_url` (e.g. https://api.mackerelio.com) and 'path' or full 'url' for Mackerel API endpoint") unless return_url
  return_url
end

.parse_json(value) ⇒ Object



33
34
35
36
37
# File 'lib/fastlane/plugin/mackerel_api/helper/mackerel_api_helper.rb', line 33

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