module RestServiceClient::ClassMethods

Public Instance Methods

debug(flag) click to toggle source
# File lib/restserviceclient.rb, line 63
def debug(flag)
  define_method :get_debug, &-> { flag }
end
delete(method_name, path = '', options = {}) click to toggle source
# File lib/restserviceclient.rb, line 83
def delete(method_name, path = '', options = {})
  add_method :delete, method_name, path, options
end
get(method_name, path = '', options = {}) click to toggle source
# File lib/restserviceclient.rb, line 67
def get(method_name, path = '', options = {})
  add_method :get, method_name, path, options
end
headers(default_headers) click to toggle source
# File lib/restserviceclient.rb, line 55
def headers(default_headers)
  define_method :get_default_headers, &-> { default_headers }
end
host(url) click to toggle source
# File lib/restserviceclient.rb, line 47
def host(url)
  define_method :get_host, &-> { url }
end
params(default_params) click to toggle source
# File lib/restserviceclient.rb, line 59
def params(default_params)
  define_method :get_default_params, &-> { default_params }
end
patch(method_name, path = '', options = {}) click to toggle source
# File lib/restserviceclient.rb, line 79
def patch(method_name, path = '', options = {})
  add_method :patch, method_name, path, options
end
post(method_name, path = '', options = {}) click to toggle source
# File lib/restserviceclient.rb, line 71
def post(method_name, path = '', options = {})
  add_method :post, method_name, path, options
end
put(method_name, path = '', options = {}) click to toggle source
# File lib/restserviceclient.rb, line 75
def put(method_name, path = '', options = {})
  add_method :put, method_name, path, options
end
serializer(serializer_class) click to toggle source
# File lib/restserviceclient.rb, line 51
def serializer(serializer_class)
  define_method :get_serializer, &-> { serializer_class }
end

Private Instance Methods

add_method(http_method, method_name, path, options) click to toggle source

default_params, default_headers, default_payload, serializer

# File lib/restserviceclient.rb, line 90
def add_method(http_method, method_name, path, options)
  define_method method_name do |headers: {}, payload: {}, parameters: {}, **params|
    options_params = options.fetch(:params, {})
    options_headers = options.fetch(:headers, {})
    options_payload = options.fetch(:payload, {})
    serializer = options.fetch(:serializer, @serializer)

    params = @default_params.merge(options_params.merge(params.merge(parameters)))
    headers = @default_headers.merge(options_headers.merge(headers))
    payload = options_payload.merge payload

    uri = path.clone

    params.each_with_object(uri) do |(k, v), p|
      p.sub! ":#{k}", v.to_s
    end

    endpoint = @host + uri

    if @debug
      puts
      puts ' ______'
      puts '|'
      puts "|  #{self.class.name} is processing #{http_method.upcase} request to #{endpoint}"
      puts "|    Headers: #{headers}"
      puts "|    Payload: #{payload}"
    end

    begin
      result = make_request http_method, endpoint, payload, headers
      response = RestServiceClient::Response.new(
        result.code,
        result.headers,
        result.body,
        serializer.deserialize(result.body)
      )
    rescue RestClient::Exception => e
      response = RestServiceClient::ResponseWithError.new(
          e.http_code,
          e.http_headers,
          e.http_body,
          e.http_body,
          e.message
      )
    end

    if @debug
      puts '|'
      puts "|  #{self.class.name} is processing the response on #{http_method.upcase} request to #{endpoint}"
      puts "|    Status: #{response.status}"
      puts "|    Headers: #{response.headers}"
      puts "|    Result: #{response.result}"
      puts "|    Error message: #{response.message}" if response.is_a?(RestServiceClient::ResponseWithError)
      puts '|______'
      puts
    end

    response
  end
end