Class: Libmagellan::HTTP

Inherits:
Object
  • Object
show all
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 Method Summary collapse

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”)



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/libmagellan/http.rb', line 23

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
  @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

Instance Method Details

#generate_oauth1_2legged_request(uri, options = {}) ⇒ Object



79
80
81
82
83
84
85
# File 'lib/libmagellan/http.rb', line 79

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 = {}) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/libmagellan/http.rb', line 87

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) ⇒ Object



104
105
106
107
108
109
110
111
# File 'lib/libmagellan/http.rb', line 104

def json_body(r)
  body = r.body
  begin
    JSON.parse(body)
  rescue ::JSON::ParserError
    body
  end
end

#pingObject



113
114
115
# File 'lib/libmagellan/http.rb', line 113

def ping
  request("/ping")
end

#request_with_oauth(path, method = :get, body = "", headers = {}) ⇒ Object Also known as: request



66
67
68
69
70
71
72
73
74
75
76
# File 'lib/libmagellan/http.rb', line 66

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

#request_without_oauth(path, method = :get, body = "", headers = {}) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/libmagellan/http.rb', line 40

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?

    response = case method.downcase.to_sym
               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



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/libmagellan/http.rb', line 121

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 = {}) ⇒ Object



117
118
119
# File 'lib/libmagellan/http.rb', line 117

def test_ping(options = {})
  test("/ping", options){|r| r.code == '200' or r.body.strip == 'pong'}
end