module Angus::Remote::Utils
Constants
- HTTP_METHODS_WITH_BODY
- RE_PATH_PARAM
- SEVERE_STATUS_CODES
Public Class Methods
build_base_request(method, path, multipart_request = false)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
Builds the URI path. It applies the params to the path
@param [String] path the path with place holders @param [Array<String>] path_params Array of params to be used as values in the path
@return [String] the URI path
@raise ArgumentError when the length of path_params doesn’t match the count of placeholders
@example
path = "/users/:user_id/profile/:profile_id" path_params = [4201, 2] build_path(path, path_params) #=> "/users/4201/profile/2"
# 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)
click to toggle source
Builds a request for the given method, path and params.
@param [String] method @param [String] path @param [String] request_params @param [String] encode_as_json
@return (see .build_base_request)
# 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)
click to toggle source
@param [#code] response
# 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