Class: Net::Hippie::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/net/hippie/client.rb

Overview

A simple client for connecting with http resources.

Constant Summary collapse

DEFAULT_HEADERS =
{
  'Accept' => 'application/json',
  'Content-Type' => 'application/json',
  'User-Agent' => "net/hippie #{Net::Hippie::VERSION}"
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Client

Returns a new instance of Client.



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/net/hippie/client.rb', line 15

def initialize(options = {})
  @options = options
  @mapper = options.fetch(:mapper, ContentTypeMapper.new)
  @logger = options.fetch(:logger, Net::Hippie.logger)
  @follow_redirects = options.fetch(:follow_redirects, 0)
  @default_headers = options.fetch(:headers, DEFAULT_HEADERS)
  @connections = Hash.new do |hash, key|
    scheme, host, port = key
    hash[key] = Connection.new(scheme, host, port, options)
  end
end

Instance Attribute Details

#follow_redirectsObject (readonly)

Returns the value of attribute follow_redirects.



13
14
15
# File 'lib/net/hippie/client.rb', line 13

def follow_redirects
  @follow_redirects
end

#loggerObject (readonly)

Returns the value of attribute logger.



13
14
15
# File 'lib/net/hippie/client.rb', line 13

def logger
  @logger
end

#mapperObject (readonly)

Returns the value of attribute mapper.



13
14
15
# File 'lib/net/hippie/client.rb', line 13

def mapper
  @mapper
end

Instance Method Details

#delete(uri, headers: {}, body: {}, &block) ⇒ Object



55
56
57
# File 'lib/net/hippie/client.rb', line 55

def delete(uri, headers: {}, body: {}, &block)
  run(uri, Net::HTTP::Delete, headers, body, &block)
end

#execute(uri, request, limit: follow_redirects, &block) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/net/hippie/client.rb', line 27

def execute(uri, request, limit: follow_redirects, &block)
  connection = connection_for(uri)
  response = connection.run(request)
  if limit.positive? && response.is_a?(Net::HTTPRedirection)
    url = connection.build_url_for(response['location'])
    request = request_for(Net::HTTP::Get, url)
    execute(url, request, limit: limit - 1, &block)
  else
    block_given? ? yield(request, response) : response
  end
end

#get(uri, headers: {}, body: {}, &block) ⇒ Object



39
40
41
# File 'lib/net/hippie/client.rb', line 39

def get(uri, headers: {}, body: {}, &block)
  run(uri, Net::HTTP::Get, headers, body, &block)
end

#patch(uri, headers: {}, body: {}, &block) ⇒ Object



43
44
45
# File 'lib/net/hippie/client.rb', line 43

def patch(uri, headers: {}, body: {}, &block)
  run(uri, Net::HTTP::Patch, headers, body, &block)
end

#post(uri, headers: {}, body: {}, &block) ⇒ Object



47
48
49
# File 'lib/net/hippie/client.rb', line 47

def post(uri, headers: {}, body: {}, &block)
  run(uri, Net::HTTP::Post, headers, body, &block)
end

#put(uri, headers: {}, body: {}, &block) ⇒ Object



51
52
53
# File 'lib/net/hippie/client.rb', line 51

def put(uri, headers: {}, body: {}, &block)
  run(uri, Net::HTTP::Put, headers, body, &block)
end

#with_retry(retries: 3) ⇒ Object

attempt 1 -> delay 0.1 second attempt 2 -> delay 0.2 second attempt 3 -> delay 0.4 second attempt 4 -> delay 0.8 second attempt 5 -> delay 1.6 second attempt 6 -> delay 3.2 second attempt 7 -> delay 6.4 second attempt 8 -> delay 12.8 second



67
68
69
70
71
72
73
74
75
# File 'lib/net/hippie/client.rb', line 67

def with_retry(retries: 3)
  retries = 0 if retries.nil? || retries.negative?

  0.upto(retries) do |n|
    attempt(n, retries) do
      return yield self
    end
  end
end