Module: Angus::Remote::Utils

Defined in:
lib/angus/remote/utils.rb

Constant Summary collapse

HTTP_METHODS_WITH_BODY =
%w(post put)
RE_PATH_PARAM =
/:\w+/
SEVERE_STATUS_CODES =
%w(500 501 503)

Class Method Summary collapse

Class Method Details

.build_base_request(method, path, multipart_request = false) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/angus/remote/utils.rb', line 60

def self.build_base_request(method, path, multipart_request = false)
  case method.to_s.downcase
  when 'get'
    Net::HTTP::Get.new(path)
  when 'post'
    multipart_request ? Http::MultipartMethods::Post.new(path) : Net::HTTP::Post.new(path)
  when 'put'
    multipart_request ? Http::MultipartMethods::Put.new(path) : Net::HTTP::Put.new(path)
  when 'delete'
    Net::HTTP::Delete.new(path)
  else
    raise MethodArgumentError.new(method)
  end
end

.build_json_request(method, path, params) ⇒ Object



75
76
77
78
79
80
81
# File 'lib/angus/remote/utils.rb', line 75

def self.build_json_request(method, path, params)
  request = build_base_request(method, path)
  request.body = JSON(params)
  request['Content-Type'] = 'application/json'

  request
end

.build_normal_request(method, path, params) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/angus/remote/utils.rb', line 37

def self.build_normal_request(method, path, params)
  multipart_request = Http::Multipart.hash_contains_files?(params)

  params = if multipart_request
             Http::Multipart::QUERY_STRING_NORMALIZER.call(params)
           else
             Http::QueryParams.to_params(params)
           end

  if HTTP_METHODS_WITH_BODY.include?(method)
    request = build_base_request(method, path, multipart_request)
    request.body = params
  else
    uri = URI(path)
    uri.query = params

    request = build_base_request(method, uri.to_s)
  end

  request
end

.build_path(path, path_params) ⇒ String

Builds the URI path. It applies the params to the path

Examples:

path = "/users/:user_id/profile/:profile_id"
path_params = [4201, 2]

build_path(path, path_params) #=> "/users/4201/profile/2"

Parameters:

  • path (String)

    the path with place holders

  • path_params (Array<String>)

    Array of params to be used as values in the path

Returns:

  • (String)

    the URI path

Raises:

  • ArgumentError when the length of path_params doesn’t match the count of placeholders



97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/angus/remote/utils.rb', line 97

def self.build_path(path, path_params)
  matches = path.scan(RE_PATH_PARAM)
  if matches.length != path_params.length
    raise PathArgumentError.new(path_params.length, matches.length)
  end

  matches.each_with_index do |match, index|
    path = path.sub(match, path_params[index].to_s)
  end

  path
end

.build_request(method, path, request_params = {}, encode_as_json = false) ⇒ Object

Builds a request for the given method, path and params.

Parameters:

  • method (String)
  • path (String)
  • request_params (String) (defaults to: {})
  • encode_as_json (String) (defaults to: false)


29
30
31
32
33
34
35
# File 'lib/angus/remote/utils.rb', line 29

def self.build_request(method, path, request_params = {}, encode_as_json = false)
  if encode_as_json
    build_json_request(method, path, request_params)
  else
    build_normal_request(method, path, request_params)
  end
end

.severe_error_response?(response) ⇒ Boolean

Parameters:

  • response (#code)

Returns:

  • (Boolean)


111
112
113
114
# File 'lib/angus/remote/utils.rb', line 111

def self.severe_error_response?(response)
  status_code = response.code.to_s
  SEVERE_STATUS_CODES.include?(status_code)
end