Class: Ralesforce::Client
- Inherits:
-
Object
- Object
- Ralesforce::Client
- Defined in:
- lib/ralesforce/client.rb
Constant Summary collapse
- @@host =
"http://login.salesforce.com"
- @@partner_path =
"/services/Soap/u/"
- @@oauth_path =
"/services/oauth2/token"
- @@rest_path =
"/services/data/"
- @@version =
"22.0"
- @@filename =
File.("~/.ralesforce/.logininfo")
Instance Method Summary collapse
- #access_token_valid? ⇒ Boolean
- #get_username_and_password ⇒ Object
-
#initialize ⇒ Client
constructor
A new instance of Client.
- #login ⇒ Object
- #logout ⇒ Object
- #query(query = "", outfile) ⇒ Object
- #read_login_info ⇒ Object
- #set_env(params) ⇒ Object
- #store_login_info ⇒ Object
Constructor Details
#initialize ⇒ Client
Returns a new instance of Client.
19 20 21 22 |
# File 'lib/ralesforce/client.rb', line 19 def initialize # read the login info and store in class or instance vars read_login_info end |
Instance Method Details
#access_token_valid? ⇒ Boolean
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/ralesforce/client.rb', line 115 def access_token_valid? if !@@access_token return false end begin url = "#{@@host}#{@@rest_path}v#{@@version}" puts "Checking access token with request to #{url}" response = RestClient.get url, :Authorization => "OAuth #{@@access_token}", "X-PrettyPrint" => true, :accept => :json #puts response puts "Current access token is valid." return true rescue => e puts e puts "Current access token is invalid." return false end end |
#get_username_and_password ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/ralesforce/client.rb', line 46 def get_username_and_password print "Enter username: " STDOUT.flush username = STDIN.gets print "Enter password: " STDOUT.flush system "stty -echo" password = STDIN.gets system "stty echo" username.strip! password.strip! return username, password end |
#login ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/ralesforce/client.rb', line 24 def login #Savon::configure do |config| #config.log = false #end username, password = get_username_and_password puts "Logging in" client = Savon::Client.new do wsdl.document = File.("../partner.wsdl", __FILE__) wsdl.endpoint = "#{@@host}#{@@partner_path}#{@@version}" end response = client.request :login do soap.body = { :username => username, :password => password } end result = response.to_hash[:login_response][:result] @@access_token = result[:session_id] server_url = result[:server_url] puts "Successfully logged in as #{username}" uri = URI.parse(server_url) @@host = "#{uri.scheme}://#{uri.host}:#{uri.port}" store_login_info end |
#logout ⇒ Object
133 134 135 136 137 138 139 140 |
# File 'lib/ralesforce/client.rb', line 133 def logout if File.exists?(@@filename) File.delete(@@filename) puts "Logged out." else puts "Not currently logged in." end end |
#query(query = "", outfile) ⇒ Object
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/ralesforce/client.rb', line 89 def query(query="", outfile) if query.size == 0 raise "Query must be specified" end if !access_token_valid? login end begin url = "#{@@host}#{@@rest_path}v#{@@version}/query" response = RestClient.get url, :params => {:q => query}, :Authorization => "OAuth #{@@access_token}", :accept => :json, "X-PrettyPrint" => 1 if outfile File.open(outfile, "w") { |f| f.write(response) } puts "Wrote query response to #{outfile}" else puts "Query response:" puts response end rescue => e puts e puts e.response raise "An error occurred while querying" end end |
#read_login_info ⇒ Object
74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/ralesforce/client.rb', line 74 def read_login_info if !File.exists?(File.dirname(@@filename)) Dir.mkdir(File.dirname(@@filename)) end if File.exists?(@@filename) s = IO.read(@@filename) login_info = JSON s @@access_token = login_info["access_token"] @@host = login_info["host"] ? login_info["host"] : @@host @@version = login_info["api_version"] ? login_info["api_version"] : @@version else puts "No login info exists." end end |
#set_env(params) ⇒ Object
60 61 62 63 64 65 66 |
# File 'lib/ralesforce/client.rb', line 60 def set_env(params) @@host = params["host"] ? params["host"] : @@host @@access_token = params["access_token"] ? params["access_token"] : @@access_token @@version = params["api_version"] ? params["api_version"] : @@version puts "the access token #{@@access_token}" store_login_info end |
#store_login_info ⇒ Object
68 69 70 71 72 |
# File 'lib/ralesforce/client.rb', line 68 def store_login_info puts "Storing login info to #{@@filename}..." logininfo = { "host" => @@host, "access_token" => @@access_token, "api_version" => @@version } File.open(@@filename, "w") { |f| f.write(logininfo.to_json) } end |