class RequestBuilder

Public Class Methods

new(name: "", env: "dev") click to toggle source
# File lib/cadbury/helpers/request_builder.rb, line 7
def initialize(name: "", env: "dev")
  @env = env
  @name = name
  @endpoint = ""
  @headers = {}
  @method = "get"
  @body = {}
  @request = {}
  @config_manager = ConfigManager.new
end

Public Instance Methods

create_request() click to toggle source
# File lib/cadbury/helpers/request_builder.rb, line 18
def create_request
  get_endpoint
  get_headers
  get_method
  get_body
  self
end
get_all() click to toggle source
# File lib/cadbury/helpers/request_builder.rb, line 26
def get_all
  @config_manager.get_all[@env]["requests"]
end
get_by_name(name) click to toggle source
# File lib/cadbury/helpers/request_builder.rb, line 30
def get_by_name(name)
  @config_manager.get_all[@env]["requests"][name]
end
save() click to toggle source
# File lib/cadbury/helpers/request_builder.rb, line 34
def save
  configs = @config_manager.get_all
  if configs.nil? || configs == {}
    puts "Error: No Environments detected."
    puts "Please run 'cadburybot env set <env-name>' command to create a new environment"
    exit
  end
  configs[@env]["requests"][@name] = build_request
  @config_manager.save configs
end

Private Instance Methods

build_request() click to toggle source
# File lib/cadbury/helpers/request_builder.rb, line 76
def build_request
  {
    method: @method,
    headers: @headers,
    body: @body
  }
end
get_body() click to toggle source
# File lib/cadbury/helpers/request_builder.rb, line 70
def get_body
  puts "Body: []"
  body = $stdin.gets.chomp
  @body = JSON.parse body unless body.empty?
end
get_endpoint() click to toggle source
# File lib/cadbury/helpers/request_builder.rb, line 47
def get_endpoint
  puts "Endpoint: eg., /api/check-weather"
  @endpoint = $stdin.gets.chomp
end
get_headers() click to toggle source
# File lib/cadbury/helpers/request_builder.rb, line 52
def get_headers
  loop do
    puts "Headers: [headerKey:headerValue] ---> Type value or press <Enter> to skip"
    key, value = $stdin.gets.chomp.split ":"
    break if key.nil? || value.nil?

    @headers[key] = value
  end
rescue StandardError => e
  puts "Error: #{e.message}"
  exit
end
get_method() click to toggle source
# File lib/cadbury/helpers/request_builder.rb, line 65
def get_method
  puts "Method: [GET]"
  @method = $stdin.gets.chomp.upcase
end