class Dyndnsd::Responder::RestStyle

Public Class Methods

new(app) click to toggle source

@param app [#call]

# File lib/dyndnsd/responder/rest_style.rb, line 7
def initialize(app)
  @app = app
end

Public Instance Methods

call(env) click to toggle source

@param env [Hash{String => String}] @return [Array{Integer,Hash{String => String},Array<String>}]

# File lib/dyndnsd/responder/rest_style.rb, line 13
def call(env)
  @app.call(env).tap do |status_code, headers, body|
    if headers.key?('X-DynDNS-Response')
      return decorate_dyndnsd_response(status_code, headers, body)
    else
      return decorate_other_response(status_code, headers, body)
    end
  end
end

Private Instance Methods

decorate_dyndnsd_response(status_code, headers, body) click to toggle source

@param status_code [Integer] @param headers [Hash{String => String}] @param body [Array<String>] @return [Array{Integer,Hash{String => String},Array<String>}]

# File lib/dyndnsd/responder/rest_style.rb, line 29
def decorate_dyndnsd_response(status_code, headers, body)
  case status_code
  when 200
    [200, {'Content-Type' => 'text/plain'}, [get_success_body(body[0], body[1])]]
  when 422
    error_response_map[headers['X-DynDNS-Response']]
  end
end
decorate_other_response(status_code, headers, _body) click to toggle source

@param status_code [Integer] @param headers [Hash{String => String}] @param _body [Array<String>] @return [Array{Integer,Hash{String => String},Array<String>}]

# File lib/dyndnsd/responder/rest_style.rb, line 42
def decorate_other_response(status_code, headers, _body)
  case status_code
  when 400
    [status_code, headers, ['Bad Request']]
  when 401
    [status_code, headers, ['Unauthorized']]
  end
end
error_response_map() click to toggle source

@return [Hash{String => Object}]

# File lib/dyndnsd/responder/rest_style.rb, line 59
def error_response_map
  {
    # general http errors
    'method_forbidden'   => [405, {'Content-Type' => 'text/plain'}, ['Method Not Allowed']],
    'not_found'          => [404, {'Content-Type' => 'text/plain'}, ['Not Found']],
    # specific errors
    'hostname_missing'   => [422, {'Content-Type' => 'text/plain'}, ['Hostname missing']],
    'hostname_malformed' => [422, {'Content-Type' => 'text/plain'}, ['Hostname malformed']],
    'host_forbidden'     => [403, {'Content-Type' => 'text/plain'}, ['Forbidden']]
  }
end
get_success_body(changes, myips) click to toggle source

@param changes [Array<Symbol>] @param myips [Array<String>] @return [String]

# File lib/dyndnsd/responder/rest_style.rb, line 54
def get_success_body(changes, myips)
  changes.map { |change| change == :good ? "Changed to #{myips.join(' ')}" : "No change needed for #{myips.join(' ')}" }.join("\n")
end