Module: CC::Service::HTTP
Defined Under Namespace
Modules: ClassMethods
Constant Summary collapse
- REDIRECT_CODES =
[302, 307].freeze
Instance Method Summary collapse
- #allow_internal_webhooks? ⇒ Boolean
-
#ca_file ⇒ Object
Gets the path to the SSL Certificate Authority certs.
- #formatted_post_response(response, url, body) ⇒ Object
- #http(options = {}) ⇒ Object
- #http_method(method, url = nil, body = nil, headers = nil) ⇒ Object
- #raw_get(url = nil, params = nil, headers = nil) ⇒ Object
- #raw_post(url = nil, body = nil, headers = nil) ⇒ Object
- #service_get(url = nil, body = nil, headers = nil, &block) ⇒ Object
- #service_post(url, body = nil, headers = nil, &block) ⇒ Object
- #service_post_with_redirects(url, body = nil, headers = nil, &block) ⇒ Object
Instance Method Details
#allow_internal_webhooks? ⇒ Boolean
107 108 109 110 |
# File 'lib/cc/service/http.rb', line 107 def allow_internal_webhooks? var = ENV["CODECLIMATE_ALLOW_INTERNAL_WEBHOOKS"] || "" var == "1" || var == "true" end |
#ca_file ⇒ Object
Gets the path to the SSL Certificate Authority certs. These were taken from: curl.haxx.se/ca/cacert.pem
Returns a String path.
93 94 95 |
# File 'lib/cc/service/http.rb', line 93 def ca_file @ca_file ||= ENV.fetch("CODECLIMATE_CA_FILE", File.("../../../../config/cacert.pem", __FILE__)) end |
#formatted_post_response(response, url, body) ⇒ Object
97 98 99 100 101 102 103 104 105 |
# File 'lib/cc/service/http.rb', line 97 def formatted_post_response(response, url, body) { ok: response.success?, params: body.as_json, endpoint_url: url, status: response.status, message: "Success", } end |
#http(options = {}) ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/cc/service/http.rb', line 69 def http( = {}) @http ||= begin config = self.class. config.each do |key, | next if key == :adapter sub_hash = [key] ||= {} .each do |sub_key, sub_value| sub_hash[sub_key] ||= sub_value end end [:ssl][:ca_file] ||= ca_file Faraday.new() do |b| b.use(CC::Service::ResponseCheck) b.request(:url_encoded) b.adapter(*Array([:adapter] || config[:adapter])) end end end |
#http_method(method, url = nil, body = nil, headers = nil) ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/cc/service/http.rb', line 53 def http_method(method, url = nil, body = nil, headers = nil) block = Proc.new if block_given? http.send(method) do |req| req.url(url) if url req.headers.update(headers) if headers req.body = body if body block.call req if block unless allow_internal_webhooks? safe_webhook = CC::Service::SafeWebhook.new(url) safe_webhook.validate!(req) end end end |
#raw_get(url = nil, params = nil, headers = nil) ⇒ Object
41 42 43 44 45 46 |
# File 'lib/cc/service/http.rb', line 41 def raw_get(url = nil, params = nil, headers = nil) http_method(:get, url, nil, headers) do |req| req.params.update(params) if params yield req if block_given? end end |
#raw_post(url = nil, body = nil, headers = nil) ⇒ Object
48 49 50 51 |
# File 'lib/cc/service/http.rb', line 48 def raw_post(url = nil, body = nil, headers = nil) block = Proc.new if block_given? http_method :post, url, body, headers, &block end |
#service_get(url = nil, body = nil, headers = nil, &block) ⇒ Object
21 22 23 |
# File 'lib/cc/service/http.rb', line 21 def service_get(url = nil, body = nil, headers = nil, &block) raw_get(url, body, headers, &block) end |
#service_post(url, body = nil, headers = nil, &block) ⇒ Object
25 26 27 28 29 |
# File 'lib/cc/service/http.rb', line 25 def service_post(url, body = nil, headers = nil, &block) block ||= ->(*_args) { Hash.new } response = raw_post(url, body, headers) formatted_post_response(response, url, body).merge(block.call(response)) end |
#service_post_with_redirects(url, body = nil, headers = nil, &block) ⇒ Object
31 32 33 34 35 36 37 38 39 |
# File 'lib/cc/service/http.rb', line 31 def service_post_with_redirects(url, body = nil, headers = nil, &block) block ||= ->(*_args) { Hash.new } response = raw_post(url, body, headers) if REDIRECT_CODES.include?(response.status) response = raw_post(response.headers["location"], body, headers) end formatted_post_response(response, url, body).merge(block.call(response)) end |