class GnipApi::PowerTrack::Rule

  1. lib/gnip_api/power_track/rule.rb
Superclass: Object

Represents a PowerTrack rule with its necesary attribures.

Methods

Public Class

  1. new

Public Instance

  1. attributes
  2. consistent?
  3. id
  4. tag
  5. to_json
  6. uid
  7. value

Attributes

id [RW]
tag [RW]
value [RW]

Public Class methods

new (params={})
[show source]
# 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

Public Instance methods

attributes ()
[show source]
# 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)

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

[show source]
# 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_json ()
[show source]
# File lib/gnip_api/power_track/rule.rb, line 13
def to_json
  attributes.to_json
end
uid ()
[show source]
# File lib/gnip_api/power_track/rule.rb, line 25
def uid
  rule = @value
  rule += "tag:#{@tag}" if @tag
  Digest::SHA2.hexdigest(rule)
end