Class: CloudRailSi::ServiceCode::AwaitCodeRedirect

Inherits:
Object
  • Object
show all
Defined in:
lib/cloudrail_si/servicecode/commands/AwaitCodeRedirect.rb

Instance Method Summary collapse

Instance Method Details

#execute(environment, parameters) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/cloudrail_si/servicecode/commands/AwaitCodeRedirect.rb', line 13

def execute(environment, parameters)
    Helper.assert(parameters.length >= 2 && Helper.is_var_address(parameters[0]) &&
                  (Helper.is_string(parameters[1]) || Helper.is_var_address(parameters[1])))

    res_var = parameters[0]
    url_str = Helper.resolve(environment, parameters[1])
    legacy = parameters.length == 2
    keys = []

    (2..parameters.length - 1).each do |i|
        keys.push(Helper.resolve(environment, parameters[i]))
    end
    keys.push('code') if (legacy)
    redirect_receiver = environment.instance_dependency_storage[:redirect_receiver] # TODO: use of symbols not consistent with most of rest of app
    if (!redirect_receiver || !redirect_receiver.respond_to?(:call))
        raise Errors::UserError.new("This service needs the RedirectReceiver to be implemented as a function. Have a look at our examples and documentation if you are unsure how to do that.")
    end
    redirect_url = redirect_receiver.call(url_str, environment.save_state_to_string())
    parts = redirect_url.split('?')
    param_str = (parts.length >= 2) ? parts[1] : parts[0]

    query_map = CGI::parse(param_str)
    res_map = {}
    keys.each do |key|
        if query_map[key].nil? || query_map[key].empty?
            raise Errors::UserError.new("The URL the RedirectReceiver returns does not contain all necessary keys in the query, it's missing at least #{key}")
        else
            res_map[key] = query_map[key][0] # Note we assume only one param here with a given name
        end
    end

    environment.set_variable(res_var, legacy ? res_map["code"] : res_map)
end

#get_identifierObject



9
10
11
# File 'lib/cloudrail_si/servicecode/commands/AwaitCodeRedirect.rb', line 9

def get_identifier
    'awaitCodeRedirect'
end