lib/yandex_cleanweb.rb in yandex_cleanweb-0.0.4 vs lib/yandex_cleanweb.rb in yandex_cleanweb-0.0.5
- old
+ new
@@ -3,10 +3,12 @@
require "uri"
require "nokogiri"
require "net/http"
module YandexCleanweb
+ class NoApiKeyException < Exception; end
+
API_URL = 'http://cleanweb-api.yandex.ru/1.0/'
class << self
attr_accessor :api_key
@@ -52,11 +54,11 @@
private
def api_check_captcha(request_id, captcha_id, value)
check_captcha_url = "#{API_URL}/check-captcha"
params = {
- :key => api_key,
+ :key => prepare_api_key,
:id => request_id,
:captcha => captcha_id,
:value => value
}
@@ -66,20 +68,20 @@
Net::HTTP.get(uri)
end
def api_get_captcha(request_id)
get_captcha_url = "#{API_URL}/get-captcha"
- params = { :key => api_key, :id => request_id }
+ params = { :key => prepare_api_key, :id => request_id }
uri = URI.parse(get_captcha_url)
uri.query = URI.encode_www_form(params)
Net::HTTP.get(uri)
end
def api_check_spam(options)
- cleanweb_options = { :key => api_key }
+ cleanweb_options = { :key => prepare_api_key }
if options[0].is_a?(String) # quick check
cleanweb_options[:body_plain] = options[0]
else
options = options[0]
@@ -88,8 +90,14 @@
check_spam_url = "#{API_URL}/check-spam"
uri = URI.parse(check_spam_url)
response = Net::HTTP.post_form(uri, cleanweb_options)
response.body
+ end
+
+ def prepare_api_key
+ raise NoApiKeyException if api_key.nil? || api_key.empty?
+
+ api_key
end
end
end