lib/yandex_cleanweb.rb in yandex_cleanweb-0.0.5 vs lib/yandex_cleanweb.rb in yandex_cleanweb-0.0.6

- old
+ new

@@ -4,12 +4,13 @@ require "nokogiri" require "net/http" module YandexCleanweb class NoApiKeyException < Exception; end + class BadResponseException < Exception; end - API_URL = 'http://cleanweb-api.yandex.ru/1.0/' + API_URL = 'http://cleanweb-api.yandex.ru/1.0' class << self attr_accessor :api_key def spam?(*options) @@ -17,21 +18,23 @@ doc = Nokogiri::XML(response) request_id_tag = doc.xpath('//check-spam-result/id') spam_flag_tag = doc.xpath('//check-spam-result/text') + raise BadResponseException if request_id_tag.size.zero? + request_id = request_id_tag[0].content spam_flag = spam_flag_tag[0].attributes["spam-flag"].content if spam_flag == 'yes' links = doc.xpath('//check-spam-result/links')[0].children links.map do |el| [el.attributes["url"], el.attributes["spam_flag"] == 'yes'] end - { :id => request_id, :links => links } + { id: request_id, links: links } else false end end @@ -40,11 +43,11 @@ doc = Nokogiri::XML(response) url = doc.xpath('//get-captcha-result/url').text captcha_id = doc.xpath('//get-captcha-result/captcha').text - { :url => url, :captcha => captcha_id } + { url: url, captcha: captcha_id } end def valid_captcha?(request_id, captcha_id, value) response = api_check_captcha(request_id, captcha_id, value) doc = Nokogiri::XML(response) @@ -54,33 +57,33 @@ private def api_check_captcha(request_id, captcha_id, value) check_captcha_url = "#{API_URL}/check-captcha" params = { - :key => prepare_api_key, - :id => request_id, - :captcha => captcha_id, - :value => value + key: prepare_api_key, + id: request_id, + captcha: captcha_id, + value: value } uri = URI.parse(check_captcha_url) uri.query = URI.encode_www_form(params) Net::HTTP.get(uri) end def api_get_captcha(request_id) get_captcha_url = "#{API_URL}/get-captcha" - params = { :key => prepare_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 => prepare_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]