Class: AMEE::Object

Inherits:
Object
  • Object
show all
Extended by:
ParseHelper
Includes:
ParseHelper
Defined in:
lib/amee/object.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ParseHelper

load_xml_doc, node_value, xmlpathpreamble

Constructor Details

#initialize(data = nil) ⇒ Object

Returns a new instance of Object.



8
9
10
11
12
13
14
15
# File 'lib/amee/object.rb', line 8

def initialize(data = nil)
  @uid = data ? data[:uid] : nil
  @created = data ? data[:created] : Time.now
  @modified = data ? data[:modified] : @created
  @path = data ? data[:path] : nil
  @name = data ? data[:name] : nil
  @connection = data ? data[:connection] : nil
end

Instance Attribute Details

#connectionObject

Returns the value of attribute connection.



17
18
19
# File 'lib/amee/object.rb', line 17

def connection
  @connection
end

#createdObject (readonly)

Returns the value of attribute created.



19
20
21
# File 'lib/amee/object.rb', line 19

def created
  @created
end

#modifiedObject (readonly)

Returns the value of attribute modified.



20
21
22
# File 'lib/amee/object.rb', line 20

def modified
  @modified
end

#nameObject (readonly)

Returns the value of attribute name.



22
23
24
# File 'lib/amee/object.rb', line 22

def name
  @name
end

#pathObject (readonly)

Returns the value of attribute path.



21
22
23
# File 'lib/amee/object.rb', line 21

def path
  @path
end

#uidObject (readonly)

Returns the value of attribute uid.



18
19
20
# File 'lib/amee/object.rb', line 18

def uid
  @uid
end

Class Method Details

.get_and_parse(connection, path, options) ⇒ Object

A nice shared get/parse handler that handles retry on parse errors



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/amee/object.rb', line 29

def self.get_and_parse(connection, path, options)
  # Note that we don't check the number of times retry has been done lower down
  # and count separately instead.
  # Worst case could be retries squared given the right pattern of failure, but
  # that seems unlikely. Would need, for instance, repeated 503 503 200->parse_fail
  retries = [1] * connection.retries
  begin
    # Load data from path
    response = connection.get(path, options).body
    # Parse data from response
    if response.is_json?
      from_json(response)
    else
      from_xml(response)
    end
  rescue JSON::ParserError, Nokogiri::XML::SyntaxError, REXML::ParseException => e
    # Invalid JSON or XML received, try the GET again in case it got cut off mid-stream
    connection.expire(path)
    if delay = retries.shift
      sleep delay
      retry
    else
      raise
    end
  rescue AMEE::BadData
    connection.expire(path)
    raise
  end
end

Instance Method Details

#expire_cacheObject



24
25
26
# File 'lib/amee/object.rb', line 24

def expire_cache
  @connection.expire_matching(full_path+'.*')
end