salesforce_http_client 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: ced76573a38601c160a91e585daac06094c6a1f7
4
+ data.tar.gz: d140a7815470645577a4bc8310f4e02af941ea00
5
+ SHA512:
6
+ metadata.gz: 778cf8fde5dca1f7af4ad9951f8c8e22096fb7957c95fe43f13ccd33b8a476788d9e6c68755a9b52cf61c8c03c883ba67d478557bed82a396fc0b505e2441a62
7
+ data.tar.gz: a0d21e3f8e1adbcd3ee4e02848e117d3484ef160f2687dc32a73a63110a82a08ab978f5633dfb32e3e0f8dbc151270205c407fe3f3e89220d52901b75f549923
data/.gitignore ADDED
@@ -0,0 +1,22 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ mkmf.log
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format=documentation
data/.rubocop.yml ADDED
@@ -0,0 +1,10 @@
1
+ AllCops:
2
+ Include:
3
+ - lib/**/*.rb
4
+ - spec/**/*.rb
5
+ Documentation:
6
+ Enabled: false
7
+ StringLiterals:
8
+ Enabled: false
9
+ LineLength:
10
+ Enabled: false
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in salesforce-http-client.gemspec
4
+ gemspec
data/Guardfile ADDED
@@ -0,0 +1,16 @@
1
+ group :dev, halt_on_fail: true do
2
+ guard :rspec, all_on_start: false, all_after_pass: false, failed_mode: :keep, cmd: 'bundle exec rspec' do
3
+ watch(%r{^spec/.+_spec\.rb$})
4
+ watch(%r{^lib/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
5
+ watch('spec/spec_helper.rb') { 'spec' }
6
+ watch(%r{^spec/support/(.+)\.rb$}) { 'spec' }
7
+ end
8
+
9
+ guard :rubocop, all_on_start: false, all_after_pass: false do
10
+ watch(%r{.+\.rb$})
11
+ watch(%r{(?:.+/)?\.rubocop\.yml$}) { |m| File.dirname(m[0]) }
12
+ end
13
+ end
14
+
15
+ # default group
16
+ scope group: :dev
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Kun Wang
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # SalesforceHttpClient
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'salesforce_http_client'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install salesforce_http_client
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it ( https://github.com/[my-github-username]/salesforce_http_client/fork )
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,3 @@
1
+ Dir["#{File.dirname(__FILE__)}/**/*.rb"].sort.each do |path|
2
+ require path
3
+ end
@@ -0,0 +1,42 @@
1
+ require 'salesforce_http_client/salesforce_http_access'
2
+
3
+ module SalesforceHttpClient
4
+ class Client
5
+ include SalesforceHttpAccess
6
+
7
+ attr_accessor :config
8
+ attr_accessor :logger
9
+
10
+ def initialize
11
+ @config = Configuration.new
12
+ @logger = config.logger
13
+ end
14
+
15
+ def download_report(report_id, output_save_path, override_if_exists = false)
16
+ return if check_override_ng(output_save_path, override_if_exists)
17
+ download_from_salesforce(output_save_path, report_id)
18
+ end
19
+
20
+ private
21
+
22
+ def check_override_ng(output_save_path, override_if_exists)
23
+ if !override_if_exists && File.exist?(output_save_path)
24
+ @logger.error "file exists: #{output_save_path}"
25
+ true
26
+ else
27
+ delete_exist_file(output_save_path)
28
+ false
29
+ end
30
+ end
31
+
32
+ def delete_exist_file(output_save_path)
33
+ return unless File.exist?(output_save_path)
34
+ File.delete(output_save_path)
35
+ @logger.info "exist file deleted: #{output_save_path}"
36
+ end
37
+
38
+ def report_url(report_id)
39
+ @config.report_url(report_id)
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,42 @@
1
+ require 'logger'
2
+
3
+ module SalesforceHttpClient
4
+ class Configuration
5
+ attr_accessor :salesforce_login_url
6
+ attr_accessor :salesforce_logout_url
7
+
8
+ attr_accessor :salesforce_login_id
9
+ attr_accessor :salesforce_password
10
+
11
+ attr_accessor :salesforce_report_url_format
12
+ attr_accessor :salesforce_report_id_param
13
+
14
+ attr_accessor :http_timeout
15
+ attr_accessor :tmp_dir
16
+
17
+ attr_accessor :logger
18
+ attr_accessor :log_level
19
+
20
+ def initialize
21
+ @salesforce_login_url = "https://login.salesforce.com/"
22
+ @salesforce_logout_url = "https://ap.salesforce.com/secur/logout.jsp"
23
+
24
+ @salesforce_report_url_format = 'https://ap.salesforce.com/#{report_id}?export=1&enc=UTF-8&xf=csv'
25
+ @salesforce_report_id_param = '#{report_id}'
26
+
27
+ @http_timeout = 5 * 60 * 1000
28
+ @tmp_dir = './tmp'
29
+
30
+ @logger = Logger.new(STDOUT)
31
+ @logger.level = Logger::WARN
32
+ end
33
+
34
+ def report_url(report_id)
35
+ @salesforce_report_url_format.gsub(@salesforce_report_id_param, report_id)
36
+ end
37
+
38
+ def cookie_store_file_path
39
+ File.join(@tmp_dir, 'cookie_store.dat')
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,53 @@
1
+ module SalesforceHttpClient
2
+ module SalesforceHttpAccess
3
+ attr_accessor :http_client
4
+
5
+ def download_from_salesforce(output_save_path, report_id)
6
+ create_http_client
7
+ salesforce_login
8
+ download_and_save_report(report_id, output_save_path)
9
+ salesforce_logout
10
+ end
11
+
12
+ def create_http_client
13
+ @http_client = HTTPClient.new
14
+ @http_client.receive_timeout = @config.http_timeout
15
+ @http_client.set_cookie_store(@config.cookie_store_file_path)
16
+ @http_client
17
+ end
18
+
19
+ def salesforce_login
20
+ @logger.info "try salesforce login..."
21
+ @http_client.get config.salesforce_login_url
22
+ body = { 'un' => @config.salesforce_login_id, 'pw' => @config.salesforce_password }
23
+ response = @http_client.post(config.salesforce_login_url, body)
24
+ respond_to_redirect(response)
25
+ end
26
+
27
+ def respond_to_redirect(response)
28
+ while response.status == 302 && response.headers['Location'] && !response.headers['Location'].empty?
29
+ @logger.info "redirect to #{response.headers['Location']}"
30
+ response = @http_client.get(response.headers['Location'])
31
+ end
32
+ end
33
+
34
+ def download_and_save_report(report_id, output_save_path)
35
+ @logger.info "begin download reports"
36
+ response = @http_client.get report_url(report_id)
37
+ if response.status == 200
38
+ report_content = response.content
39
+ report_file = output_save_path
40
+ @logger.info "save result to file #{report_file}"
41
+ File.write(report_file, report_content)
42
+ else
43
+ @logger.error "failed to download reports."
44
+ end
45
+ end
46
+
47
+ def salesforce_logout
48
+ @logger.info "try salesforce logout..."
49
+ @http_client.get config.salesforce_logout_url
50
+ @logger.info "salesforce logged out."
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,3 @@
1
+ module SalesforceHttpClient
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,33 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'salesforce_http_client/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "salesforce_http_client"
8
+ spec.version = SalesforceHttpClient::VERSION
9
+ spec.authors = ["Apuruni"]
10
+ spec.email = ["apuruni@gmail.com"]
11
+ spec.summary = "a http client for salesforce.com"
12
+ spec.description = "allow you to download a report from salesforce.com quickly."
13
+ spec.homepage = "https://github.com/apuruni/salesforce_http_client"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_runtime_dependency 'httpclient', '~> 2.6.0.1'
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.6"
24
+ spec.add_development_dependency "rake"
25
+ spec.add_development_dependency "rspec"
26
+ spec.add_development_dependency "rspec-legacy_formatters"
27
+ spec.add_development_dependency "rubocop"
28
+ spec.add_development_dependency "guard"
29
+ spec.add_development_dependency "guard-rspec"
30
+ spec.add_development_dependency "guard-rubocop"
31
+ spec.add_development_dependency "pry"
32
+ spec.add_development_dependency "webmock"
33
+ end
@@ -0,0 +1,38 @@
1
+ require 'spec_helper'
2
+
3
+ module SalesforceHttpClient
4
+ describe Client do
5
+ it 'can be init' do
6
+ client = SalesforceHttpClient::Client.new
7
+ expect(client).to_not be_nil
8
+ end
9
+ end
10
+
11
+ describe '#download_report' do
12
+ before do
13
+ stub_request(:get, "https://login.salesforce.com/")
14
+ .to_return(status: 200, body: "", headers: {})
15
+
16
+ stub_request(:post, "https://login.salesforce.com/")
17
+ .with(body: { "pw" => "", "un" => "" })
18
+ .to_return(status: 302, body: "", headers: { 'Location' => 'https://ap.salesforce.com/secur/frontdoor.jsp?sid=00D10000000ZIju%21ARkAQBU5ixFxxAzNvQTreC6rOQ9QSt_cMxFbwmVoVNbWtl.xxHasjFO8v100wa6b0kv.c9Q9RzYuwss9F5TodikF.ELZAHwq&apv=1&cshc=0000001shFs0000000ZIju&display=page' })
19
+
20
+ stub_request(:get, "https://ap.salesforce.com/secur/frontdoor.jsp?apv=1&cshc=0000001shFs0000000ZIju&display=page&sid=00D10000000ZIju!ARkAQBU5ixFxxAzNvQTreC6rOQ9QSt_cMxFbwmVoVNbWtl.xxHasjFO8v100wa6b0kv.c9Q9RzYuwss9F5TodikF.ELZAHwq")
21
+ .to_return(status: 200, body: "", headers: {})
22
+
23
+ stub_request(:get, "https://ap.salesforce.com/a?enc=UTF-8&export=1&xf=csv")
24
+ .to_return(status: 200, body: "", headers: {})
25
+
26
+ stub_request(:get, "https://ap.salesforce.com/secur/logout.jsp")
27
+ .to_return(status: 200, body: "", headers: {})
28
+ end
29
+
30
+ it 'can download report' do
31
+ output_file = "tmp/report_a.csv"
32
+ File.delete(output_file) if File.exist?(output_file)
33
+
34
+ client = SalesforceHttpClient::Client.new
35
+ client.download_report("a", output_file)
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,10 @@
1
+ require 'spec_helper'
2
+
3
+ module SalesforceHttpClient
4
+ describe Configuration do
5
+ it 'can be init' do
6
+ client = SalesforceHttpClient::Configuration.new
7
+ expect(client).to_not be_nil
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,10 @@
1
+ require 'spec_helper'
2
+ require 'salesforce_http_client/version'
3
+
4
+ module SalesforceHttpClient
5
+ describe 'VERSION' do
6
+ it 'has a version number' do
7
+ expect(SalesforceHttpClient::VERSION).not_to be nil
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,6 @@
1
+ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
2
+ require 'salesforce_http_client'
3
+
4
+ RSpec.configure do
5
+ require 'webmock/rspec'
6
+ end
metadata ADDED
@@ -0,0 +1,220 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: salesforce_http_client
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Apuruni
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-01-20 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: httpclient
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 2.6.0.1
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 2.6.0.1
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.6'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.6'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
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: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec-legacy_formatters
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rubocop
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: guard
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: guard-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: guard-rubocop
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
+ - !ruby/object:Gem::Dependency
140
+ name: pry
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - ">="
144
+ - !ruby/object:Gem::Version
145
+ version: '0'
146
+ type: :development
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - ">="
151
+ - !ruby/object:Gem::Version
152
+ version: '0'
153
+ - !ruby/object:Gem::Dependency
154
+ name: webmock
155
+ requirement: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - ">="
158
+ - !ruby/object:Gem::Version
159
+ version: '0'
160
+ type: :development
161
+ prerelease: false
162
+ version_requirements: !ruby/object:Gem::Requirement
163
+ requirements:
164
+ - - ">="
165
+ - !ruby/object:Gem::Version
166
+ version: '0'
167
+ description: allow you to download a report from salesforce.com quickly.
168
+ email:
169
+ - apuruni@gmail.com
170
+ executables: []
171
+ extensions: []
172
+ extra_rdoc_files: []
173
+ files:
174
+ - ".gitignore"
175
+ - ".rspec"
176
+ - ".rubocop.yml"
177
+ - Gemfile
178
+ - Guardfile
179
+ - LICENSE.txt
180
+ - README.md
181
+ - Rakefile
182
+ - lib/salesforce_http_client.rb
183
+ - lib/salesforce_http_client/client.rb
184
+ - lib/salesforce_http_client/configuration.rb
185
+ - lib/salesforce_http_client/salesforce_http_access.rb
186
+ - lib/salesforce_http_client/version.rb
187
+ - salesforce_http_client.gemspec
188
+ - spec/salesforce_http_client/client_spec.rb
189
+ - spec/salesforce_http_client/configuration_spec.rb
190
+ - spec/salesforce_http_client/version_spec.rb
191
+ - spec/spec_helper.rb
192
+ homepage: https://github.com/apuruni/salesforce_http_client
193
+ licenses:
194
+ - MIT
195
+ metadata: {}
196
+ post_install_message:
197
+ rdoc_options: []
198
+ require_paths:
199
+ - lib
200
+ required_ruby_version: !ruby/object:Gem::Requirement
201
+ requirements:
202
+ - - ">="
203
+ - !ruby/object:Gem::Version
204
+ version: '0'
205
+ required_rubygems_version: !ruby/object:Gem::Requirement
206
+ requirements:
207
+ - - ">="
208
+ - !ruby/object:Gem::Version
209
+ version: '0'
210
+ requirements: []
211
+ rubyforge_project:
212
+ rubygems_version: 2.2.2
213
+ signing_key:
214
+ specification_version: 4
215
+ summary: a http client for salesforce.com
216
+ test_files:
217
+ - spec/salesforce_http_client/client_spec.rb
218
+ - spec/salesforce_http_client/configuration_spec.rb
219
+ - spec/salesforce_http_client/version_spec.rb
220
+ - spec/spec_helper.rb