Class: I2X::Helper

Inherits:
Object
  • Object
show all
Defined in:
lib/i2x/helper.rb

Overview

Helper Class

> Miscellaneous helper methods and utils to deal with data.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeHelper

Returns a new instance of Helper.



12
13
14
15
# File 'lib/i2x/helper.rb', line 12

def initialize
  # load each helper function into a map for replacement in the delivery
  @replacements = [ ["%{i2x.date}", self.date], ["%{i2x.datetime}", self.datetime], ["%{i2x.hostname}", self.hostname]]
end

Instance Attribute Details

#replacementsObject

Returns the value of attribute replacements.



9
10
11
# File 'lib/i2x/helper.rb', line 9

def replacements
  @replacements
end

Class Method Details

.validate_agentObject

Validate Agent

> Validates Agent-specific properties

+ agent - the agent for validation



132
133
134
135
136
137
138
139
140
# File 'lib/i2x/helper.rb', line 132

def self.validate_agent
  begin
    valid = self.validate_seed(agent[:publisher], agent[:payload]) && self.validate_payload(agent[:publisher], agent[:payload])
  rescue Exception => e
    
  end

  valid
end

.validate_payload(publisher, payload) ⇒ Object

Validate payload

> Validates content payload.

+ publisher - for publisher-specific validations + payload - content for validation



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/i2x/helper.rb', line 57

def self.validate_payload publisher, payload
  @database_servers = ["mysql","sqlite","postgresql"]
  valid = true

  begin
    case publisher
    when 'csv', 'xml', 'json', 'file', 'js'
      # file content URI is mandatory
      if payload[:uri].nil? then
        valid = false
      end
    when 'sql'

      # check if database server is available
      unless database_servers.include? payload[:server] then
        valid = false
      end

      # database username is mandatory
      if payload[:username].nil? then
        valid = false
      end

      # database user password is mandatory
      if payload[:password].nil? then
        valid = false
      end

      # database name is mandatory
      if payload[:database].nil? then
        valid = false
      end

      # database query is mandatory
      if payload[:query].nil? then
        valid = false
      end
    end
  rescue Exception => e
    
  end
  valid
end

.validate_seed(publisher, seed) ⇒ Object

Validate Seed

> Validates Seed-specific properties

+ publisher - for publisher-specific validations + seed - the seed hash



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/i2x/helper.rb', line 108

def self.validate_seed publisher, seed
  begin
    valid = self.validate_payload publisher, seed
    if valid then
      # seed must have selectors
      if seed[:selectors].nil? then
        valid = false
      end
    else
      valid = false
    end
  rescue Exception => e
    
  end

  valid
end

Instance Method Details

#dateObject



26
27
28
# File 'lib/i2x/helper.rb', line 26

def date
  Time.now.strftime("%Y-%m-%d").to_s
end

#datetimeObject



22
23
24
# File 'lib/i2x/helper.rb', line 22

def datetime
  Time.now.to_s
end

#deep_copy(object) ⇒ Object

Copy Object/Hash/Array…

> Copies any object into new object (overcome references).

+ o - the object being copied



148
149
150
# File 'lib/i2x/helper.rb', line 148

def deep_copy object
  Marshal.load(Marshal.dump(object))
end

#hostnameObject



18
19
20
# File 'lib/i2x/helper.rb', line 18

def hostname
  ENV["APP_HOST"]
end

#identify_variables(text) ⇒ Object

Identify Variables

> Identifies variables on string set, generates array with all scanned variables for processing.

> Variables are enclosed in %variable string.

  • text - string to be scanned



37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/i2x/helper.rb', line 37

def identify_variables text
  begin
    results = Array.new
    text.scan(/%{(.*?)}/).each do |m|
      results.push m[0]
    end
  rescue Exception => e
    
  end

  results
end