Class: EPC::Command::GetUserPropertyCommand

Inherits:
BaseCommand show all
Defined in:
lib/epc/command/userproperty/get_user_property_command.rb

Constant Summary

Constants inherited from BaseCommand

BaseCommand::GIVEUP_TICKS, BaseCommand::SLEEP_TIME, BaseCommand::TICKER_TICKS

Instance Attribute Summary

Attributes inherited from BaseCommand

#client, #klass_name, #object_id, #object_type, #options, #target_id, #target_type

Instance Method Summary collapse

Methods inherited from BaseCommand

#check_options, #context_params, #context_params=, #go, include_module, inherited, #initialize, required_options, #say_err

Methods included from PersistentAttributes

#auth_token, #caller_id, #target_url

Constructor Details

This class inherits a constructor from EPC::Command::BaseCommand

Instance Method Details

#execute(*args) ⇒ Object



3
4
5
6
7
8
9
10
11
12
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
46
47
48
49
50
51
52
53
# File 'lib/epc/command/userproperty/get_user_property_command.rb', line 3

def execute(*args)
  identifier = args[1] || (args[0].split(":")[1] rescue nil)

  if @options[:file] && File.exists?(@options[:file])
    proceed = ask_yn("File #{@options[:file]} already exists. Overwrite? [Yn]: ")
    if proceed.upcase == 'N'
      return 1
    end
  end

  if identifier.blank? && !@options[:all]
    say_err("User property identifier is missing.")
    return 1
  elsif @options[:all]
    retrieve_user_id
    path = EPC::Config::ENTITY_PROPERTIES_PATH + "/for/User/#{@user_id}"
  elsif identifier.to_i.to_s == identifier
    path = EPC::Config::ENTITY_PROPERTIES_PATH + "/#{identifier}"
  else
    retrieve_user_id
    identifier = retrieve_identifier_for("EntityProperty", "#{identifier}:#{@user_id}:User")
    path = EPC::Config::ENTITY_PROPERTIES_PATH + "/#{identifier}"
  end
  
  status, response, headers = client.get(path)
  
  if status.failure?
    say("Request failed with [#{response[:message]}]")
  else
    response = [response] unless response.is_a?(Array)
    if @options[:file]
      begin
        File.open(@options[:file], "w+") do |f|
          response.each do |r|
            f << r.delete_if{|k,v| ![:id, :name, :value].include?(k)}.to_json
            f << "\n"
          end
        end
        say("User properties were saved to #{@options[:file]}")
      rescue Exception => ex
        say_err("User properties could not be saved (#{ex.to_s})")
        return 1
      end
    else
      properties_table = EPC::TabularOutputter.new(response, [:id, :name, :value])
      say(properties_table.print)
    end
  end
  
  return status
end