Guest User

Untitled

a guest
Oct 6th, 2018
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.98 KB | None | 0 0
  1. require 'databasedotcom'
  2.  
  3. class Utils < Thor
  4.  
  5. desc "query SOQL", "runs a soql query and displays the value of each record's 'name' field"
  6. method_option :config_file, :type => :string, :default => "databasedotcom.yml",
  7. :aliases => "-c", :desc => "The name of the file containing the connection parameters."
  8. def query(soql)
  9. client = authenticate(options[:config_file])
  10. # execute the soql and iterate over the results to output the name
  11. client.query("#{soql}").each do |r|
  12. puts r.Name
  13. end
  14. end
  15.  
  16. desc "export SOQL FIELDS FILE", "runs a soql query and exports the specified
  17. comma separated list of fields to a comma separated file"
  18. method_option :config_file, :type => :string, :default => "databasedotcom.yml",
  19. :aliases => "-c", :desc => "The name of the file containing the connection parameters."
  20. def export(soql, fields, file)
  21. client = authenticate(options[:config_file])
  22. # query for records
  23. records = client.query("#{soql}")
  24. # open the file to write (probably local directory)
  25. File.open(file, 'w') do |f|
  26. # interate over the records
  27. records.each do |r|
  28. # create a single line with all field values specified
  29. line = ''
  30. fields.split(',').each do |field|
  31. line += "#{eval("r.#{field}")},"
  32. end
  33. # write each line to the csv file
  34. f.puts "#{line}\n"
  35. end
  36. end
  37. end
  38.  
  39. desc "describe OBJECT", "displays the describe info for a particular object"
  40. method_option :config_file, :type => :string, :default => "databasedotcom.yml",
  41. :aliases => "-c", :desc => "The name of the file containing the connection parameters."
  42. def describe(object)
  43. client = authenticate(options[:config_file])
  44. # call describe on the object by name
  45. sobject = client.describe_sobject(object)
  46. # output the results -- not very useful (frowny face)
  47. puts sobject
  48. end
  49.  
  50. desc "get_token", "retreives an access token"
  51. method_option :config_file, :type => :string, :default => "databasedotcom.yml",
  52. :aliases => "-c", :desc => "The name of the file containing the connection parameters."
  53. def get_token
  54. client = authenticate(options[:config_file]).oauth_token
  55. puts "Access token: #{client}"
  56. end
  57.  
  58. desc "show_config", "display the salesforce connection properties"
  59. method_option :config_file, :type => :string, :default => "databasedotcom.yml",
  60. :aliases => "-c", :desc => "The name of the file containing the connection parameters."
  61. def show_config
  62. config = YAML.load_file(options[:config_file])
  63. puts config
  64. end
  65.  
  66. private
  67.  
  68. def authenticate(file_name)
  69. # load the configuration file with connection parameters
  70. config = YAML.load_file(file_name)
  71. # init the databasedotcom gem with the specified yml config file
  72. client = Databasedotcom::Client.new(file_name)
  73. # pass the credentials to authenticate
  74. client.authenticate :username => config['username'], :password => config['password']
  75. return client
  76. end
  77.  
  78. end
Add Comment
Please, Sign In to add comment