Class: Abrio::Url

Inherits:
Object
  • Object
show all
Defined in:
lib/abrio/url.rb

Overview

Represents Abrio API response.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attrs = {}) ⇒ Url

Initialize all read-only atributes with Hash.



10
11
12
13
14
# File 'lib/abrio/url.rb', line 10

def initialize(attrs = {})
  attrs.each_pair do |attr, value|
    self.instance_variable_set("@#{attr}", value)
  end
end

Instance Attribute Details

#error_codeObject (readonly)

Returns the value of attribute error_code.



6
7
8
# File 'lib/abrio/url.rb', line 6

def error_code
  @error_code
end

#error_messageObject (readonly)

Returns the value of attribute error_message.



6
7
8
# File 'lib/abrio/url.rb', line 6

def error_message
  @error_message
end

#short_urlObject (readonly)

Returns the value of attribute short_url.



7
8
9
# File 'lib/abrio/url.rb', line 7

def short_url
  @short_url
end

#status_codeObject (readonly)

Returns the value of attribute status_code.



6
7
8
# File 'lib/abrio/url.rb', line 6

def status_code
  @status_code
end

Class Method Details

.parse(xml_body) ⇒ Object

Parses xml from Abrio API.

See documentation at abr.io/pages/api



19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/abrio/url.rb', line 19

def self.parse(xml_body)
  validates_xml(xml_body)

  doc = REXML::Document.new(xml_body)

  attributes = {:status_code => doc.elements["*/statusCode"].text,
                :error_message => doc.elements["*/errorMessage"].text,
                :error_code => doc.elements["*/errorCode"].text}

  if (result = doc.elements["*/results/nodeKeyVal"])
    attributes.merge!({:short_url => result.elements["shortUrl"].text})
  end
  self.new(attributes)
end

.validates_xml(xml) ⇒ Object

Raises:



34
35
36
37
# File 'lib/abrio/url.rb', line 34

def self.validates_xml(xml)
  raise Abrio::Error.new("XML body cannot be blank") if xml.nil? || xml.empty?
  raise Abrio::Error.new("Does not appear to be a valid Abrio XML.\n\n#{xml}") unless xml.start_with?("<abrio>")
end