Module: Hdo::StortingImporter::Util

Defined in:
lib/hdo/storting_importer/util.rb

Constant Summary collapse

ID_CONVERSIONS =
{
  '_AE' => 'Æ',
  '_O'  => 'Ø',
  '_A'  => 'Å'
}

Class Method Summary collapse

Class Method Details

.current_sessionObject



52
53
54
# File 'lib/hdo/storting_importer/util.rb', line 52

def current_session
  session_for_date Date.today
end

.json_pretty(obj) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/hdo/storting_importer/util.rb', line 66

def json_pretty(obj)
  case obj
  when Array
    obj.map! { |e| e.respond_to?(:to_hash) ? e.to_hash : e }
  when Hash
    # do nothing
  else
    obj = obj.to_hash if obj.respond_to?(:to_hash)
  end

  Yajl::Encoder.encode obj, :pretty => true, :indent => "  "
end

.period_to_date_range(str) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/hdo/storting_importer/util.rb', line 32

def period_to_date_range(str)
  start_year, end_year = str.split("-").map do |y|
    case y.size
    when 4
      Integer(y)
    when 2
      Integer("19#{y}")
    else
      "couldn't parse year #{str.inspect}"
    end
  end

  if start_year > end_year
    raise "invalid period: #{str.inspect}"
  end

  # approximate dates
  Date.new(start_year, 10, 1)..Date.new(end_year, 9, 30)
end

.remove_invalid_html(str) ⇒ Object



12
13
14
15
# File 'lib/hdo/storting_importer/util.rb', line 12

def remove_invalid_html(str)
  str.gsub("<\\p>", "")

end

.remove_newlines(str) ⇒ Object



8
9
10
# File 'lib/hdo/storting_importer/util.rb', line 8

def remove_newlines(str)
  str.gsub(/\r?\n/, '')
end

.session_for_date(date) ⇒ Object



56
57
58
59
60
61
62
63
64
# File 'lib/hdo/storting_importer/util.rb', line 56

def session_for_date(date)
  new_session_start = Date.new(date.year, 10, 1)

  if date >= new_session_start
    new_session_start..(Date.new(date.year + 1, 9, 30))
  else
    Date.new(date.year - 1, 10, 1)..Date.new(date.year, 9, 30)
  end
end

.unescape_param(query_param) ⇒ Object



23
24
25
26
27
28
29
30
# File 'lib/hdo/storting_importer/util.rb', line 23

def unescape_param(query_param)
  return unless query_param.kind_of?(String)

  q = query_param.dup
  ID_CONVERSIONS.each { |k, v| q.gsub!(k, v) }

  q
end