Module: GenericHelper

Defined in:
lib/sapis/generic_helper.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.camelize(string) ⇒ Object

Trivial version. Can easily be done with a regex.



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/sapis/generic_helper.rb', line 46

def self.camelize(string)
  buffer          = ""
  capitalize_next = true

  string.chars.each do |char|
    if char == '_'
      capitalize_next = true
    elsif capitalize_next
      buffer << char.upcase
      capitalize_next = false
    else
      buffer << char
    end
  end

  buffer
end

.do_retry(options = {}, &block) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/sapis/generic_helper.rb', line 24

def self.do_retry(options={}, &block)
  max_retries    = options[:max_retries] || 3
  sleep_interval = options[:sleep] || 0

  current_retries = 0

  begin
    yield
  rescue
    current_retries += 1

    if current_retries <= max_retries
      sleep sleep_interval
      retry
    end

    raise
  end
end

Instance Method Details

#decode_date(encoded_date, future: true) ⇒ Object

options:

:past: [false] the date can be future (supported only by some cases)

Format:

'YYYY-MM-DD', 'YYYYMMDD', 'MMM/DD/YYYY', 'MMM/D(D)', 'D(D)/MMM' (curr. year), 'MMDD' (curr. year)
'to[day]', 'ye[sterday]', 'mon' (monday), 'tue-' (last tuesday),
'+3' (today+3), '-2' (today-2)

Note that the easiest way to pass ‘-<value>’ in bash, is to use ‘–’ (end of options).



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/sapis/generic_helper.rb', line 76

def decode_date(encoded_date, future: true)
   case encoded_date.downcase
   # YYYY-MM-DD
   when %r{^(\d\d\d\d)-(\d\d)-(\d\d)$}
     Date.new($1.to_i, $2.to_i, $3.to_i)
   # YYYYMMDD
   when /^(\d\d\d\d)(\d\d)(\d\d)$/
     Date.new($1.to_i, $2.to_i, $3.to_i)
   # MMM/DD/YYYY
   when %r{^(\w{3})/(\d\d)/(\d\d\d\d)$}
     month_index_for_time = Date.strptime($1, "%b").month
     Date.new($3.to_i, month_index_for_time, $2.to_i)
   # MMM/D(D) (+future)
   when %r{^(\w{3})\/(\d{1,2})$}
     month_index_for_time = Date.strptime($1, "%b").month
     Date.new(Date.today.year, month_index_for_time, $2.to_i)
       .then { !future && it > Date.today ? it << 12 : it }
   # D(D)/MMM/YYYY
   when %r{^(\d{1,2})/(\w{3})/(\d\d\d\d)$}
     month_index_for_time = Date.strptime($2, "%b").month
     Date.new($3.to_i, month_index_for_time, $1.to_i)
   # D(D)/MMM
   when %r{^(\d{1,2})\/(\w{3})$}
     month_index_for_time = Date.strptime($2, "%b").month
     Date.new(Date.today.year, month_index_for_time, $1.to_i)
   # MMDD
   when /^(\d\d)(\d\d)$/
     Date.new(Time.now.year, $1.to_i, $2.to_i)
   when 'to', 'today'
     Date.today
   when 'ye', 'yesterday'
     Date.today - 1
   when /^(sun|mon|tue|wed|thu|fri|sat)$/
     diff = (Date.strptime($1, "%a") - Date.today).to_i
     diff += 7 if diff <= 0
     Date.today + diff
   when /^(sun|mon|tue|wed|thu|fri|sat)-$/
     diff = (Date.strptime($1, "%a") - Date.today).to_i
     diff -= 7 if diff >= 0
     Date.today + diff
   when /^\+(\d+)$/
     Date.today + $1.to_i
   when /^-(\d+)$/
     Date.today - $1.to_i
   else
     raise "Unrecognized date: #{encoded_date}"
   end
end

#decode_interval(*daytime) ⇒ Object

Params:

day, start, end

Format:

* DAY:   'YYYY-MM-DD', 'YYYYMMDD', 'MMDD', 'to[day]', 'ye[sterday]', 'thu' (thursday of the current week), 'mon-' (last monday), 'tue+' (next tuesday), 'MMM/DD'
* START: 'HH', 'HH:MM'
* END:   '3d', '2h', '45m', 'HH', 'HH:MM'

Possible combinations:

* (nothing)          all today
* DAY                on the day, for all day
* DAY,END            from day to end, for all day
* DAY,START,END      on the day, from start to end
* START,END          today, from start to end

Note that the ‘current week’ starts on Sunday.

In the regexes, numbered repetitions (‘n’) are not used for consistency.

The Date class pretty much sucks. Between the other things, it doesn’t accept string values when instantiating.



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
# File 'lib/sapis/generic_helper.rb', line 150

def decode_interval(*daytime)
  current_token = daytime.shift

  if current_token
    base_day = decode_date(current_token)

    if base_day.nil?
      base_day = Date.today
      daytime.unshift(current_token)
    end
  else
    base_day = Date.today
    daytime.unshift(current_token)
  end

  raise "Wrong start year format. Non-consumed tokens: #{daytime}" if base_day.nil?

  current_token = daytime.shift

  # The next token could be both a start (e.g. DAY,START,END) or an END (e.g. DAY,END).
  # Since we can't rely on character patterns because of the HH:MM case, we rely on the
  # number of tokens: START can be present only if at this point there are two tokens.
  #
  if daytime.size == 1
    case current_token
    when /^(\d{1,2})$/
      start_daytime = Time.local(base_day.year, base_day.month, base_day.day, $1)
    when /^(\d{1,2}):(\d\d)$/
      start_daytime = Time.local(base_day.year, base_day.month, base_day.day, $1, $2)
    else
      daytime.unshift(current_token)
    end

    all_day = false
  else
    start_daytime = Time.local(base_day.year, base_day.month, base_day.day)
    all_day = true
    daytime.unshift(current_token)
  end

  raise "Wrong start daytime format. Non-consumed tokens: #{daytime}" if start_daytime.nil?

  current_token = daytime.shift

  if current_token
    if all_day
      # Only full days allowed in case of all-day timespan
      #
      if current_token =~ /^(\d+)d$/
        end_daytime = add_days(start_daytime, $1.to_i)
      else
        daytime.unshift(current_token)
      end
    else
      case current_token.downcase
      when /^(\d+)d$/
        end_daytime = start_daytime + $1.to_i * 24 * 60 * 60
      when /^(\d+)h$/
        end_daytime = start_daytime + $1.to_i * 60 * 60
      when /^(\d+)m$/
        end_daytime = start_daytime + $1.to_i * 60
      when /^(\d{1,2})$/
        end_daytime = Time.local(start_daytime.year, start_daytime.month, start_daytime.day, $1)
      when /^(\d{1,2}):(\d\d)$/
        end_daytime = Time.local(start_daytime.year, start_daytime.month, start_daytime.day, $1, $2)
      else
        daytime.unshift(current_token)
      end
    end
  else
    # We're here if none of START/END have been passed; we default to +1 day.
    # At this point, there are no tokens to unshift
    #
    end_daytime = add_days(start_daytime, 1)
  end

  raise "Wrong end daytime format. Non-consumed tokens: #{daytime}" if end_daytime.nil?

  raise "Non-consumed tokens found: #{daytime}" if daytime.size > 0

  [start_daytime, end_daytime, all_day]
end