Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Refactor helpers into modules

  • Loading branch information...
commit 8fccaad7547f5c1689cca3e8894d869fbf1a4819 1 parent 99efa75
@mistydemeo authored
View
101 helpers.rb
@@ -1,96 +1,9 @@
-helpers do
- def json_requested?
- params["format"] == "json"
- end
-
- def validate_limit
- if not (params["limit"] && params["limit"] < 100 && params["limit"] > 0)
- params["limit"] = 10
- end
- end
-
- def input_errors mode
- if ![:found, :lost].include? mode
- raise ArgumentError, "Invalid mode - only :found and :lost are valid"
- end
-
- required_fields = ["name", "desc", "item"]
- if mode == :found
- required_fields.concat ["at"]
- elsif mode == :lost
- required_fields.concat ["seen"]
- end
-
- required_fields.select do |field|
- not params[field]
- end
- end
-
- def invalid_query_response
- if not json_requested?
- "You need to specify a search query with \"?q=\"!"
- else
- {"errors" => [{"message" => "Missing input(s)", "code" => 400}, "missing" => ["q"]]}
- end
- end
-
- def invalid_input_response_for errors
- if not json_requested?
- erb :error, :locals => {:errors => errors}
- else
- {"errors" => [{"message" => "Missing input(s)", "code" => 400, "missing" => errors}]}
- end
- end
+require 'helpers/parameters'
+require 'helpers/responses'
+require 'helpers/news'
- def search_response_for results
- if not json_requested?
- # This doesn't return 404 since that would raise an exception
- # for _why's client, which didn't have exception handling
- return "No results found :(" if results.empty?
-
- # _why's version didn't have a public "kind" value
- # or any kind of ID number
- result = results.first.to_hash
- result.delete_if do |k,_|
- [:kind, :public_id].include? k
- end
-
- # _why's guide specifies only one result, with unnamed fields
- # separated by "--\n"
- content_type :text
- result.values.join("\n--\n")
- else
- content_type :json
- status 404 if results.empty?
- results.map(&:to_hash).to_json
- end
- end
-
- def submission_confirmation item
- status 200
-
- if not json_requested?
- content_type :text
- "Thanks for submitting your #{params["item"]}!"
- else
- content_type :json
- item.to_hash.to_json
- end
- end
-
- # Formats each newspost in this format with a 1-space indent,
- # as seen in the Poignant Guide:
- # / dd month year /
- # text...
- # - salutation, poster
- def newspost_string post; <<-EOS
- / #{post.created_on.strftime("%d %B %Y")} /
-#{post.text.each_line.map {|l| l.prepend " "}.join}
-#{" " * 49} - #{post.salutation+", " if post.salutation}#{post.poster.name}
- EOS
- end
-
- def display_newsposts
- Post.limit(10).reverse.map {|p| newspost_string p}.join "\n"
- end
+helpers do
+ include ParameterHelpers
+ include Responses
+ include NewsHelpers
end
View
17 helpers/news.rb
@@ -0,0 +1,17 @@
+module NewsHelpers
+ # Formats each newspost in this format with a 1-space indent,
+ # as seen in the Poignant Guide:
+ # / dd month year /
+ # text...
+ # - salutation, poster
+ def newspost_string post; <<-EOS
+ / #{post.created_on.strftime("%d %B %Y")} /
+#{post.text.each_line.map {|l| l.prepend " "}.join}
+#{" " * 49} - #{post.salutation+", " if post.salutation}#{post.poster.name}
+ EOS
+ end
+
+ def display_newsposts
+ Post.limit(10).reverse.map {|p| newspost_string p}.join "\n"
+ end
+end
View
28 helpers/parameters.rb
@@ -0,0 +1,28 @@
+module ParameterHelpers
+ def json_requested?
+ params["format"] == "json"
+ end
+
+ def validate_limit
+ if not (params["limit"] && params["limit"] < 100 && params["limit"] > 0)
+ params["limit"] = 10
+ end
+ end
+
+ def input_errors mode
+ if ![:found, :lost].include? mode
+ raise ArgumentError, "Invalid mode - only :found and :lost are valid"
+ end
+
+ required_fields = ["name", "desc", "item"]
+ if mode == :found
+ required_fields.concat ["at"]
+ elsif mode == :lost
+ required_fields.concat ["seen"]
+ end
+
+ required_fields.select do |field|
+ not params[field]
+ end
+ end
+end
View
53 helpers/responses.rb
@@ -0,0 +1,53 @@
+module Responses
+ def invalid_query_response
+ if not json_requested?
+ "You need to specify a search query with \"?q=\"!"
+ else
+ {"errors" => [{"message" => "Missing input(s)", "code" => 400}, "missing" => ["q"]]}
+ end
+ end
+
+ def invalid_input_response_for errors
+ if not json_requested?
+ erb :error, :locals => {:errors => errors}
+ else
+ {"errors" => [{"message" => "Missing input(s)", "code" => 400, "missing" => errors}]}
+ end
+ end
+
+ def search_response_for results
+ if not json_requested?
+ # This doesn't return 404 since that would raise an exception
+ # for _why's client, which didn't have exception handling
+ return "No results found :(" if results.empty?
+
+ # _why's version didn't have a public "kind" value
+ # or any kind of ID number
+ result = results.first.to_hash
+ result.delete_if do |k,_|
+ [:kind, :public_id].include? k
+ end
+
+ # _why's guide specifies only one result, with unnamed fields
+ # separated by "--\n"
+ content_type :text
+ result.values.join("\n--\n")
+ else
+ content_type :json
+ status 404 if results.empty?
+ results.map(&:to_hash).to_json
+ end
+ end
+
+ def submission_confirmation item
+ status 200
+
+ if not json_requested?
+ content_type :text
+ "Thanks for submitting your #{params["item"]}!"
+ else
+ content_type :json
+ item.to_hash.to_json
+ end
+ end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.