/
utils.thor
83 lines (73 loc) · 3.17 KB
/
utils.thor
1
2
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
54
55
56
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
# databasedotcom info:
# documentation: http://rubydoc.info/github/heroku/databasedotcom/master/frames
# github: https://github.com/heroku/databasedotcom
# thor info: https://github.com/wycats/thor
require 'databasedotcom'
class Utils < Thor
desc "query SOQL", "runs a soql query and displays the value of each record's 'name' field"
method_option :config_file, :type => :string, :default => "databasedotcom.yml",
:aliases => "-c", :desc => "The name of the file containing the connection parameters."
def query(soql)
client = authenticate(options[:config_file])
# execute the soql and iterate over the results to output the name
client.query("#{soql}").each do |r|
puts r.Name
end
end
desc "export SOQL FIELDS FILE", "runs a soql query and exports the specified
comma separated list of fields to a comma separated file"
method_option :config_file, :type => :string, :default => "databasedotcom.yml",
:aliases => "-c", :desc => "The name of the file containing the connection parameters."
def export(soql, fields, file)
client = authenticate(options[:config_file])
# query for records
records = client.query("#{soql}")
# open the file to write (probably local directory)
File.open(file, 'w') do |f|
# interate over the records
records.each do |r|
# create a single line with all field values specified
line = ''
fields.split(',').each do |field|
line += "#{eval("r.#{field}")},"
end
# write each line to the csv file
f.puts "#{line}\n"
end
end
end
desc "describe OBJECT", "displays the describe info for a particular object"
method_option :config_file, :type => :string, :default => "databasedotcom.yml",
:aliases => "-c", :desc => "The name of the file containing the connection parameters."
def describe(object)
client = authenticate(options[:config_file])
# call describe on the object by name
sobject = client.describe_sobject(object)
# output the results -- not very useful (frowny face)
puts sobject
end
desc "get_token", "retreives an access token"
method_option :config_file, :type => :string, :default => "databasedotcom.yml",
:aliases => "-c", :desc => "The name of the file containing the connection parameters."
def get_token
client = authenticate(options[:config_file]).oauth_token
puts "Access token: #{client}"
end
desc "show_config", "display the salesforce connection properties"
method_option :config_file, :type => :string, :default => "databasedotcom.yml",
:aliases => "-c", :desc => "The name of the file containing the connection parameters."
def show_config
config = YAML.load_file(options[:config_file])
puts config
end
private
def authenticate(file_name)
# load the configuration file with connection parameters
config = YAML.load_file(file_name)
# init the databasedotcom gem with the specified yml config file
client = Databasedotcom::Client.new(file_name)
# pass the credentials to authenticate
client.authenticate :username => config['username'], :password => config['password']
return client
end
end