Class: SalesforceBulkAPINotifier::SalesforceService
- Inherits:
-
Object
- Object
- SalesforceBulkAPINotifier::SalesforceService
- Defined in:
- lib/salesforce_bulkapi_notifier/salesforce_service.rb
Constant Summary collapse
- API_VERSION =
47.0
Instance Attribute Summary collapse
-
#instance_url ⇒ Object
readonly
Returns the value of attribute instance_url.
Instance Method Summary collapse
- #annotate(job_info) ⇒ Object
-
#get_all_jobs ⇒ Object
ref.
-
#get_job_info(sf_job_id) ⇒ Object
ref.
-
#get_user_name(user_id) ⇒ Object
ref.
-
#initialize ⇒ SalesforceService
constructor
A new instance of SalesforceService.
- #rest_api_client ⇒ Object
- #screening_by_time(jobs, started_at) ⇒ Object
Constructor Details
#initialize ⇒ SalesforceService
Returns a new instance of SalesforceService.
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/salesforce_bulkapi_notifier/salesforce_service.rb', line 6 def initialize con = Faraday.new(url: "https://#{SalesforceBulkAPINotifier.salesforce_host}") do |c| c.adapter :httpclient c.request :json end # https://help.salesforce.com/articleView?id=remoteaccess_oauth_username_password_flow.htm&language=ja&type=0 response = con.post("/services/oauth2/token", { grant_type: :password, username: SalesforceBulkAPINotifier.salesforce_user_id, password: SalesforceBulkAPINotifier.salesforce_password, client_id: SalesforceBulkAPINotifier.salesforce_client_id, client_secret: SalesforceBulkAPINotifier.salesforce_client_secret, format: :json, }) data = JSON.parse(response.body) @access_token = data['access_token'] @instance_url = data['instance_url'] end |
Instance Attribute Details
#instance_url ⇒ Object (readonly)
Returns the value of attribute instance_url.
4 5 6 |
# File 'lib/salesforce_bulkapi_notifier/salesforce_service.rb', line 4 def instance_url @instance_url end |
Instance Method Details
#annotate(job_info) ⇒ Object
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/salesforce_bulkapi_notifier/salesforce_service.rb', line 78 def annotate(job_info) user_name = get_user_name(job_info['createdById']) error_rate = ((job_info['numberRecordsFailed'].to_f / job_info['numberRecordsProcessed'].to_f) * 100).floor(2) job_status = {success: true, message: "Success", error_rate: error_rate, user_name: user_name} if job_info['state'] == 'Failed' job_status[:success] = false job_status[:message] = "Job state is Failed" elsif error_rate >= SalesforceBulkAPINotifier.error_rate && job_info['state'] == "Closed" job_status[:success] = false job_status[:message] = "Error rate is #{error_rate}%. Error percentage is higher than #{SalesforceBulkAPINotifier.error_rate}%" else end job_status end |
#get_all_jobs ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/salesforce_bulkapi_notifier/salesforce_service.rb', line 41 def get_all_jobs res = rest_api_client.get("/services/data/v#{API_VERSION}/jobs/ingest") raise res if res.status != 200 data = JSON.parse(res.body) jobs = data['records'] done = data['done'] until done res = rest_api_client.get(data['nextRecordsUrl']) raise res if res.status != 200 data = JSON.parse(res.body) done = data['done'] jobs += data['records'] end jobs end |
#get_job_info(sf_job_id) ⇒ Object
59 60 61 62 63 |
# File 'lib/salesforce_bulkapi_notifier/salesforce_service.rb', line 59 def get_job_info(sf_job_id) res = rest_api_client.get("/services/data/v#{API_VERSION}/jobs/ingest/#{sf_job_id}") raise res if res.status != 200 JSON.parse(res.body) end |
#get_user_name(user_id) ⇒ Object
66 67 68 69 70 |
# File 'lib/salesforce_bulkapi_notifier/salesforce_service.rb', line 66 def get_user_name(user_id) res = rest_api_client.get("/services/data/v#{API_VERSION}/sobjects/User/#{user_id}") raise res if res.status != 200 JSON.parse(res.body)['Name'] end |
#rest_api_client ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/salesforce_bulkapi_notifier/salesforce_service.rb', line 28 def rest_api_client return nil unless @instance_url && @access_token @rest_api_client ||= Faraday.new(url: @instance_url) do |c| c.adapter :httpclient c.request :json c.headers['Authorization'] = "OAuth #{@access_token}" c.headers['Content-Type'] = 'application/json' c.headers['charset'] = 'UTF-8' end end |
#screening_by_time(jobs, started_at) ⇒ Object
72 73 74 75 76 |
# File 'lib/salesforce_bulkapi_notifier/salesforce_service.rb', line 72 def screening_by_time(jobs, started_at) jobs.map do |job| job if job['systemModstamp'].to_datetime >= started_at end.compact end |