Class: Twroute::Parser::Regex

Inherits:
Object
  • Object
show all
Defined in:
lib/twroute/parser/regex.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(regex_hash) ⇒ Regex

Returns a new instance of Regex.



6
7
8
# File 'lib/twroute/parser/regex.rb', line 6

def initialize(regex_hash)
  self.regex_hash = regex_hash  
end

Instance Attribute Details

#regex_hashObject

Returns the value of attribute regex_hash.



4
5
6
# File 'lib/twroute/parser/regex.rb', line 4

def regex_hash
  @regex_hash
end

Instance Method Details

#is_match?Boolean

Returns:

  • (Boolean)


15
16
17
18
19
# File 'lib/twroute/parser/regex.rb', line 15

def is_match?
  @parsed_hash.values.inject(true) do |accum, val|
    accum && !!val
  end
end

#parse_to_hash(tweet_text) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/twroute/parser/regex.rb', line 21

def parse_to_hash(tweet_text)
  regex_hash.inject({ }) do |accum, key_val|
    val = key_val.last
    case val
      when Regexp
        match_data = tweet_text.match(key_val.last)
      when Proc
        match_data = val.call(tweet_text)
      when Array
        res = tweet_text.match(val.first)
        match_data = res ? res[val.last] : res
    end
    accum[key_val.first] = (match_data ? process_data(match_data.to_s) : nil)
    accum
  end
end

#parse_tweet(tweet_text) ⇒ Object



10
11
12
13
# File 'lib/twroute/parser/regex.rb', line 10

def parse_tweet(tweet_text)
  @parsed_hash = parse_to_hash(tweet_text)
  is_match? ? @parsed_hash : false
end

#process_data(data) ⇒ Object

delayed job can’t unmarshal things that have lines that end in :



39
40
41
42
43
44
45
# File 'lib/twroute/parser/regex.rb', line 39

def process_data(data)
  if(data && data.is_a?(String))
    data.gsub(/:+$/m, '')
  else
    data
  end
end