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.



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

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.



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

def replacements
  @replacements
end

Class Method Details

.validate_agentObject

Validate Agent

> Validates Agent-specific properties

+ agent - the agent for validation



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

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



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
100
# File 'lib/i2x/helper.rb', line 58

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



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

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



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

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

#datetimeObject



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

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



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

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

#hostnameObject



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

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



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

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

  results
end