Module: Jets::Gems::Api::Core
Instance Method Summary collapse
- #account ⇒ Object
- #build_request(klass, url, data = {}) ⇒ Object
- #delete(path, data = {}) ⇒ Object
- #get(path, data = {}) ⇒ Object
- #global_data ⇒ Object
- #http ⇒ Object
- #load_json(url, res) ⇒ Object
-
#ok?(http_code) ⇒ Boolean
Note: 422 is Unprocessable Entity.
- #patch(path, data = {}) ⇒ Object
- #post(path, data = {}) ⇒ Object
-
#request(klass, path, data = {}) ⇒ Object
Always translate raw json response to ruby Hash.
- #set_headers!(req) ⇒ Object
- #sts ⇒ Object
- #token ⇒ Object
-
#url(path) ⇒ Object
API does not include the /.
Instance Method Details
#account ⇒ Object
104 105 106 107 108 |
# File 'lib/jets/gems/api/core.rb', line 104 def account sts.get_caller_identity.account rescue nil end |
#build_request(klass, url, data = {}) ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/jets/gems/api/core.rb', line 15 def build_request(klass, url, data = {}) data = global_data.merge(data) if klass == Net::HTTP::Get url += "?#{URI.encode_www_form(data)}" if data.any? end req = klass.new(url) # url includes query string and uri.path does not, must used url set_headers!(req) if [Net::HTTP::Delete, Net::HTTP::Patch, Net::HTTP::Post, Net::HTTP::Put].include?(klass) text = JSON.dump(data) req.body = text req.content_length = text.bytesize end req end |
#delete(path, data = {}) ⇒ Object
100 101 102 |
# File 'lib/jets/gems/api/core.rb', line 100 def delete(path, data = {}) request(Net::HTTP::Delete, path, data) end |
#get(path, data = {}) ⇒ Object
88 89 90 |
# File 'lib/jets/gems/api/core.rb', line 88 def get(path, data = {}) request(Net::HTTP::Get, path, data) end |
#global_data ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/jets/gems/api/core.rb', line 36 def global_data Jets.boot params = {} params[:jets_env] = Jets.env.to_s params[:jets_extra] = Jets.extra.to_s if Jets.extra params[:name] = Jets.project_namespace params[:region] = Jets.aws.region params[:account] = Jets.aws.account params[:project_id] = Jets.config.project_name params[:serverlessgems_version] = Jets::Gems::VERSION params[:jets_version] = Jets::VERSION params[:ruby_version] = RUBY_VERSION params[:ruby_folder] = Jets::Gems.ruby_folder params end |
#http ⇒ Object
74 75 76 77 78 79 80 |
# File 'lib/jets/gems/api/core.rb', line 74 def http uri = URI(endpoint) http = Net::HTTP.new(uri.host, uri.port) http.open_timeout = http.read_timeout = 30 http.use_ssl = true if uri.scheme == "https" http end |
#load_json(url, res) ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/jets/gems/api/core.rb', line 56 def load_json(url, res) uri = URI(url) if ok?(res.code) JSON.load(res.body) else puts "Error: Non-successful http response status code: #{res.code}" puts "headers: #{res.each_header.to_h.inspect}" puts "ServerlessGems API #{url}" if ENV["SG_DEBUG"] raise "ServerlessGems API called failed: #{uri.host}" end end |
#ok?(http_code) ⇒ Boolean
Note: 422 is Unprocessable Entity. This means an invalid data payload was sent. We want that to error and raise
70 71 72 |
# File 'lib/jets/gems/api/core.rb', line 70 def ok?(http_code) http_code =~ /^20/ || http_code =~ /^40/ end |
#patch(path, data = {}) ⇒ Object
96 97 98 |
# File 'lib/jets/gems/api/core.rb', line 96 def patch(path, data = {}) request(Net::HTTP::Patch, path, data) end |
#post(path, data = {}) ⇒ Object
92 93 94 |
# File 'lib/jets/gems/api/core.rb', line 92 def post(path, data = {}) request(Net::HTTP::Post, path, data) end |
#request(klass, path, data = {}) ⇒ Object
Always translate raw json response to ruby Hash
8 9 10 11 12 13 |
# File 'lib/jets/gems/api/core.rb', line 8 def request(klass, path, data = {}) url = url(path) req = build_request(klass, url, data) resp = http.request(req) # send request load_json(url, resp) end |
#set_headers!(req) ⇒ Object
30 31 32 33 34 |
# File 'lib/jets/gems/api/core.rb', line 30 def set_headers!(req) req["Authorization"] = token if token req["x-account"] = account if account req["Content-Type"] = "application/vnd.api+json" end |
#sts ⇒ Object
111 112 113 |
# File 'lib/jets/gems/api/core.rb', line 111 def sts Aws::STS::Client.new end |
#token ⇒ Object
52 53 54 |
# File 'lib/jets/gems/api/core.rb', line 52 def token Jets::Gems::Config.instance.data["key"] end |
#url(path) ⇒ Object
API does not include the /. IE: app.terraform.io/api/v2
84 85 86 |
# File 'lib/jets/gems/api/core.rb', line 84 def url(path) "#{endpoint}/#{path}" end |