module Inflect::Responsive

Allows services to respond an Inflect::Response instance.

Public Instance Methods

respond(content, options = {}) click to toggle source

Supply more expressiveness and flexibility to the interface by allowing multiple ways of responding. @example Only String

respond 'String Response'

@example As a String with options

respond 'String Response', opt: 'Extra options'

@example Or as a Hash with options

respond({content: 'Hashed Response'}, {opt: 'Extra options'})
# File lib/inflect/responsive.rb, line 23
def respond(content, options = {})
  [content, options]
end
serve(request) click to toggle source

Method that creates Response instance. @param words [Array<String>] The queried words. @return [Inflect::Response | nil] Returns nil if response is not valid.

# File lib/inflect/responsive.rb, line 9
def serve(request)
  content, options = handle(request)
  opts = merge_options(options, { query_words: request.query_words })
  validate_response(Inflect::Response.new(content, opts))
end

Private Instance Methods

default_options() click to toggle source

Set a Hash with required option parameters for Inflect::Response. @return [Hash]

# File lib/inflect/responsive.rb, line 41
def default_options
  {served_by: self.class}
end
merge_options(*options) click to toggle source
# File lib/inflect/responsive.rb, line 33
def merge_options(*options)
  options.compact.inject(default_options) do |combined_options, current_options|
    combined_options.merge current_options
  end
end
validate_response(response) click to toggle source
# File lib/inflect/responsive.rb, line 29
def validate_response(response)
  response.valid? ? response : nil
end