lib/human_power.rb in human_power-0.0.6 vs lib/human_power.rb in human_power-0.1.0

- old
+ new

@@ -22,9 +22,22 @@ # Hash of registered user agents. def user_agents @user_agents ||= load_user_agents end + # Regular expression to match bot user agents. + def bot_regex + @bot_regex ||= begin + escaped_values = user_agents.values.map { |ua| Regexp.escape(ua) } + /#{escaped_values.join("|")}/i + end + end + + # Returns +true+ if a given user agent is a bot. + def is_bot?(user_agent) + !!(user_agent =~ bot_regex) + end + private # Loads the built-in user agents from crawlers.yml. def load_user_agents path = File.expand_path("../../user_agents.yml", __FILE__) \ No newline at end of file