class Libmagellan::HTTP
Constants
- DEFAULT_OPTIONS
Attributes
base_uri[RW]
client_version[RW]
consumer_key[RW]
project[RW]
verbose[RW]
Public Class Methods
new(*args)
click to toggle source
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”)
# File lib/libmagellan/http.rb, line 26 def initialize(*args) options = DEFAULT_OPTIONS.dup.update((args.last.is_a?(Hash) ? args.pop : {})) @project = options.delete :project @consumer_key = options.delete :consumer_key || @project @consumer_secret = options.delete :consumer_secret @client_version = options.delete :client_version @verbose = options.delete :verbose @skip_verify = options[:skip_verify] || false arg = args.first @base_uri = case arg when nil then URI::HTTP.build(options) 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(options)) end end
Public Instance Methods
generate_oauth1_2legged_request(uri, options={})
click to toggle source
# File lib/libmagellan/http.rb, line 92 def generate_oauth1_2legged_request(uri, options={}) header = generate_oauth_request(uri, options) do |c, o| c.two_legged = true [c, o] end return header end
generate_oauth_request(uri, options={}) { |client, options| ... }
click to toggle source
# File lib/libmagellan/http.rb, line 100 def generate_oauth_request(uri, options={}) 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} options[:uri] = uri if options[:uri].blank? client = ::Signet::OAuth1::Client.new(init_opt) client, options = yield(client, options) if block_given? req = client.generate_authenticated_request(options) header["Authorization"] = req["Authorization"] end return header end
json_body(r)
click to toggle source
# 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()
click to toggle source
# File lib/libmagellan/http.rb, line 126 def ping request("/ping") end
request_with_oauth(path, method=:get, body="", headers={}) { |r| ... }
click to toggle source
# File lib/libmagellan/http.rb, line 79 def request_with_oauth(path, method=:get, body="", headers={}) uri = URI.join(@base_uri.to_s, path) options = {method: method} auth_headers = generate_oauth1_2legged_request(uri, options) 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
Also aliased as: request
request_without_oauth(path, method=:get, body="", headers={}) { |response| ... }
click to toggle source
# 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 = {}) { |r| ... }
click to toggle source
# File lib/libmagellan/http.rb, line 134 def test(path, options = {}) options = { method: :get, body: "", headers: {} }.update(options || {}) method = options.delete(:method) body = options.delete(:body) headers = options.delete(:headers) max_retry_count = options[:max_retry_count] || 5 retry_interval = options[: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 = {})
click to toggle source
# File lib/libmagellan/http.rb, line 130 def test_ping(options = {}) test("/ping", options){|r| r.code == '200' or r.body.strip == 'pong'} end