Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class ColorsController < ApplicationController
- # Define colors catalog
- @@colors = [
- ['red', 'f00'],
- ['green', '0f0'],
- ['blue', '00f']
- ]
- # Define column names
- @@indexes = {
- 'name' => 0,
- 'hex' => 1
- }
- # Index method
- def index
- # Define result
- @result = {}
- # We "try" some code ...
- begin
- # Throw an error if parameter "what" or "which" is not provided
- [:what, :which].each{ |param| raise "Parameter '#{param}' not provided." if params[param].nil? }
- # Throw an error unless parameter "what" is found in indexes
- raise 'Parameter "what" not in catalog.' unless @@indexes.has_key? 'name'
- # Find the matching row in catalog via parameter "which"
- # Note: Binary flip used here. 1-x => 1-1=0 or 1-0=1
- row_found = @@colors.find{ |color| color[1 - @@indexes[params[:what]]] == params[:which] }
- # Throw an error if parameter "which" is not found in catalog
- raise 'Parameter "which" not in catalog.' if row_found.nil?
- # Format output
- @result[:message] = row_found[@@indexes[params[:what]]]
- @result[:status] = true
- # ... and catch an error if necessary (and format output)
- rescue StandardError => error
- @result[:message] = error
- @result[:status] = false
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement