Class: Stash::Sword::HTTPHelper

Inherits:
Object
  • Object
show all
Includes:
LogUtils
Defined in:
lib/stash/sword/http_helper.rb

Overview

Utility class simplifying GET requests for HTTP/HTTPS resources.

Constant Summary collapse

DEFAULT_MAX_REDIRECTS =

The default number of redirects to follow before erroring out.

5
DEFAULT_TIMEOUT =

The default number of seconds to allow before timing out. Defaults to 10 minutes.

60 * 10

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from LogUtils

create_default_logger, #default_logger, #hash_to_log_msg, #level, #log, #log_error, #log_hash, #to_log_msg

Constructor Details

#initialize(user_agent:, username: nil, password: nil, redirect_limit: DEFAULT_MAX_REDIRECTS, timeout: DEFAULT_TIMEOUT, logger: nil) ⇒ HTTPHelper

Creates a new HTTPHelper



40
41
42
43
44
45
46
47
# File 'lib/stash/sword/http_helper.rb', line 40

def initialize(user_agent:, username: nil, password: nil, redirect_limit: DEFAULT_MAX_REDIRECTS, timeout: DEFAULT_TIMEOUT, logger: nil)
  @user_agent = user_agent
  @redirect_limit = redirect_limit
  @timeout = timeout
  @username = username
  @password = password
  @log = logger || default_logger
end

Instance Attribute Details

#passwordString (readonly)



32
33
34
# File 'lib/stash/sword/http_helper.rb', line 32

def password
  @password
end

#redirect_limitInteger



23
24
25
# File 'lib/stash/sword/http_helper.rb', line 23

def redirect_limit
  @redirect_limit
end

#timeoutInteger



26
27
28
# File 'lib/stash/sword/http_helper.rb', line 26

def timeout
  @timeout
end

#user_agentString



20
21
22
# File 'lib/stash/sword/http_helper.rb', line 20

def user_agent
  @user_agent
end

#usernameString (readonly)



29
30
31
# File 'lib/stash/sword/http_helper.rb', line 29

def username
  @username
end

Instance Method Details

#get(uri:, limit: redirect_limit) ⇒ String

Gets the content of the specified URI as a string.



53
54
55
56
57
58
59
# File 'lib/stash/sword/http_helper.rb', line 53

def get(uri:, limit: redirect_limit)
  do_get(uri, limit) do |success|
    # not 100% clear why we need an explicit return here; it
    # doesn't show up in unit tests but it does in example.rb
    return success.body
  end
end

#post(uri:, payload:, headers: {}, limit: redirect_limit)

Posts the specified payload string to the specified URI.



62
63
64
# File 'lib/stash/sword/http_helper.rb', line 62

def post(uri:, payload:, headers: {}, limit: redirect_limit)
  do_post_or_put(method: :post, uri: uri, payload: payload, headers: headers, limit: limit, timeout: timeout)
end

#put(uri:, payload:, headers: {}, limit: redirect_limit)

Puts the specified payload string to the specified URI.



67
68
69
# File 'lib/stash/sword/http_helper.rb', line 67

def put(uri:, payload:, headers: {}, limit: redirect_limit)
  do_post_or_put(method: :put, uri: uri, payload: payload, headers: headers, limit: limit, timeout: timeout)
end