Class: Libmagellan::HTTP
- Inherits:
-
Object
- Object
- Libmagellan::HTTP
- Defined in:
- lib/libmagellan/http.rb
Constant Summary collapse
- DEFAULT_OPTIONS =
{ # consumer_key: "groovenauts-test", # consumer_secret: "test", # client_version: "0.0.1", }.freeze
Instance Attribute Summary collapse
-
#base_uri ⇒ Object
Returns the value of attribute base_uri.
-
#client_version ⇒ Object
Returns the value of attribute client_version.
-
#consumer_key ⇒ Object
Returns the value of attribute consumer_key.
-
#project ⇒ Object
Returns the value of attribute project.
-
#verbose ⇒ Object
Returns the value of attribute verbose.
Instance Method Summary collapse
- #generate_oauth1_2legged_request(uri, options = {}) ⇒ Object
- #generate_oauth_request(uri, options = {}) ⇒ Object
-
#initialize(*args) ⇒ HTTP
constructor
LibMagellan.new(“localhost”) LibMagellan.new(“localhost:3000”) LibMagellan.new(host: “localhost”, port: 3000) LibMagellan.new(“localhost”, consumer_key: “foo”, consumer_secret: “bar”) LibMagellan.new(“localhost:3000”, consumer_key: “foo”, consumer_secret: “bar”) LibMagellan.new(host: “localhost”, port: 3000, consumer_key: “foo”, consumer_secret: “bar”).
- #json_body(r) ⇒ Object
- #ping ⇒ Object
- #request_with_oauth(path, method = :get, body = "", headers = {}) ⇒ Object (also: #request)
- #request_without_oauth(path, method = :get, body = "", headers = {}) ⇒ Object
- #test(path, options = {}) ⇒ Object
- #test_ping(options = {}) ⇒ Object
Constructor Details
#initialize(*args) ⇒ HTTP
LibMagellan.new(“localhost”) LibMagellan.new(“localhost:3000”) LibMagellan.new(host: “localhost”, port: 3000) LibMagellan.new(“localhost”, consumer_key: “foo”, consumer_secret: “bar”) LibMagellan.new(“localhost:3000”, consumer_key: “foo”, consumer_secret: “bar”) LibMagellan.new(host: “localhost”, port: 3000, consumer_key: “foo”, consumer_secret: “bar”)
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/libmagellan/http.rb', line 26 def initialize(*args) = DEFAULT_OPTIONS.dup.update((args.last.is_a?(Hash) ? args.pop : {})) @project = .delete :project @consumer_key = .delete :consumer_key || @project @consumer_secret = .delete :consumer_secret @client_version = .delete :client_version @verbose = .delete :verbose @skip_verify = [:skip_verify] || false arg = args.first @base_uri = case arg when nil then URI::HTTP.build() when Array then URI::HTTP.build(arg) when Hash then URI::HTTP.build({scheme: "http", host: "localhost"}.update(arg)) when String then (URI.regexp =~ arg) ? URI.parse(arg) : URI::HTTP.build({scheme: "http", host: arg}.update()) end end |
Instance Attribute Details
#base_uri ⇒ Object
Returns the value of attribute base_uri.
11 12 13 |
# File 'lib/libmagellan/http.rb', line 11 def base_uri @base_uri end |
#client_version ⇒ Object
Returns the value of attribute client_version.
11 12 13 |
# File 'lib/libmagellan/http.rb', line 11 def client_version @client_version end |
#consumer_key ⇒ Object
Returns the value of attribute consumer_key.
11 12 13 |
# File 'lib/libmagellan/http.rb', line 11 def consumer_key @consumer_key end |
#project ⇒ Object
Returns the value of attribute project.
11 12 13 |
# File 'lib/libmagellan/http.rb', line 11 def project @project end |
#verbose ⇒ Object
Returns the value of attribute verbose.
12 13 14 |
# File 'lib/libmagellan/http.rb', line 12 def verbose @verbose end |
Instance Method Details
#generate_oauth1_2legged_request(uri, options = {}) ⇒ Object
92 93 94 95 96 97 98 |
# File 'lib/libmagellan/http.rb', line 92 def generate_oauth1_2legged_request(uri, ={}) header = generate_oauth_request(uri, ) do |c, o| c.two_legged = true [c, o] end return header end |
#generate_oauth_request(uri, options = {}) ⇒ Object
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/libmagellan/http.rb', line 100 def generate_oauth_request(uri, ={}) header = { "Cache-Control" => "no-store" } if @consumer_key.blank? and @consumer_secret.blank? and @project.blank? return header elsif (@consumer_key.blank? or @consumer_secret.blank?) and @project.present? header["Project"] = @project else init_opt = {client_credential_key: @consumer_key, client_credential_secret: @consumer_secret} [:uri] = uri if [:uri].blank? client = ::Signet::OAuth1::Client.new(init_opt) client, = yield(client, ) if block_given? req = client.generate_authenticated_request() header["Authorization"] = req["Authorization"] end return header end |
#json_body(r) ⇒ Object
117 118 119 120 121 122 123 124 |
# File 'lib/libmagellan/http.rb', line 117 def json_body(r) body = r.body begin JSON.parse(body) rescue ::JSON::ParserError body end end |
#ping ⇒ Object
126 127 128 |
# File 'lib/libmagellan/http.rb', line 126 def ping request("/ping") end |
#request_with_oauth(path, method = :get, body = "", headers = {}) ⇒ Object Also known as: request
79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/libmagellan/http.rb', line 79 def request_with_oauth(path, method=:get, body="", headers={}) uri = URI.join(@base_uri.to_s, path) = {method: method} auth_headers = generate_oauth1_2legged_request(uri, ) auth_headers["Client-Version"] = @client_version if @client_version.present? headers = headers.update(auth_headers) request_without_oauth(path, method, body, headers) do |r| yield(r) if block_given? r end end |
#request_without_oauth(path, method = :get, body = "", headers = {}) ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/libmagellan/http.rb', line 44 def request_without_oauth(path, method=:get, body="", headers={}) if body.is_a?(Hash) body = URI.encode_www_form(body) end uri = URI.join(@base_uri.to_s, path) http_client = Net::HTTP.new(uri.host, uri.port) if uri.scheme == 'https' http_client.use_ssl = true http_client.verify_mode = OpenSSL::SSL::VERIFY_NONE if @skip_verify end response = http_client.start do |http| path_query = "#{uri.path}" path_query << "?#{uri.query}" unless uri.query.blank? if verbose verbose_body = case method.downcase.to_sym when :patch, :put, :post then "\nBody:\n#{body}" else nil end $stderr.puts("\e[34m#{uri.to_s}\nHeaders:\n#{headers.inspect}#{verbose_body}\e[0m") end response = case method.downcase.to_sym when :head then http.head(path_query, headers) when :patch then http.patch(path_query, body, headers) when :delete then http.delete(path_query, headers) when :put then http.put(path_query, body, headers) when :post then http.post(path_query, body, headers) else http.get(path_query, headers) end yield(response) if block_given? end return response end |
#test(path, options = {}) ⇒ Object
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 |
# File 'lib/libmagellan/http.rb', line 134 def test(path, = {}) = { method: :get, body: "", headers: {} }.update( || {}) method = .delete(:method) body = .delete(:body) headers = .delete(:headers) max_retry_count = [:max_retry_count] || 5 retry_interval = [:retry_interval] || 3 c = 0 begin r = request(path, method, body, headers) unless yield(r) raise InvalidResponse, "invalid response code: #{r.code.inspect} body: #{r.body.strip.inspect}" end return self rescue Errno::ECONNREFUSED => e c += 1 if c > max_retry_count raise e else # puts "retrying GET /ping... #{c} times" sleep(retry_interval) retry end end end |
#test_ping(options = {}) ⇒ Object
130 131 132 |
# File 'lib/libmagellan/http.rb', line 130 def test_ping( = {}) test("/ping", ){|r| r.code == '200' or r.body.strip == 'pong'} end |