restforce-bulk 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: dcbd2c076f55df624e52aced7166735e68d673c9
4
+ data.tar.gz: 4af5fbf48b92f2d82e73f3c8846fd879ade2533d
5
+ SHA512:
6
+ metadata.gz: 3169bdb0e9ae8ccaffc564da615bf71df8eb12e9da949a7a5224172f0b822b059ec43bcf6685ba8f10dcf938162658b6cf446df32ffd0926336fc415301cca7b
7
+ data.tar.gz: dcc31a1f28fa3cbf28cdc8c8950d38dbe7b42276073019586d2ad93920fc11d4980238d7a9847079996104b55fb418cb0a6ce11b9a3168552472814d3fa95b5e
@@ -0,0 +1,11 @@
1
+ .DS_Store
2
+ /.bundle/
3
+ /.yardoc
4
+ /.byebug_history
5
+ /Gemfile.lock
6
+ /_yardoc/
7
+ /coverage/
8
+ /doc/
9
+ /pkg/
10
+ /spec/reports/
11
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1 @@
1
+ restforce-bulk
@@ -0,0 +1 @@
1
+ 2.2.1
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.1
4
+ before_install: gem install bundler -v 1.10.5
@@ -0,0 +1,13 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
4
+
5
+ We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, ethnicity, age, or religion.
6
+
7
+ Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
8
+
9
+ Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
10
+
11
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
12
+
13
+ This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in restforce-bulk.gemspec
4
+ gemspec
5
+
6
+ gem 'byebug'
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Vicente Mundim
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,102 @@
1
+ # Restforce::Bulk
2
+
3
+ [![build status][1]][2]
4
+
5
+ [1]: https://travis-ci.org/dtmtec/restforce-bulk.svg
6
+ [2]: http://travis-ci.org/dtmtec/restforce-bulk
7
+
8
+
9
+ Client for Salesforce Bulk API.
10
+
11
+ ## Installation
12
+
13
+ Add this line to your application's Gemfile:
14
+
15
+ ```ruby
16
+ gem 'restforce-bulk'
17
+ ```
18
+
19
+ And then execute:
20
+
21
+ $ bundle
22
+
23
+ Or install it yourself as:
24
+
25
+ $ gem install restforce-bulk
26
+
27
+ ## Usage
28
+
29
+ ### Query:
30
+
31
+ # Creating a query job and adding a batch, using CSV type
32
+ job = Restforce::Bulk::Job.create(:query, 'Account', :csv)
33
+ batch = job.add_batch("select Id, Name from Account limit 10")
34
+
35
+ # wait for the batch to complete, then refresh data
36
+ batch.refresh
37
+ batch.completed? # => true
38
+
39
+ # query batches returns only one result
40
+ result = batch.results.first
41
+
42
+ # we can get the contents from the result
43
+ csv = result.content
44
+
45
+ # csv is a CSV::Table, now you can process it any way you want
46
+
47
+ ### CRUD operations
48
+
49
+ # Creating an upsert job, using XML type (default)
50
+ job = Restforce::Bulk::Job.create(:upsert, 'Account')
51
+
52
+ # Adding a batch
53
+ batch = job.add_batch([{ Name: "New Account" }, { Id: 'a0B29000000XGxf', Name: 'Old Account' }])
54
+
55
+ # wait for the batch to complete, then refresh data
56
+ batch.refresh
57
+ batch.completed? # => true
58
+
59
+ # get the results for each row
60
+ batch.results.each do |result|
61
+ puts result.id # Id of the result
62
+ puts result.success # row successfully processed
63
+ puts result.error # error for row
64
+ end
65
+
66
+ ### Binary Attachments
67
+
68
+ # Creating an upsert job, either :zip_xml or :zip_csv are accepted
69
+ job = Restforce::Bulk::Job.create(:insert, 'Attachment', :zip_xml)
70
+
71
+ # Adding a batch, as an array of file data, with the following keys:
72
+ # - full_filename: The full path to the file in your filesystem
73
+ # - filename: The name of the attachment (can have folders in it)
74
+ # - parent_id: The ID of the parent record for the attachment
75
+ batch = job.add_batch([{ full_filename: "<HOME_DIR>/Pictures/image.jpg", filename: "image.jpg", parent_id: "a0E29000000DzPy" }])
76
+
77
+ # wait for the batch to complete, then refresh data
78
+ batch.refresh
79
+ batch.completed? # => true
80
+
81
+ # get the results for each row
82
+ batch.results.each do |result|
83
+ puts result.id # Id of the result
84
+ puts result.success # row successfully processed
85
+ puts result.error # error for row
86
+ end
87
+
88
+ ## Development
89
+
90
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake rspec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
91
+
92
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
93
+
94
+ ## Contributing
95
+
96
+ Bug reports and pull requests are welcome on GitHub at https://github.com/dtmtec/restforce-bulk. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](contributor-covenant.org) code of conduct.
97
+
98
+
99
+ ## License
100
+
101
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
102
+
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "restforce/bulk"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,55 @@
1
+ require "csv"
2
+ require "multi_xml"
3
+ require "nokogiri"
4
+ require "faraday"
5
+ require "faraday_middleware"
6
+ require "faraday_middleware/response_middleware"
7
+ require "restforce"
8
+ require "restforce/bulk/version"
9
+ require "active_support/inflector"
10
+ require "zip"
11
+
12
+ module Restforce
13
+ module Bulk
14
+ autoload :Client, 'restforce/bulk/client'
15
+ autoload :Job, 'restforce/bulk/job'
16
+ autoload :Batch, 'restforce/bulk/batch'
17
+ autoload :Result, 'restforce/bulk/result'
18
+
19
+ autoload :Attributes, 'restforce/bulk/attributes'
20
+
21
+ autoload :Zipper, 'restforce/bulk/zipper'
22
+
23
+ module Builder
24
+ autoload :Xml, 'restforce/bulk/builder/xml'
25
+ autoload :Csv, 'restforce/bulk/builder/csv'
26
+ autoload :ZipXml, 'restforce/bulk/builder/zip_xml'
27
+ autoload :ZipCsv, 'restforce/bulk/builder/zip_csv'
28
+ end
29
+
30
+ module Parser
31
+ autoload :Xml, 'restforce/bulk/parser/xml'
32
+ autoload :Csv, 'restforce/bulk/parser/csv'
33
+ end
34
+
35
+ module Middleware
36
+ autoload :Authorization, 'restforce/bulk/middleware/authorization'
37
+ autoload :ParseCsv, 'restforce/bulk/middleware/parse_csv'
38
+ end
39
+
40
+ MIME_TYPE_MAPPING = {
41
+ csv: 'text/csv',
42
+ xml: 'application/xml',
43
+ zip_csv: 'zip/csv',
44
+ zip_xml: 'zip/xml'
45
+ }
46
+
47
+ def self.client
48
+ @client ||= Restforce::Bulk::Client.new
49
+ end
50
+
51
+ def self.client=(client)
52
+ @client = client
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,11 @@
1
+ module Restforce
2
+ module Bulk
3
+ module Attributes
4
+ def assign_attributes(value)
5
+ value.each do |attr, value|
6
+ send("#{attr.to_s.underscore}=", value) if respond_to?("#{attr.to_s.underscore}=")
7
+ end
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,72 @@
1
+ module Restforce
2
+ module Bulk
3
+ class Batch
4
+ include Restforce::Bulk::Attributes
5
+
6
+ class << self
7
+ def create(job_id, data, operation, content_type=:xml)
8
+ builder = builder_class_for(content_type).new(operation)
9
+
10
+ response = Restforce::Bulk.client.perform_request(:post, "job/#{job_id}/batch", builder.transform(data, operation, content_type), content_type)
11
+
12
+ new(response.body.batchInfo)
13
+ end
14
+
15
+ def find(job_id, id)
16
+ new(job_id: job_id, id: id).tap(&:refresh)
17
+ end
18
+
19
+ def builder_class_for(content_type)
20
+ Restforce::Bulk::Builder.const_get(content_type.to_s.camelize)
21
+ end
22
+ end
23
+
24
+ attr_accessor :id, :job_id, :state, :created_date, :system_modstamp, :number_records_processed
25
+
26
+ def initialize(attributes={})
27
+ assign_attributes(attributes)
28
+ end
29
+
30
+ def queued?
31
+ state == 'Queued'
32
+ end
33
+
34
+ def in_progress?
35
+ state == 'InProgress'
36
+ end
37
+
38
+ def completed?
39
+ state == 'Completed'
40
+ end
41
+
42
+ def failed?
43
+ state == 'Failed'
44
+ end
45
+
46
+ def not_processed?
47
+ state == 'Not Processed'
48
+ end
49
+
50
+ def refresh
51
+ response = Restforce::Bulk.client.perform_request(:get, "job/#{job_id}/batch/#{id}")
52
+
53
+ assign_attributes(response.body.batchInfo)
54
+ end
55
+
56
+ def results
57
+ response = Restforce::Bulk.client.perform_request(:get, "job/#{job_id}/batch/#{id}/result")
58
+ parser = results_parser_for(response.body).new
59
+
60
+ parser.results_on(response.body).map do |result|
61
+ Restforce::Bulk::Result.new({job_id: job_id, batch_id: id}.merge(result))
62
+ end
63
+ end
64
+
65
+ protected
66
+
67
+ def results_parser_for(body)
68
+ body.is_a?(CSV::Table) ? Restforce::Bulk::Parser::Csv : Restforce::Bulk::Parser::Xml
69
+ end
70
+ end
71
+ end
72
+ end
@@ -0,0 +1,31 @@
1
+ module Restforce
2
+ module Bulk
3
+ module Builder
4
+ class Csv
5
+ attr_accessor :operation
6
+
7
+ def initialize(operation)
8
+ self.operation = operation
9
+ end
10
+
11
+ def transform(data, operation, content_type)
12
+ operation == 'query' ? query(data) : generate(data)
13
+ end
14
+
15
+ def query(data)
16
+ data
17
+ end
18
+
19
+ def generate(data)
20
+ ::CSV.generate do |csv|
21
+ csv << data.first.keys
22
+
23
+ data.each do |attributes|
24
+ csv << attributes.values
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,61 @@
1
+ module Restforce
2
+ module Bulk
3
+ module Builder
4
+ class Xml
5
+ attr_accessor :operation
6
+
7
+ def initialize(operation)
8
+ self.operation = operation
9
+ end
10
+
11
+ def job(object_name, content_type)
12
+ build_xml(:jobInfo) do |xml|
13
+ xml.operation operation
14
+ xml.object object_name
15
+ xml.contentType content_type
16
+ end
17
+ end
18
+
19
+ def close
20
+ build_xml(:jobInfo) do |xml|
21
+ xml.state 'Closed'
22
+ end
23
+ end
24
+
25
+ def abort
26
+ build_xml(:jobInfo) do |xml|
27
+ xml.state 'Aborted'
28
+ end
29
+ end
30
+
31
+ def transform(data, operation, content_type)
32
+ operation == 'query' ? query(data) : generate(data)
33
+ end
34
+
35
+ def query(data)
36
+ data
37
+ end
38
+
39
+ def generate(data)
40
+ build_xml(:sObjects) do |xml|
41
+ data.each do |item|
42
+ xml.sObject do
43
+ item.each do |attr, value|
44
+ xml.send(attr, value)
45
+ end
46
+ end
47
+ end
48
+ end
49
+ end
50
+
51
+ protected
52
+
53
+ def build_xml(root, options={}, &block)
54
+ Nokogiri::XML::Builder.new { |xml|
55
+ xml.send(root, { xmlns: 'http://www.force.com/2009/06/asyncapi/dataload' }.merge(options), &block)
56
+ }.to_xml(encoding: 'UTF-8')
57
+ end
58
+ end
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,22 @@
1
+ module Restforce
2
+ module Bulk
3
+ module Builder
4
+ class ZipCsv < Csv
5
+ def transform(data, operation, content_type)
6
+ zipper = Restforce::Bulk::Zipper.new(data, content_type)
7
+ File.read(zipper.zip)
8
+ end
9
+
10
+ def create_request_txt(data)
11
+ ::CSV.generate do |csv|
12
+ csv << %w{Name ParentId Body}
13
+
14
+ data.each do |item|
15
+ csv << [item[:filename], item[:parent_id], "##{item[:filename]}"]
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,24 @@
1
+ module Restforce
2
+ module Bulk
3
+ module Builder
4
+ class ZipXml < Xml
5
+ def transform(data, operation, content_type)
6
+ zipper = Restforce::Bulk::Zipper.new(data, content_type)
7
+ File.read(zipper.zip)
8
+ end
9
+
10
+ def create_request_txt(data)
11
+ build_xml(:sObjects, "xmlns:xsi" => "http://www.w3.org/2001/XMLSchema-instance") do |xml|
12
+ data.each do |item|
13
+ xml.sObject do
14
+ xml.Name item[:filename]
15
+ xml.ParentId item[:parent_id]
16
+ xml.Body "##{item[:filename]}"
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,38 @@
1
+ module Restforce
2
+ module Bulk
3
+ class Client
4
+ def initialize(restforce_client=nil)
5
+ @restforce_client = restforce_client
6
+ end
7
+
8
+ def connection
9
+ @connection ||= (@restforce_client || Restforce.new).tap do |client|
10
+ client.authenticate!
11
+ client.middleware.insert_after Restforce::Middleware::Authorization, Restforce::Bulk::Middleware::Authorization, client, client.options
12
+ client.middleware.response :xml, content_type: /\bxml$/
13
+ client.middleware.use Restforce::Bulk::Middleware::ParseCsv, content_type: /\bcsv$/
14
+ end
15
+ end
16
+
17
+ def perform_request(method, path, data=nil, content_type=:xml, headers={})
18
+ result_headers = content_type_header_for(content_type).merge(headers)
19
+
20
+ connection.send(method, [base_path, path].join('/'), data, result_headers)
21
+ end
22
+
23
+ private
24
+
25
+ def base_path
26
+ @base_path ||= "/services/async/#{connection.options[:api_version]}"
27
+ end
28
+
29
+ def content_type_header_for(content_type)
30
+ { 'Content-Type' => "#{mime_type_for(content_type)} ;charset=UTF-8" }
31
+ end
32
+
33
+ def mime_type_for(content_type)
34
+ Restforce::Bulk::MIME_TYPE_MAPPING[(content_type || :csv).to_sym]
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,79 @@
1
+ module Restforce
2
+ module Bulk
3
+ class Job
4
+ include Restforce::Bulk::Attributes
5
+
6
+ JOB_CONTENT_TYPE_MAPPING = {
7
+ csv: 'CSV',
8
+ xml: 'XML',
9
+ zip_csv: 'ZIP_CSV',
10
+ zip_xml: 'ZIP_XML'
11
+ }
12
+
13
+ class << self
14
+ def create(operation, object_name, content_type=:xml)
15
+ builder = Restforce::Bulk::Builder::Xml.new(operation)
16
+ data = builder.job(object_name, JOB_CONTENT_TYPE_MAPPING[content_type.to_sym])
17
+
18
+ response = Restforce::Bulk.client.perform_request(:post, 'job', data)
19
+
20
+ new(response.body.jobInfo)
21
+ end
22
+
23
+ def find(id)
24
+ response = Restforce::Bulk.client.perform_request(:get, "job/#{id}")
25
+
26
+ new(response.body.jobInfo)
27
+ end
28
+ end
29
+
30
+ attr_accessor :id, :operation, :object, :created_by_id, :created_date,
31
+ :system_modstamp, :state, :content_type
32
+
33
+ def initialize(attributes={})
34
+ assign_attributes(attributes)
35
+
36
+ @batches = []
37
+ end
38
+
39
+ def content_type=(value)
40
+ @content_type = JOB_CONTENT_TYPE_MAPPING.invert[value] || value
41
+ end
42
+
43
+ def batches
44
+ @batches
45
+ end
46
+
47
+ def reload_batches
48
+ response = Restforce::Bulk.client.perform_request(:get, "job/#{id}/batch")
49
+ parser = Restforce::Bulk::Parser::Xml.new
50
+
51
+ @batches = parser.batches(response.body).map do |batch_info|
52
+ Restforce::Bulk::Batch.new(batch_info)
53
+ end
54
+ end
55
+
56
+ def add_batch(data)
57
+ Restforce::Bulk::Batch.create(id, data, operation, content_type).tap do |batch|
58
+ batches << batch
59
+ end
60
+ end
61
+
62
+ def close
63
+ builder = Restforce::Bulk::Builder::Xml.new(operation)
64
+
65
+ response = Restforce::Bulk.client.perform_request(:post, "job/#{id}", builder.close)
66
+
67
+ assign_attributes(response.body.jobInfo)
68
+ end
69
+
70
+ def abort
71
+ builder = Restforce::Bulk::Builder::Xml.new(operation)
72
+
73
+ response = Restforce::Bulk.client.perform_request(:post, "job/#{id}", builder.abort)
74
+
75
+ assign_attributes(response.body.jobInfo)
76
+ end
77
+ end
78
+ end
79
+ end
@@ -0,0 +1,14 @@
1
+ module Restforce
2
+ module Bulk
3
+ # Piece of middleware that simply injects the OAuth token into the request
4
+ # headers.
5
+ class Middleware::Authorization < Restforce::Middleware
6
+ AUTH_HEADER = 'X-SFDC-Session'.freeze
7
+
8
+ def call(env)
9
+ env[:request_headers][AUTH_HEADER] = @options[:oauth_token]
10
+ @app.call(env)
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,13 @@
1
+ module Restforce
2
+ module Bulk
3
+ module Middleware
4
+ class ParseCsv < ::FaradayMiddleware::ResponseMiddleware
5
+ dependency 'csv'
6
+
7
+ define_parser do |body|
8
+ ::CSV.parse(body, headers: true)
9
+ end
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,15 @@
1
+ module Restforce
2
+ module Bulk
3
+ module Parser
4
+ class Csv
5
+ def results_on(data)
6
+ data
7
+ end
8
+
9
+ def content_on(data)
10
+ data
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,31 @@
1
+ module Restforce
2
+ module Bulk
3
+ module Parser
4
+ class Xml
5
+ def batches(data)
6
+ parsed_data = Restforce::Mash.new(::MultiXml.parse(data))
7
+
8
+ wrap_in_array(parsed_data.batchInfoList.batchInfo)
9
+ end
10
+
11
+ def results_on(data)
12
+ if data.results
13
+ wrap_in_array(data.results.result)
14
+ else
15
+ [{ id: data.result_list.result }]
16
+ end
17
+ end
18
+
19
+ def content_on(data)
20
+ data.queryResult
21
+ end
22
+
23
+ protected
24
+
25
+ def wrap_in_array(value)
26
+ value.is_a?(Array) ? value : [value]
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,28 @@
1
+ module Restforce
2
+ module Bulk
3
+ class Result
4
+ include Restforce::Bulk::Attributes
5
+
6
+ attr_accessor :id, :success, :created, :error, :job_id, :batch_id
7
+
8
+ def initialize(attributes={})
9
+ assign_attributes(attributes)
10
+ end
11
+
12
+ def content
13
+ response = Restforce::Bulk.client.perform_request(:get, "job/#{job_id}/batch/#{batch_id}/result/#{id}")
14
+ parser = results_parser_for(response.body).new
15
+
16
+ parser.content_on(response.body)
17
+ end
18
+
19
+ protected
20
+
21
+ def results_parser_for(body)
22
+ body.is_a?(CSV::Table) ? Restforce::Bulk::Parser::Csv : Restforce::Bulk::Parser::Xml
23
+ end
24
+ end
25
+ end
26
+ end
27
+
28
+
@@ -0,0 +1,5 @@
1
+ module Restforce
2
+ module Bulk
3
+ VERSION = "0.1.0"
4
+ end
5
+ end
@@ -0,0 +1,36 @@
1
+ module Restforce
2
+ module Bulk
3
+ class Zipper
4
+ attr_accessor :files_mapping, :content_type
5
+
6
+ def initialize(files_mapping, content_type=:zip_xml)
7
+ self.files_mapping = files_mapping
8
+ self.content_type = content_type
9
+ end
10
+
11
+ def zip
12
+ ::Zip::File.open(output_filename, ::Zip::File::CREATE) do |zip_file|
13
+ zip_file.get_output_stream('request.txt') do |io|
14
+ io.write builder.create_request_txt(files_mapping)
15
+ end
16
+
17
+ files_mapping.each do |mapping|
18
+ zip_file.add(mapping[:filename], mapping[:full_filename])
19
+ end
20
+ end
21
+
22
+ output_filename
23
+ end
24
+
25
+ protected
26
+
27
+ def builder
28
+ @builder ||= Restforce::Bulk::Builder.const_get(content_type.to_s.camelize).new('insert')
29
+ end
30
+
31
+ def output_filename
32
+ @output_filename ||= "/tmp/#{SecureRandom.hex}.zip"
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,32 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'restforce/bulk/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "restforce-bulk"
8
+ spec.version = Restforce::Bulk::VERSION
9
+ spec.authors = ["Vicente Mundim"]
10
+ spec.email = ["vicente.mundim@gmail.com"]
11
+
12
+ spec.summary = %q{Client for Salesforce Bulk API}
13
+ spec.description = %q{Client for Salesforce Bulk API}
14
+ spec.homepage = "https://github.com/dtmtec/restforce-bulk"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_dependency "restforce", "~> 2.1.2"
23
+ spec.add_dependency "nokogiri"
24
+ spec.add_dependency "multi_xml"
25
+ spec.add_dependency "activesupport", "~> 4.2.4"
26
+ spec.add_dependency "rubyzip", "~> 1.1.7"
27
+
28
+ spec.add_development_dependency "bundler", "~> 1.10"
29
+ spec.add_development_dependency "rake", "~> 10.0"
30
+ spec.add_development_dependency "rspec"
31
+ spec.add_development_dependency "simplecov"
32
+ end
metadata ADDED
@@ -0,0 +1,199 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: restforce-bulk
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Vicente Mundim
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-12-14 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: restforce
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 2.1.2
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 2.1.2
27
+ - !ruby/object:Gem::Dependency
28
+ name: nokogiri
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: multi_xml
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: activesupport
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 4.2.4
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 4.2.4
69
+ - !ruby/object:Gem::Dependency
70
+ name: rubyzip
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: 1.1.7
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: 1.1.7
83
+ - !ruby/object:Gem::Dependency
84
+ name: bundler
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '1.10'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '1.10'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rake
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '10.0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '10.0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: rspec
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ - !ruby/object:Gem::Dependency
126
+ name: simplecov
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ description: Client for Salesforce Bulk API
140
+ email:
141
+ - vicente.mundim@gmail.com
142
+ executables: []
143
+ extensions: []
144
+ extra_rdoc_files: []
145
+ files:
146
+ - ".gitignore"
147
+ - ".rspec"
148
+ - ".ruby-gemset"
149
+ - ".ruby-version"
150
+ - ".travis.yml"
151
+ - CODE_OF_CONDUCT.md
152
+ - Gemfile
153
+ - LICENSE.txt
154
+ - README.md
155
+ - Rakefile
156
+ - bin/console
157
+ - bin/setup
158
+ - lib/restforce/bulk.rb
159
+ - lib/restforce/bulk/attributes.rb
160
+ - lib/restforce/bulk/batch.rb
161
+ - lib/restforce/bulk/builder/csv.rb
162
+ - lib/restforce/bulk/builder/xml.rb
163
+ - lib/restforce/bulk/builder/zip_csv.rb
164
+ - lib/restforce/bulk/builder/zip_xml.rb
165
+ - lib/restforce/bulk/client.rb
166
+ - lib/restforce/bulk/job.rb
167
+ - lib/restforce/bulk/middleware/authorization.rb
168
+ - lib/restforce/bulk/middleware/parse_csv.rb
169
+ - lib/restforce/bulk/parser/csv.rb
170
+ - lib/restforce/bulk/parser/xml.rb
171
+ - lib/restforce/bulk/result.rb
172
+ - lib/restforce/bulk/version.rb
173
+ - lib/restforce/bulk/zipper.rb
174
+ - restforce-bulk.gemspec
175
+ homepage: https://github.com/dtmtec/restforce-bulk
176
+ licenses:
177
+ - MIT
178
+ metadata: {}
179
+ post_install_message:
180
+ rdoc_options: []
181
+ require_paths:
182
+ - lib
183
+ required_ruby_version: !ruby/object:Gem::Requirement
184
+ requirements:
185
+ - - ">="
186
+ - !ruby/object:Gem::Version
187
+ version: '0'
188
+ required_rubygems_version: !ruby/object:Gem::Requirement
189
+ requirements:
190
+ - - ">="
191
+ - !ruby/object:Gem::Version
192
+ version: '0'
193
+ requirements: []
194
+ rubyforge_project:
195
+ rubygems_version: 2.4.6
196
+ signing_key:
197
+ specification_version: 4
198
+ summary: Client for Salesforce Bulk API
199
+ test_files: []