Class: NeverBounce::CLI::Script::RequestMaker

Inherits:
Meaningful show all
Defined in:
lib/never_bounce/cli/script/request_maker.rb

Overview

An API request maker script base class.

Constant Summary collapse

SHARED_ENVARS =

Shared envar defaults.

{
  "AUTO_START" => ["Start processing the job immediately after parsing", ["y", "n"]],
  "RUN_SAMPLE" => ["Run this job as a sample", ["y", "n"]],
}

Instance Attribute Summary collapse

Attributes inherited from Meaningful

#banner_text, #envar_text, #help_text, #manifest, #options_text

Attributes inherited from Base

#argv, #env, #stderr, #stdout

Instance Method Summary collapse

Methods inherited from Meaningful

#call_slim_main, error_klasses, format_envar_examples, #handle_help_and_options, #help?, #main, #option_parser, #options, #slim_main

Methods inherited from Base

#env_falsey?, #env_truthy?, env_value_truthy?, #main, #system, #verbose?

Instance Attribute Details

#api_keyString

Returns:

  • (String)

25
26
27
# File 'lib/never_bounce/cli/script/request_maker.rb', line 25

def api_key
  @api_key ||= env["API_KEY"] || user_config.api_key or raise UsageError, "API key not given, use `API_KEY=`"
end

#api_urlString

Returns:

  • (String)

31
32
33
34
35
# File 'lib/never_bounce/cli/script/request_maker.rb', line 31

def api_url
  @api_url ||= igetset(:api_url) do
    env["API_URL"] || user_config.api_url
  end
end

#requestObject

This method is abstract.

An instance of API::Request::Base successor.

Returns:

  • (Object)

41
42
43
# File 'lib/never_bounce/cli/script/request_maker.rb', line 41

def request
  @request or raise NotImplementedError, "Redefine `request` in your class: #{self.class}"
end

#request_curlArray

Request's cURL representation. Default is request.to_curl

Returns:

  • (Array)

48
49
50
# File 'lib/never_bounce/cli/script/request_maker.rb', line 48

def request_curl
  require_attr(:request).to_curl
end

#responseObject

An API::Response::Base successor instance.

Returns:

  • (Object)

55
56
57
# File 'lib/never_bounce/cli/script/request_maker.rb', line 55

def response
  @response ||= require_attr(:session).response
end

#server_rawString

Raw server response text. Default is session.server_raw. This attribute is used by RAW=y mode only.

Returns:

  • (String)

63
64
65
# File 'lib/never_bounce/cli/script/request_maker.rb', line 63

def server_raw
  @server_raw ||= require_attr(:session).server_raw
end

#sessionObject

An instance of API::Session built around #request.

Returns:

  • (Object)

70
71
72
# File 'lib/never_bounce/cli/script/request_maker.rb', line 70

def session
  @session ||= API::Session.new(request: require_attr(:request))
end

#user_configUserConfig

Returns:


76
77
78
# File 'lib/never_bounce/cli/script/request_maker.rb', line 76

def user_config
  @user_config ||= UserConfig.new
end

Instance Method Details

#get_table_value(r, hspec) ⇒ Object (private)

Extract response's value according to a heading spec.

get_table_value(reasponse, ["ID", :id, :right])

85
86
87
88
89
90
91
# File 'lib/never_bounce/cli/script/request_maker.rb', line 85

def get_table_value(r, hspec)
  if (m = hspec[1]).is_a? Proc
    m.(r)
  else
    r.public_send(m)
  end
end

#inil(v) ⇒ String (private)

“Inspect or nil” – format a scalar for table-friendly output.

inil(5)     # => "5"
inil(nil)   # => "-"

Returns:

  • (String)

100
101
102
# File 'lib/never_bounce/cli/script/request_maker.rb', line 100

def inil(v)
  v.nil?? "-" : v.inspect
end

Print request as a ready-to-run cURL command. Return 0.


106
107
108
109
# File 'lib/never_bounce/cli/script/request_maker.rb', line 106

def print_curl_request
  stdout.puts "curl #{request_curl.map(&:shellescape).join(' ')}"
  0
end

Print error response as a standard table. Return 1.

return print_error_response if response_error?

114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/never_bounce/cli/script/request_maker.rb', line 114

def print_error_response
  "ErrorResponse".tap do |label|
    headings = [
      ["Status", :status, :center],
      ["Message", :message],
      ["ExecTime", :execution_time, :right],
    ]

    table = Table.new(
      headings: headings.map { |ar| ar[0] },
      rows: [headings.map { |ar| get_table_value(response, ar) }],
    ).align!(headings)

    stdout.puts "\n#{label}:"
    stdout.puts table
  end

  1
end

Print raw response. Return 0.

return print_raw_response if env_truthy? "RAW"

137
138
139
140
# File 'lib/never_bounce/cli/script/request_maker.rb', line 137

def print_server_raw
  stdout.puts server_raw
  0
end

#slim_main1Object

————————————— Main


144
145
146
147
148
149
150
151
152
153
# File 'lib/never_bounce/cli/script/request_maker.rb', line 144

def slim_main1
  # Perform common boilerplate actions.
  return print_curl_request if env_truthy? "CURL"

  # Any of these, unless during tests, touch `response`, which triggers the actual request.
  return print_server_raw if env_truthy? "RAW"
  return print_error_response if response.error?

  call_slim_main(0)
end