Module: TaliaCore::ActiveSourceParts::Xml::GenericReaderHelpers

Included in:
GenericReader
Defined in:
lib/talia_core/active_source_parts/xml/generic_reader_helpers.rb

Overview

Helper methods that can be used during the import operation

Instance Method Summary collapse

Instance Method Details

#current_is_a?(type) ⇒ Boolean

Returns true if the currently imported element already contains type information AND is of the given type.

Returns:

  • (Boolean)


16
17
18
19
# File 'lib/talia_core/active_source_parts/xml/generic_reader_helpers.rb', line 16

def current_is_a?(type)
  assit_kind_of(Class, type)
  @current.attributes['type'] && ("TaliaCore::#{@current.attributes['type']}".constantize <= type)
end

#get_absolute_file_url(url) ⇒ Object

Gets an absolute path to the given file url, using the base_file_url



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/talia_core/active_source_parts/xml/generic_reader_helpers.rb', line 37

def get_absolute_file_url(url)
  orig_url = url.to_s.strip
  
  url = file_url(orig_url)
  # If a file:// was stripped from the url, this means it will always point
  # to a file
  force_file = (orig_url != url)
  # Indicates wether the base url is a network url or a file/directory
  base_is_net = !base_file_url.is_a?(String)
  # Try to find if we have a "net" URL if we aren't sure if this is a file. In
  # case the base url is a network url, we'll always assume that the
  # url is also a net thing. Otherwise we only have a net url if it contains a
  # '://' string
  is_net_url = !force_file && (base_is_net || url.include?('://'))
  # The url is absolute if there is a : character to be found
  
  
  if(is_net_url)
    base_is_net ? join_url(base_file_url, url) : url
  else
    base_is_net ? url : join_files(base_file_url, url)
  end
end

#join_files(base_dir, path) ⇒ Object

Joins the two files. If the path is an absolute path, the base_dir is ignored



63
64
65
66
67
68
69
# File 'lib/talia_core/active_source_parts/xml/generic_reader_helpers.rb', line 63

def join_files(base_dir, path)
  if(Pathname.new(path).relative?)
    File.join(base_dir, path)
  else
    path
  end
end

#join_url(base_url, path) ⇒ Object

Joins the two url parts. If the path is an absolute URL, the base_url is ignored.



73
74
75
76
77
78
79
80
81
82
# File 'lib/talia_core/active_source_parts/xml/generic_reader_helpers.rb', line 73

def join_url(base_url, path)
  return path if(path.include?(':')) # Absolute URL contains ':'
  if(path[0..0] == '/')
    new_url = base_url.clone
    new_url.path = path
    new_url.to_s
  else
    (base_file_url + path).to_s
  end
end

#parse_date(date, fmt = nil) ⇒ Object

Parses the given string and returns it as a date object



29
30
31
32
33
34
# File 'lib/talia_core/active_source_parts/xml/generic_reader_helpers.rb', line 29

def parse_date(date, fmt = nil)
  return nil if(date.blank?)
  return DateTime.strptime(date, fmt) if(fmt) # format given
  return DateTime.new(date.to_i) if(date.size < 5) # this short should be a year
  DateTime.parse(date)
end

#source_exists?(uri) ⇒ Boolean

Returns true if the given source was already imported. This can return false if you call this for the currently importing source.

Returns:

  • (Boolean)


10
11
12
# File 'lib/talia_core/active_source_parts/xml/generic_reader_helpers.rb', line 10

def source_exists?(uri)
  !@sources[uri].blank?
end

#to_iso8601(date) ⇒ Object

Get the iso8601 string for the date



22
23
24
25
26
# File 'lib/talia_core/active_source_parts/xml/generic_reader_helpers.rb', line 22

def to_iso8601(date)
  return nil unless(date)
  date = DateTime.parse(date) unless(date.respond_to?(:strftime))
  date.strftime('%Y-%m-%dT%H:%M:%SZ')
end