class Sg::CLI

SendGrid Command Line Interface Class

Public Class Methods

get_client(options) click to toggle source
# File lib/sg.rb, line 90
def self.get_client(options)
  if options[:user] && options[:pass]
    up = "#{options[:user]}:#{options[:pass]}"
    headers = {}
    headers['Authorization'] = "Basic #{Base64.urlsafe_encode64(up)}"
    SendGrid::API.new(api_key: '', request_headers: headers).client
  else
    api_key = options[:apikey]
    api_key ||= ENV['SENDGRID_API_KEY']
    SendGrid::API.new(api_key: api_key).client
  end
end
parameterise(options) click to toggle source
# File lib/sg.rb, line 82
def self.parameterise(options)
  options.each_with_object({}) do |(k, v), memo|
    if k.to_s == 'request_body' || k.to_s == 'query_params'
      memo[k.to_s.to_sym] = JSON.parse(v) unless v.nil?
    end
  end
end

Public Instance Methods

client(*args) click to toggle source
# File lib/sg.rb, line 69
def client(*args)
  return puts Sg::VERSION if options[:version]
  idx = 0
  params = CLI.parameterise(options)
  response = args.inject(CLI.get_client(options)) do |c, arg|
    idx += 1
    args.length == idx ? c.send(arg, params) : c.send('_', arg)
  end
  puts response.status_code if options[:response_status]
  puts response.headers if options[:response_header]
  puts response.body
end