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

Parameters:

  • user_agent (String)

    the User-Agent string to send when making requests

  • redirect_limit (Integer) (defaults to: DEFAULT_MAX_REDIRECTS)

    the number of redirects to follow before erroring out (defaults to DEFAULT_MAX_REDIRECTS)

  • logger (Logger, nil) (defaults to: nil)

    the logger to use, or nil to use a default logger



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)

Returns the HTTP Basic Authentication password.

Returns:

  • (String)

    the HTTP Basic Authentication password



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

def password
  @password
end

#redirect_limitInteger

Returns the number of redirects to follow before erroring out.

Returns:

  • (Integer)

    the number of redirects to follow before erroring out



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

def redirect_limit
  @redirect_limit
end

#timeoutInteger

Returns the number of seconds to allow before timing out.

Returns:

  • (Integer)

    the number of seconds to allow before timing out



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

def timeout
  @timeout
end

#user_agentString

Returns the User-Agent string to send when making requests.

Returns:

  • (String)

    the User-Agent string to send when making requests



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

def user_agent
  @user_agent
end

#usernameString (readonly)

Returns the HTTP Basic Authentication username.

Returns:

  • (String)

    the HTTP Basic Authentication username



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.

Parameters:

  • uri (URI)

    the URI to download

  • limit (Integer) (defaults to: redirect_limit)

    the number of redirects to follow (defaults to #redirect_limit)

Returns:

  • (String)

    the content of the URI



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