Class: Validocno::Validator
- Inherits:
-
Object
- Object
- Validocno::Validator
- Defined in:
- lib/validocno/validator.rb
Constant Summary collapse
- CONTROL_URI =
"https://www2.politsei.ee/qr/".freeze
Instance Method Summary collapse
- #check_response(response) ⇒ Object
-
#initialize(doc_number) ⇒ Validator
constructor
A new instance of Validator.
- #response_string ⇒ Object
- #response_strings ⇒ Object
- #validate ⇒ Object
Constructor Details
#initialize(doc_number) ⇒ Validator
Returns a new instance of Validator.
6 7 8 |
# File 'lib/validocno/validator.rb', line 6 def initialize(doc_number) @doc_number = doc_number end |
Instance Method Details
#check_response(response) ⇒ Object
43 44 45 46 |
# File 'lib/validocno/validator.rb', line 43 def check_response(response) return true if response.code == 200 fail StandardError, "#{response.code}: #{response.message}" end |
#response_string ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/validocno/validator.rb', line 10 def response_string response = RestClient.get(CONTROL_URI, :params => { :qr => @doc_number }) check_response(response) document = Nokogiri::HTML(response) node = document.at_css(".qrWidgetDocumentCheckResult") if node.nil? || node.children.empty? fail StandardError, 'Cannot parse document status string.' else node.children.at_css("div").text.strip end end |
#response_strings ⇒ Object
35 36 37 38 39 40 41 |
# File 'lib/validocno/validator.rb', line 35 def response_strings { :valid => "Dokument #{@doc_number} on kehtiv.", :expired => "Dokument #{@doc_number} on kehtetu.", :invalid => "Dokumenti #{@doc_number} ei ole välja antud." } end |
#validate ⇒ Object
25 26 27 28 29 30 31 32 33 |
# File 'lib/validocno/validator.rb', line 25 def validate response_string_result = response_string is_valid = response_string_result == response_strings[:valid] { :valid => is_valid, :message => response_string_result } end |