Class: CloudRailSi::ServiceCode::Helper

Inherits:
Object
  • Object
show all
Defined in:
lib/cloudrail_si/helpers/Helper.rb

Class Method Summary collapse

Class Method Details

.add_all(target, source) ⇒ Object



11
12
13
# File 'lib/cloudrail_si/helpers/Helper.rb', line 11

def add_all(target, source)
    target.concat(source)
end

.assert(expression) ⇒ Object



62
63
64
65
# File 'lib/cloudrail_si/helpers/Helper.rb', line 62

def assert(expression)
    debug if !expression
    raise Errors::InternalError.new("Assertion failed") if (!expression)
end

.clear(target) ⇒ Object



19
20
21
22
23
# File 'lib/cloudrail_si/helpers/Helper.rb', line 19

def clear(target)
	target.each do |key|
        delete target[key]
    end
end

.compare(a_obj, b_obj) ⇒ Object



74
75
76
77
78
79
# File 'lib/cloudrail_si/helpers/Helper.rb', line 74

def compare(a_obj, b_obj)
    result = (a_obj <=> b_obj)
    raise Errors::InternalError.new("Compare compares incomparable values") if result.nil?

    return result
end

.debugObject

Only debug if allowed by env



58
59
60
# File 'lib/cloudrail_si/helpers/Helper.rb', line 58

def debug
    byebug if (ENV['cloudrail_ruby_debug']==='true')
end

.dump_stream(stream, target_encoding = nil) ⇒ Object



80
81
82
83
84
# File 'lib/cloudrail_si/helpers/Helper.rb', line 80

def dump_stream(stream, target_encoding=nil)
    contents = stream.string
    contents = contents.force_encoding(target_encoding) unless target_encoding.nil?
    return contents
end

.is_array(object) ⇒ Object



135
136
137
# File 'lib/cloudrail_si/helpers/Helper.rb', line 135

def is_array(object)
	object.is_a?(Array)
end

.is_boolean(object) ⇒ Object



39
40
41
# File 'lib/cloudrail_si/helpers/Helper.rb', line 39

def is_boolean(object)
	(object == true) || (object == false)
end

.is_number(object) ⇒ Object



36
37
38
# File 'lib/cloudrail_si/helpers/Helper.rb', line 36

def is_number(object)
	object.is_a? Numeric
end

.is_number_string(obj) ⇒ Object



45
46
47
# File 'lib/cloudrail_si/helpers/Helper.rb', line 45

def is_number_string(obj)
    Float obj rescue nil  # http://stackoverflow.com/questions/1235863/test-if-a-string-is-basically-an-integer-in-quotes-using-ruby
end

.is_object(object) ⇒ Object



30
31
32
# File 'lib/cloudrail_si/helpers/Helper.rb', line 30

def is_object(object)
    (!is_string(object)) && (!is_sandbox(object)) && object.is_a?(Object) && !object.nil?
end

.is_sandbox(object) ⇒ Object



33
34
35
# File 'lib/cloudrail_si/helpers/Helper.rb', line 33

def is_sandbox(object)
    object.instance_variable_get(:@is_sandbox)
end

.is_stream(obj) ⇒ Object



48
49
50
# File 'lib/cloudrail_si/helpers/Helper.rb', line 48

def is_stream(obj)
    return obj.class.to_s == 'StringIO'
end

.is_string(object) ⇒ Object



27
28
29
# File 'lib/cloudrail_si/helpers/Helper.rb', line 27

def is_string(object)
	object.is_a? String
end

.is_var_address(object) ⇒ Object



42
43
44
# File 'lib/cloudrail_si/helpers/Helper.rb', line 42

def is_var_address(object)
    !object.instance_variable_get(:@address_string).nil?
end

.log(str) ⇒ Object

Only print if allowed by env



53
54
55
# File 'lib/cloudrail_si/helpers/Helper.rb', line 53

def log(str)
    print str if (ENV['cloudrail_ruby_print']==='true')
end

.lower_case_first_letter(str) ⇒ Object



129
130
131
# File 'lib/cloudrail_si/helpers/Helper.rb', line 129

def lower_case_first_letter(str)
    return str[0].downcase + str[1..-1]
end

.make_request(url_string, headers, body, method) ⇒ Object

TODO: add stream support TODO: add faraday support



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/cloudrail_si/helpers/Helper.rb', line 96

def make_request(url_string, headers, body, method)
    Helper.log "\nmake_request_headers #{headers}\n" if headers
    Helper.log "\nmake_request_url: #{url_string}\n"
    uri = URI.parse(url_string)
    use_ssl = uri.scheme == 'https'
    Net::HTTP.start(uri.host, uri.port, use_ssl: use_ssl, open_timeout: 30, read_timeout: 30) do |http|
        if (method.downcase == 'get')
            req = Net::HTTP::Get.new uri, headers
        elsif (method.downcase == 'post')
            req = Net::HTTP::Post.new uri, headers
        elsif (method.downcase == 'delete')
            req = Net::HTTP::Delete.new uri, headers
        elsif (method.downcase == 'put')
            req = Net::HTTP::Put.new uri, headers
        elsif (method.downcase == 'patch')
            req = Net::HTTP::Patch.new uri, headers
        else
            raise Errors::InternalError.new("Unexpected method for make_request: #{method}.")
        end

        req.body = body

        req.body = dump_stream(req.body) if is_stream(req.body)
        Helper.log "make_request_body: #{req.body}\n"

        response = http.request(req)

        return response
    end

    #TODO: support redirects
end

.put_all(target, source) ⇒ Object



14
15
16
17
18
# File 'lib/cloudrail_si/helpers/Helper.rb', line 14

def put_all(target, source)
	source.each do |key|
        target[key] = source[key]
    end
end

.remove(target, element) ⇒ Object



24
25
26
# File 'lib/cloudrail_si/helpers/Helper.rb', line 24

def remove(target, element)
    target.delete(element)
end

.resolve(environment, value, check_existence = true) ⇒ Object



66
67
68
69
70
71
72
73
# File 'lib/cloudrail_si/helpers/Helper.rb', line 66

def resolve(environment, value, check_existence=true)
    check_existence = true if (check_existence === 0)
    if is_var_address(value)
        return environment.get_variable(value, -1, !check_existence)
    else
        return value
    end
end

.streamify_string(str, source_encoding) ⇒ Object



85
86
87
88
89
90
91
92
# File 'lib/cloudrail_si/helpers/Helper.rb', line 85

def streamify_string(str, source_encoding)
    res_stream = StringIO.new
    res_stream.set_encoding(source_encoding) unless source_encoding.nil?
    res_stream.write(str)
    res_stream.write(nil)

    return res_stream
end

.to_snake_case(str) ⇒ Object



140
141
142
143
144
145
146
# File 'lib/cloudrail_si/helpers/Helper.rb', line 140

def to_snake_case(str)
    str.gsub(/::/, '/').
    gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
    gsub(/([a-z\d])([A-Z])/,'\1_\2').
    tr("-", "_").
    downcase
end

.upper_case_first_letter(str) ⇒ Object



132
133
134
# File 'lib/cloudrail_si/helpers/Helper.rb', line 132

def upper_case_first_letter(str)
    return str[0].upcase + str[1..-1]
end