Class: Sg::CLI

Inherits:
Thor
  • Object
show all
Defined in:
lib/sg.rb

Overview

SendGrid Command Line Interface Class

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.get_client(options) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
# 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) ⇒ Object



82
83
84
85
86
87
88
# 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

Instance Method Details

#client(*args) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
# 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