Class: RhetButler::Stasis::TransformQueue

Inherits:
Object
  • Object
show all
Defined in:
lib/rhet-butler/stasis/transform-queue.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeTransformQueue

Returns a new instance of TransformQueue.



32
33
34
35
# File 'lib/rhet-butler/stasis/transform-queue.rb', line 32

def initialize
  @list = []
  @hash = {}
end

Instance Attribute Details

#loaderObject

Returns the value of attribute loader.



36
37
38
# File 'lib/rhet-butler/stasis/transform-queue.rb', line 36

def loader
  @loader
end

#mappingObject

Returns the value of attribute mapping.



36
37
38
# File 'lib/rhet-butler/stasis/transform-queue.rb', line 36

def mapping
  @mapping
end

#writerObject

Returns the value of attribute writer.



36
37
38
# File 'lib/rhet-butler/stasis/transform-queue.rb', line 36

def writer
  @writer
end

Class Method Details

.lookup(type) ⇒ Object



23
24
25
26
27
28
29
# File 'lib/rhet-butler/stasis/transform-queue.rb', line 23

def lookup(type)
  type.sub!(/;.*/, '')
  type.chomp!
  transformers.fetch(type) do
    transformers.fetch(nil)
  end
end

.register(type, klass) ⇒ Object



19
20
21
# File 'lib/rhet-butler/stasis/transform-queue.rb', line 19

def register(type, klass)
  transformers[type] = klass
end

.transformersObject



15
16
17
# File 'lib/rhet-butler/stasis/transform-queue.rb', line 15

def transformers
  @transformers ||= {}
end

Instance Method Details

#add(document, source) ⇒ Object



38
39
40
41
42
43
# File 'lib/rhet-butler/stasis/transform-queue.rb', line 38

def add(document, source)
  store_to = mapping.storage_for(source)
  link_target = mapping.target_link_for(source)
  add_mapping(document, source, link_target, store_to)
  return link_target
end

#add_mapping(document, source_uri, target_uri, target_path) ⇒ Object



45
46
47
48
# File 'lib/rhet-butler/stasis/transform-queue.rb', line 45

def add_mapping(document, source_uri, target_uri, target_path)
  @hash[source_uri] = target_uri
  @list << TranformationOrder.new(document, source_uri, target_uri, target_path)
end

#canonicalize_uri(string) ⇒ Object



67
68
69
70
71
# File 'lib/rhet-butler/stasis/transform-queue.rb', line 67

def canonicalize_uri(string)
  uri = Addressable::URI.parse(string).normalize
  uri.fragment = nil
  uri
end

#goObject



85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/rhet-butler/stasis/transform-queue.rb', line 85

def go
  until @list.empty?
    order = @list.pop

    doc = load_document(order)
    transformer = transform_class(doc.type).new
    transformer.queue = self
    transformer.document = doc
    transformer.target_path = order.storage_target
    transformer.process
  end
end

#load_document(order) ⇒ Object



73
74
75
76
77
78
79
# File 'lib/rhet-butler/stasis/transform-queue.rb', line 73

def load_document(order)
  loader.load(canonicalize_uri(order.source_link))
rescue LoadFailed
  require 'pp'
  puts "Problem loading #{order.pretty_inspect}"
  raise
end

#target_for(document, link) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/rhet-butler/stasis/transform-queue.rb', line 54

def target_for(document, link)
  link = Addressable::URI.parse(link)
  case link.scheme
  when 'data'
    link
  else
    link = canonicalize_uri(mapping.absolute_uri(document.source_uri).join(link))
    @hash.fetch(link) do
      add(document, link)
    end
  end
end

#transform_class(type) ⇒ Object



50
51
52
# File 'lib/rhet-butler/stasis/transform-queue.rb', line 50

def transform_class(type)
  self.class.lookup(type)
end

#write_document(target_path, document_body) ⇒ Object



81
82
83
# File 'lib/rhet-butler/stasis/transform-queue.rb', line 81

def write_document(target_path, document_body)
  writer.write(target_path, document_body)
end