Class: GnipApi::PowerTrack::Rule

Inherits:
Object
  • Object
show all
Defined in:
lib/gnip_api/power_track/rule.rb

Overview

Represents a PowerTrack rule with its necesary attribures.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ Rule

Returns a new instance of Rule.



7
8
9
10
11
# File 'lib/gnip_api/power_track/rule.rb', line 7

def initialize params={}
  @value = params[:value] || params['value']
  @tag = params[:tag] || params['tag']
  @id = params[:id] || params['id']
end

Instance Attribute Details

#idObject

Returns the value of attribute id.



5
6
7
# File 'lib/gnip_api/power_track/rule.rb', line 5

def id
  @id
end

#tagObject

Returns the value of attribute tag.



5
6
7
# File 'lib/gnip_api/power_track/rule.rb', line 5

def tag
  @tag
end

#valueObject

Returns the value of attribute value.



5
6
7
# File 'lib/gnip_api/power_track/rule.rb', line 5

def value
  @value
end

Instance Method Details

#attributesObject



17
18
19
20
21
22
23
# File 'lib/gnip_api/power_track/rule.rb', line 17

def attributes
  attrs = {}
  attrs[:value] = @value if @value
  attrs[:tag] = @tag if @tag
  attrs[:id] = @id if @id
  attrs
end

#consistent?(raise_error = false) ⇒ Boolean

Simply checks balance of quotes and parenthesis within the value. Quoted text is sort of escaped as long as it’s consistently quoted.

Returns:

  • (Boolean)


33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/gnip_api/power_track/rule.rb', line 33

def consistent? raise_error=false
  characters = value.chars
  parenthesis = 0
  in_double_quotes = false
  in_single_quotes = false
  char = characters.shift
  while char
    parenthesis += 1 if (char == '(')
    parenthesis -= 1 if (char == ')')

    if char == '"'
      in_double_quotes = true
      char = characters.shift
      while char != '"'
        char = characters.shift
        break if char.nil?
      end
      in_double_quotes = false if char == '"'
    end

    if char == "'"
      in_single_quotes = true
      char = characters.shift
      while char != "'"
        char = characters.shift
        break if char.nil?
      end
      in_single_quotes = false if char == "'"
    end

    char = characters.shift
  end

  if raise_error
    raise ArgumentError.new("Imbalanced parenthesis") if parenthesis != 0
    raise ArgumentError.new("Imbalanced single quotes") if in_single_quotes != false
    raise ArgumentError.new("Imbalanced double quotes") if in_double_quotes != false
    return nil
  end
  return parenthesis == 0 && in_single_quotes == false && in_double_quotes == false
end

#to_jsonObject



13
14
15
# File 'lib/gnip_api/power_track/rule.rb', line 13

def to_json
  attributes.to_json
end

#uidObject



25
26
27
28
29
# File 'lib/gnip_api/power_track/rule.rb', line 25

def uid
  rule = @value
  rule += "tag:#{@tag}" if @tag
  Digest::SHA2.hexdigest(rule)
end