Module: SalesforceHttpClient::SalesforceHttpAccess

Included in:
Client
Defined in:
lib/salesforce_http_client/salesforce_http_access.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#http_clientObject

Returns the value of attribute http_client.



3
4
5
# File 'lib/salesforce_http_client/salesforce_http_access.rb', line 3

def http_client
  @http_client
end

Instance Method Details

#create_http_clientObject



12
13
14
15
16
17
# File 'lib/salesforce_http_client/salesforce_http_access.rb', line 12

def create_http_client
  @http_client = HTTPClient.new
  @http_client.receive_timeout = @config.http_timeout
  @http_client.set_cookie_store(@config.cookie_store_file_path)
  @http_client
end

#download_and_save_report(report_id, output_save_path) ⇒ Object



34
35
36
37
38
39
40
41
42
# File 'lib/salesforce_http_client/salesforce_http_access.rb', line 34

def download_and_save_report(report_id, output_save_path)
  @logger.info "begin download reports"
  response = @http_client.get report_url(report_id)
  if response.status == 200
    save_report(response, output_save_path)
  else
    @logger.error "failed to download reports."
  end
end

#download_from_salesforce(output_save_path, report_id) ⇒ Object



5
6
7
8
9
10
# File 'lib/salesforce_http_client/salesforce_http_access.rb', line 5

def download_from_salesforce(output_save_path, report_id)
  create_http_client
  
  download_and_save_report(report_id, output_save_path)
  salesforce_logout
end

#respond_to_redirect(response) ⇒ Object



27
28
29
30
31
32
# File 'lib/salesforce_http_client/salesforce_http_access.rb', line 27

def respond_to_redirect(response)
  while response.status == 302 && response.headers['Location'] && !response.headers['Location'].empty?
    @logger.info "redirect to #{response.headers['Location']}"
    response = @http_client.get(response.headers['Location'])
  end
end

#salesforce_loginObject



19
20
21
22
23
24
25
# File 'lib/salesforce_http_client/salesforce_http_access.rb', line 19

def 
  @logger.info "try salesforce login..."
  @http_client.get config.
  body = { 'un' => @config., 'pw' => @config.salesforce_password }
  response = @http_client.post(config., body)
  respond_to_redirect(response)
end

#salesforce_logoutObject



53
54
55
56
57
# File 'lib/salesforce_http_client/salesforce_http_access.rb', line 53

def salesforce_logout
  @logger.info "try salesforce logout..."
  @http_client.get config.logout_url
  @logger.info "salesforce logged out."
end

#save_report(response, output_save_path) ⇒ Object



44
45
46
47
48
49
50
51
# File 'lib/salesforce_http_client/salesforce_http_access.rb', line 44

def save_report(response, output_save_path)
  report_content = response.content
  report_file = output_save_path
  @logger.info "save result to file #{report_file}"

  FileUtils.mkdir_p(File.dirname(report_file))
  File.write(report_file, report_content)
end