zuora_api 0.2.7.3 → 1.8.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: bbc1c0c19c6ed01080a6716636227a5704c30056
4
- data.tar.gz: 36d2997dd12f6ddaff27deccf3aa0150ea796e48
2
+ SHA256:
3
+ metadata.gz: b331dbd16a302e01b8cf4dc918343eb3c3bc62e809c15e4020eb2463b0b725ac
4
+ data.tar.gz: c8f2f3f7c3153280e0624e38658b51f2cdd2e3ded90ad916701c2392c21223d1
5
5
  SHA512:
6
- metadata.gz: 6e2f67e8fbad28fc13983060f0575f34051f99a093826dc12f0bbd27526b128aef3b16aa3823cafbf427953f47f3bd8c252d5d3e8afbe746e93a25087690528e
7
- data.tar.gz: 5c22ff75f2454c8d882ebddf2916b2bdc6c5142caa86b5321989a79e00535907a32e21e8eb61db45c52aba1afd547ba30340ca3da17e48e8d046e2ff66f88f34
6
+ metadata.gz: 29be5568ae328fe982f8c25f25d802f9ab941c2e0a4aa247d12ba1ef33afb25a77e7d738a1509e96a1e28ef8b07c68a205a9c3140dcc83f36a36536cc5d6c42e
7
+ data.tar.gz: b450c458b5402843f9fdf57789cfaf28216c14d74505fe031c95bee79505f6273164f299c78dc73e4848c0ed9c620b41f3cb7ef219774e74d216d12c8a26f0c3
data/.gitignore CHANGED
@@ -7,3 +7,4 @@
7
7
  /pkg/
8
8
  /spec/reports/
9
9
  /tmp/
10
+ .idea
@@ -0,0 +1,63 @@
1
+ image: ruby:2.6
2
+ stages:
3
+ - setup
4
+ - test
5
+ - deploy
6
+
7
+ setup:
8
+ stage: setup
9
+ allow_failure: true
10
+ cache:
11
+ key: gems
12
+ paths:
13
+ - vendor/bundle
14
+ script:
15
+ - apt-get update -qy
16
+ - apt-get install -y nodejs
17
+ - bundle install
18
+
19
+ rubocop-testing:
20
+ stage: test
21
+ allow_failure: true
22
+ script:
23
+ - gem install rubocop
24
+ - rubocop --lint
25
+
26
+ security-testing:
27
+ stage: test
28
+ allow_failure: true
29
+ script:
30
+ - gem install brakeman
31
+ - brakeman
32
+
33
+ rspec-testing:
34
+ stage: test
35
+ script:
36
+ - bundle install
37
+ - rspec
38
+
39
+ rubygems-deploy:
40
+ stage: deploy
41
+ allow_failure: false
42
+ script:
43
+ - echo "deb http://ftp.us.debian.org/debian testing main contrib non-free" >> /etc/apt/sources.list
44
+ - apt-get update
45
+ - apt-get install -y git
46
+ - apt-get clean all
47
+ - gem install dpl
48
+ - if [[ "staging" == $CI_BUILD_REF_SLUG ]];then export VERSION=`git describe --match "[0-9]*\.[0-9]*\.[0-9]*[a-z]" --abbrev=0 --tags HEAD`; fi
49
+ - if [[ "master" == $CI_BUILD_REF_SLUG ]];then export VERSION=`git describe --exclude "[0-9]*\.[0-9]*\.[0-9]*[a-z]" --abbrev=0 --tags HEAD`; fi
50
+ - echo $VERSION
51
+ - sed -i "s/0.0.1/$VERSION/" lib/zuora_api/version.rb
52
+ - git add lib/zuora_api/version.rb
53
+ - git config --global user.email "connect@zuora.com"
54
+ - git config --global user.name "Connect Automation"
55
+ - git commit -m "Automated Version Update $VERSION"
56
+ - bundle install
57
+ - gem install rake
58
+ - version=$(rake install | grep -o 'pkg/zuora_api-.*gem')
59
+ - curl -u $USERNAME:$PASSWORD https://rubygems.org/api/v1/api_key.yaml > ~/.gem/credentials; chmod 0600 ~/.gem/credentials
60
+ - gem push $version
61
+ only:
62
+ - master
63
+ - staging
@@ -0,0 +1,105 @@
1
+ # Changelog
2
+ All notable changes to this project will be documented in this file.
3
+
4
+ ## [1.7.07] - 2018-9-10
5
+ ### Changed
6
+ - Cookie name for service endpoint integration
7
+
8
+ ## [1.7.06] - 2018-8-25
9
+ ### Added
10
+ - Retry for 502/503
11
+ - Standard exception for 504
12
+
13
+ ## [1.7.05] - 2018-8-25
14
+ ### Added
15
+ - Added support for oauth token forbidden
16
+
17
+ ## [1.7.02] - 2018-8-23
18
+ ### Added
19
+ - Added mulit part support for rest call
20
+
21
+ ## [1.7.01] - 2018-8-06
22
+ ### Changed
23
+ - Changed library used to determine host
24
+ - Added retry for 504 timouts in file download
25
+
26
+ ## [1.7.00] - 2018-8-05
27
+ ### Changed
28
+ - Raise proper exception when oauth client is from deactivated user.
29
+ - Support for rails < 6
30
+
31
+ ## [1.6.53] - 2018-7-29
32
+ ### Changed
33
+ - Don't attempt zsession login if bearer token is bad
34
+
35
+ ## [1.6.51 - 1.6.51] - 2018-7-22
36
+ ### Changed
37
+ - Retry on address not available.
38
+
39
+ ## [1.6.47 - 1.6.48] - 2018-6-26
40
+ ### Changed
41
+ - Changed error raise statements when incorrect credentials are supplied to Basic/Oauth Logins
42
+
43
+ ## [1.6.45] - 2018-5-30
44
+ ### Changed
45
+ - Fix retry so headers are reinstaniated on session failure.
46
+
47
+ ## [1.6.41] - 2018-5-30
48
+ ### Changed
49
+ - Retry added on SSL connection failure
50
+
51
+ ## [1.6.39-1.6.40] - 2018-5-30
52
+ ### Changed
53
+ - Added validation to fix bad urls entered into object initialization
54
+
55
+ ## [1.6.38] - 2018-5-26
56
+ ### Changed
57
+ - HttpParty validation before code extraction
58
+
59
+ ## [1.6.37] - 2018-5-23
60
+ ### Added
61
+ - Added method to determine rest endpoint domain
62
+
63
+ ## [1.6.36] - 2018-5-22
64
+ ### Changed
65
+ - Fixed zuora staging 2 endpoint
66
+
67
+ ## [1.6.33] - 2018-5-15
68
+ ### Changed
69
+ - Added Errno::EHOSTUNREACH to list of retriable error codes
70
+
71
+ ## [1.6.32] - 2018-5-14
72
+ ### Changed
73
+ - Don't log fatal errors, allow application to decide for file download
74
+ - Don't change api url if the user set a high api url
75
+
76
+ ###Removed
77
+ - Force encoding
78
+
79
+ ## [1.6.28] - 2018-3-12
80
+ ### Added
81
+ - Way to avoid force encoding for filedownload
82
+
83
+ ## [1.6.28] - 2018-3-12
84
+ ### Added
85
+ - Way to avoid force encoding for filedownload
86
+
87
+ ## [1.6.22] - 2019-01-03
88
+ ### Changed
89
+ - get_identity method - supports ZSession auth now
90
+ - updated rspecs accordingly
91
+
92
+ ## [1.6.18] - 2018-12-06
93
+ ### Added
94
+ - zconnect_provider attribute accessor for identifying ZConnect cookies
95
+ - Methods for Hallway integration:
96
+ - get_identity
97
+ - get_full_nav
98
+ - set_nav
99
+ - refresh_nav
100
+ - get_oauth_client
101
+
102
+ ### Changed
103
+ - The way environment and region are set
104
+
105
+
data/README.md CHANGED
@@ -1,74 +1,147 @@
1
- # Zuora_api
1
+ # Zuora Gem
2
2
 
3
- [![Gem Version](https://badge.fury.io/rb/zuora_api.svg)](https://badge.fury.io/rb/zuora_api)
4
-
5
- This gem provides an easy integration with Zuora
3
+ [![Gem Version](https://badge.fury.io/rb/zuora_api.svg)](https://badge.fury.io/rb/zuora_api) [![coverage report](https://gitlab.0.ecc.auw2.zuora/extension-products/shared-libraries/zuora-gem/badges/master/coverage.svg)](https://gitlab.0.ecc.auw2.zuora/extension-products/shared-libraries/zuora-gem/commits/master)
6
4
 
7
5
  ## Installation
8
-
9
6
  Add this line to your application's Gemfile:
7
+
10
8
  ```ruby
11
9
  gem 'zuora_api'
12
10
  ```
13
- And then execute:
11
+ Then execute `bundle install` in your terminal
14
12
 
15
- $ bundle
13
+ ## Usage
16
14
 
17
- Or install it yourself as:
15
+ ### Zuora Login Object
16
+ In order to make API calls a Zuora Login object must be created
18
17
 
19
- $ gem install zuora
18
+ ```ruby
19
+ zuora_client = ZuoraAPI::Login.new(username: "username", password: "password", url: "url")
20
+ ```
20
21
 
21
- ## Usage
22
+ | Name | Type | Description | Example |
23
+ | ------------------- | ----------- | ---------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------- |
24
+ | username | `Attribute` | Username to the Zuora environment | `zuora_client.username = "username"` |
25
+ | password | `Attribute` | password to the Zuora environment | `zuora_client.password = "Password"` |
26
+ | url | `Attribute` | Endpoint to the Zuora tenant | `zuora_client.url = "www.zuora.com"` |
27
+ | wsdl_number | `Attribute` | WSDL number of the zuora login | `wsdl = zuora_client.wsdl_number` |
28
+ | status | `Attribute` | Status of the login | `zuora_client.status` |
29
+ | current_session | `Attribute` | Current session for the login | `zuora_client.current_session` |
30
+ | environment | `Attribute` | environment of the login | `zuora_client.environment` |
31
+ | errors | `Attribute` | Any errors that the login has based on the login call | `zuora_client.errors` |
32
+ | current_error | `Attribute` | Current error from the new_session call | `zuora_client.current_error` |
33
+ | user_info | `Attribute` | Information related to the login | `zuora_client.user_info` |
34
+ | tenant_id | `Attribute` | Tenant ID the login is associated to | `zuora_client.tenant_id` |
35
+ | tenant_name | `Attribute` | Tenant Name of tenant the login is associated to | `zuora_client.tenant_name` |
36
+ | entity_id | `Attribute` | Current entity the login session is associated to | `zuora_client.entity_id` |
37
+ | rest_call | `Method` | Executes a REST call | `zuora_client.rest_call()` |
38
+ | soap_call | `Method` | Executes a SOAP call | `output_xml, input_xml = zuora_client.soap_call() do `&#124;xml, args&#124;` xml['ns1'].query do xml['ns1'].queryString "select id, name from account" end end` |
39
+ | query | `Method` | Executes a query call | `zuora_client.query("select id, name from account")` |
40
+ | getDataSourceExport | `Method` | Pulls a data source export with the given query and returns the file location | `zuora_client.getDataSourceExport("select id, name from account")` |
41
+ | describe_call | `Method` | Performs the describe call against the Zuora tenant for all objects or a specific object | `response = zuora_client.describe_call("Account")` |
42
+ | createJournalRun | `Method` | Creates a Journal Run | `zuora_client.createJournalRun(call)` |
43
+ | checkJRStatus | `Method` | Checks the status of a journal run | `zuora_client.checkJRStatus(journal_run_id)` |
44
+ | update_environment | `Method` | Sets the login's environment based on the url | `zuora_client.update_environment` |
45
+ | aqua_endpoint | `Method` | Returns the AQuA endpoint for the login based off the environment | `zuora_client.aqua_endpoint` |
46
+ | rest_endpoint | `Method` | Returns the REST endpoint for the login based off the environment | `zuora_client.rest_endpoint` |
47
+ | fileURL | `Method` | Returns the URL for files | `zuora_client.fileURL` |
48
+ | dateFormat | `Method` | Returns the data format syntax based on the wsdl_number | `zuora_client.dateFormat` |
49
+ | new_session | `Method` | Create a new session | `zuora_client.new_session` |
50
+ | get_session | `Method` | Returns the current session | `zuora_client.get_session`|
51
+
52
+ ## Rest Call
53
+ ```ruby
54
+ zuora_client.rest_call(method: :get, body: {}, url: zuora_client.rest_endpoint("catalog/products?pageSize=4"))
55
+ ```
22
56
 
23
- In order to make API calls a Zuora Login object must be created by running
57
+ ### Soap Call
58
+ Returns both output and input XML
24
59
 
25
- zuora_client = Zuora:API:Login.new(:username =>"username", :password => "password", :url => "endpoint")
60
+ ```ruby
61
+ zuora_client.soap_call(ns1: 'ns1', ns2: 'ns2', batch_size: nil, single_transaction: false)
62
+ ```
26
63
 
27
- ### Available Fields
28
- - username
29
- - password
30
- - url
31
- - wsdl_number
32
- - status
33
- - current_session
34
- - environment
35
- - status
36
- - errors
37
- - current_error
38
- - user_info
39
- - tenant_id
40
- - tenant_name
64
+ Example Call
41
65
 
42
- ### Rest Call
66
+ ```ruby
67
+ output_xml, input_xml = zuora_client.soap_call() do |xml, args|
68
+ xml['ns1'].query do
69
+ xml['ns1'].queryString "select id, name from account"
70
+ end
71
+ end
72
+ ```
73
+ ### Query
74
+ ```ruby
75
+ zuora_client.query("select id from account")
76
+ ```
77
+ ### Data Export
78
+ Returns the file location of the data source export after downloading from Zuora
43
79
 
44
- zuora_client.rest_call(method: :get, body: {}, url: rest_endpoint("catalog/products?pageSize=4"),headers: {}, **keyword_args)
80
+ ```ruby
81
+ zuora_client.getDataSourceExport("select id from account")
82
+ ```
45
83
 
46
- ### SOAP Call
84
+ | Name | Description | Default | Example |
85
+ | --------- | ---------------------------------------------------------------------- | ------- | ----------------------------------------------------------------------------- |
86
+ | query | The query to execute | `N/A` | `zuora_client.getDataSourceExport("select id from account")` |
87
+ | zip | Indicates if the data source export should be a zip | `true` | `zuora_client.getDataSourceExport("select id from account", zip: false)` |
88
+ | extract | Indicates if the data source export should be extracted if it is a zip | `true` | `zuora_client.getDataSourceExport("select id from account", extract: false)` |
89
+ | encrypted | Indicates if the data source export should be encrypted | `false` | `zuora_client.getDataSourceExport("select id from account", encrypted: true)` |
47
90
 
48
- soap_call(ns1: 'ns1', ns2: 'ns2', batch_size: nil, single_transaction: false, debug: false , **keyword_args)
49
- Example:
91
+ ### Describe Call
92
+ This returns all available objects from the describe call as a hash. This response can be accessed by using response["Account"] to retrieve all related data about that object.
50
93
 
51
- output_xml, input_xml = zuora_client.soap_call() do |xml, args|
52
- xml['ns1'].query do
53
- xml['ns1'].queryString "select id, name from account"
54
- end
55
- end
56
- ### Query
94
+ ```ruby
95
+ response = zuora_client.describe_call("Account")
96
+ ```
97
+ This returns all information and fields related to that object model as a hash.
57
98
 
58
- zuora_client.query(query)
99
+ ```ruby
100
+ response = zuora_client.describe_call()
101
+ ```
102
+
103
+ ### Journal Run
104
+ ```ruby
105
+ zuora_client.createJournalRun(call)
106
+ ```
59
107
 
60
- ### Data export
108
+ ## Insights API
61
109
 
62
- zuora_client.getDataSourceExport(query)
110
+ In order to make API calls a Zuora Login object must be created by running:
63
111
 
64
- ### Describe Call
112
+ ```ruby
113
+ insightsapi = InsightsAPI::Login.new(api_token: "api token", url: "Nw1.api.insights.zuora.com/api/")
114
+ ```
65
115
 
66
- response = zuora_client.describe_call()
67
- This returns all available objects from the describe call as a hash. This response can be accessed by using `response["Account"]` to retrieve all related data about that object.
116
+ Note that the login will default to the insights production url.
68
117
 
69
- response = zuora_client.describe_call("Account")
70
- This returns all information and fields related to that object model as a hash.
118
+ ```ruby
119
+ Date format: "YYYY-MM-DDT00:00:00Z"
120
+ ```
71
121
 
72
- ### Journal Run
122
+ ### Uploading Data into Insights
123
+ ```ruby
124
+ insightsapi.upload_into_insights(dataSourceName, recordType, batchDate, filePath)
125
+ ```
126
+ dataSourceName: What system the data is coming from.
127
+ recordType: The type of records ie: "EVENTS, ATTRIBUTES, and METRICS"
128
+ batachDate: The date the data applies to.
129
+
130
+ ### Describing Insights Data
131
+ ```ruby
132
+ insightsapi.describe(type: "ACCOUNT/USER", object: "ATTRIBUTES/EVENTS/SEGMENTS/METRICS")
133
+ ```
134
+ Returns json payload describing attributes, events, metrics for each Account or User.
135
+
136
+ ### Downloading Data from Insights
137
+ ```ruby
138
+ insightsapi.data_export_insights(objecttype, segmentuuid, startDate: nil, endDate: nil, tries: 30)
139
+ ```
140
+ ```ruby
141
+ insightsapi.data_export_insights_file(objecttype, segmentuuid, startDate: nil, endDate: nil, tries: 30)
142
+ ```
143
+ Both do the same thing except one returns a url(data_export_insights) to download the file yourself and the other returns an actual Ruby temporary file(data_export_insights_file).
144
+
145
+ objectype: "ACCOUNT/USER"
73
146
 
74
- zuora_client.createJournalRun(call)
147
+ segmentuuid: A single or array of string or int of a segment uuid(s) that you get from the describe call. The csv holds a column with a bool that represents if that User or Account belongs to that segment.
@@ -0,0 +1,220 @@
1
+ require "httparty"
2
+ module InsightsAPI
3
+ class Login
4
+ attr_accessor :api_token, :url
5
+
6
+ def initialize(api_token: nil, url: nil, **keyword_args)
7
+ @api_token = api_token
8
+ @url = url
9
+ @status = "Active"
10
+ end
11
+
12
+ def insight_getstatus(uuid)
13
+ response = HTTParty.get(
14
+ "https://#{@url}/export/status/#{uuid}",
15
+ :basic_auth => { :username => @api_token })
16
+ if response.code == 200
17
+ parsed = JSON.parse(response.body)
18
+ return parsed
19
+ #error handing here
20
+ end
21
+ end
22
+
23
+ def data_export_insights_file(objecttype, segmentuuid, startDate, endDate, tries: 30)
24
+ status = data_export_insights(objecttype, segmentuuid, startDate, endDate, tries: tries)
25
+ if status['status']== "COMPLETE"
26
+ signedUrl = status['signedUrl']
27
+ return get_file(file_name: "insights-#{startDate}-#{endDate}.csv", url: signedUrl, headers: {}, count: 3, file_type: "zip")
28
+ else
29
+ return status
30
+ end
31
+ end
32
+
33
+ def data_export_insights(objecttype, segmentuuid, startDate, endDate, tries: 30)
34
+ status = insights_fetch_all(objecttype, segmentuuid, startDate, endDate)
35
+ if status['uuid'] == nil
36
+ return "Failed: #{status["response"]}"
37
+ else
38
+ fileid = status['uuid']
39
+ end
40
+ for retries in 1..tries
41
+ status = insight_getstatus(fileid)
42
+ if status['status']== "COMPLETE"
43
+ signedUrl = status['signedUrl']
44
+ return status
45
+ elsif status['status'] == "FAILED" || status['status'] == "ERROR"
46
+ return status
47
+ else
48
+ sleep(60)
49
+ retries+=1
50
+ end
51
+ if retries > tries - 1
52
+ return "Timeout"
53
+ end
54
+ end
55
+ return signedUrl
56
+ end
57
+
58
+ def insights_fetch_all(objecttype, segmentuuid, startDate, endDate)
59
+ if segmentuuid.is_a? Array
60
+ segmentsForAPI = segmentuuid.join('","')
61
+ elsif segmentuuid.is_a? String
62
+ segmentsForAPI = segmentuuid
63
+ elsif segmentuuid.is_a? Integer
64
+ segmentsForAPI = segmentuuid.to_s
65
+ else
66
+ raise "Error fetching Insights data: Segmentuuid must be either an array of uuids or an single uuid in string or interger format."
67
+ end
68
+
69
+ response = HTTParty.post(
70
+ "https://#{@url}/export/type/#{objecttype}",
71
+ :basic_auth => { :username => @api_token },
72
+ :headers => {'Content-Type'=> "Application/json"},
73
+ :body =>
74
+ '
75
+ {
76
+ "endDate": "' + (dateFormat(date: endDate)).to_s + '",
77
+ "startDate":"' + (dateFormat(date: startDate)).to_s + '",
78
+ "segments": [
79
+ "'+segmentsForAPI+'"
80
+ ]
81
+ }
82
+ ')
83
+ if response.code == 200
84
+ parsed = JSON.parse(response.body)
85
+ return parsed
86
+ else
87
+ return {"uuid"=> nil, "status"=>"Error", "signedUrl"=>"signedUrl", "response" => response.body}
88
+ end
89
+ end
90
+
91
+ def dateFormat(date: nil)
92
+ date ||= DateTime.now
93
+ if date.is_a? String
94
+ if date.include? "T"
95
+ return (date.to_datetime).to_s
96
+ else
97
+ return (date.to_date).to_s + "T#{DateTime.now.to_s(:time)}:00Z"
98
+ end
99
+ elsif date.instance_of?(DateTime)
100
+ return date.to_s
101
+ elsif date.instance_of?(Date)
102
+ return (date.to_date).to_s + "T#{DateTime.now.to_s(:time)}:00Z"
103
+ else
104
+ raise "Please pass in a in format of 'YYYY-MM-DD', 'YYYY-MM-DDT00:00:00+00:00' ruby Date, or ruby DateTime"
105
+ end
106
+ end
107
+
108
+ def upload_into_insights(dataSourceName, recordType, batchDate, filePath)
109
+ begin
110
+ temp_date = dateFormat(date: batchDate)
111
+ response = HTTParty.post(
112
+ "https://#{@url}/files/upload",
113
+ :basic_auth => { :username => @api_token }, :body => {
114
+ :dataSource => dataSourceName, :recordType => recordType,
115
+ :batchDate => temp_date})
116
+ parsed = JSON.parse(response.body)
117
+ signedUrl = parsed['signedUrl']
118
+ if !File.extname(filePath) == ".gz"
119
+ zipPath = gzip_file(filePath)
120
+ else
121
+ zipPath = filePath
122
+ end
123
+
124
+ gzippedFile = File.open(zipPath)
125
+ post = HTTParty.put(signedUrl,
126
+ :body => gzippedFile.read)
127
+ if post.code == 200
128
+ return {"status"=>"COMPLETE", "signedUrl"=>"#{signedUrl}", "response" => post.code, "batchDate" => "#{temp_date}"}
129
+ else
130
+ return {"status"=>"Error", "signedUrl"=>"#{signedUrl}", "response" => post.code, "message"=> "#{post.message}", "batchDate" => "#{temp_date}"}
131
+ end
132
+ rescue Exception => e
133
+ Rails.logger.debug "[ZuoraGem]: While uploading to insights Error: #{e}"
134
+ raise e
135
+ end
136
+ end
137
+
138
+ def describe(type: "ACCOUNT", object: "attributes")
139
+ url = "https://#{@url}/export/#{object}/#{type}"
140
+ uri = URI.parse(url)
141
+ http = Net::HTTP.new(uri.host, uri.port)
142
+ http.use_ssl = true
143
+ http.verify_mode = OpenSSL::SSL::VERIFY_NONE
144
+ begin
145
+ request = Net::HTTP::Get.new(uri.request_uri)
146
+ request.basic_auth(@api_token, "")
147
+ return http.request(request)
148
+ rescue Exception => e
149
+ Rails.logger.debug "[ZuoraGem]: While describing Zoura Insights objects: #{e}"
150
+ return "Failed"
151
+ end
152
+ end
153
+
154
+ def get_file(file_name: nil, url: nil, basic: {:username => nil, :password => nil}, headers: {}, count: 3, file_type: "zip")
155
+ tries ||= 2
156
+ temp_file = nil
157
+ uri = URI(url)
158
+ Net::HTTP.start(uri.host, uri.port, :use_ssl => uri.scheme == 'https') do |http|
159
+ request = Net::HTTP::Get.new(uri)
160
+ headers.each do |k,v|
161
+ request["#{k}"] = v
162
+ end
163
+ request.basic_auth(basic[:username], basic[:password]) if (!basic[:username].blank? && !basic[:password].blank?)
164
+ http.request request do |response|
165
+ case response
166
+ when Net::HTTPNotFound
167
+ Rails.logger.fatal("[ZuoraGem]: 404 - Not Found")
168
+ raise response
169
+
170
+ when Net::HTTPUnauthorized
171
+ raise ZuoraAPI::Exceptions::ZuoraAPISessionError.new(zuora_client.current_error) if count <= 0
172
+ Rails.logger.fatal("[ZuoraGem]: Retry")
173
+ zuora_client.new_session
174
+ return get_file(:url => url, :count => count - 1, :headers => headers)
175
+
176
+ when Net::HTTPClientError
177
+ Rails.logger.debug("[ZuoraGem]: #{response}")
178
+ raise response
179
+
180
+ when Net::HTTPOK
181
+ Tempfile.open([file_name.rpartition('.').first, ".#{file_name.rpartition('.').last}"], "#{Rails.root}/tmp") do |tmp_file|
182
+ temp_file ||= tmp_file
183
+ tmp_file.binmode if (response.to_hash["content-type"].include?("application/zip") || response.to_hash["content-type"] == "application/zip")
184
+ response.read_body do |chunk|
185
+ tmp_file.write chunk.force_encoding("UTF-8")
186
+ end
187
+ end
188
+ end
189
+ end
190
+ end
191
+
192
+ rescue => ex
193
+ raise ex if tries.zero?
194
+
195
+ tries -= 1
196
+ sleep 3
197
+ retry
198
+ else
199
+ return temp_file
200
+ end
201
+
202
+ def gzip_file(filePath)
203
+ require "zip"
204
+ Zlib::GzipWriter.open(filePath + ".gz") do |gzip|
205
+ open(filePath, "rb") do |f|
206
+ f.each_chunk() {|chunk| gzip.write chunk }
207
+ end
208
+ gzip.close
209
+ end
210
+ return filePath+ ".gz"
211
+ end
212
+
213
+ end
214
+ end
215
+
216
+ class File
217
+ def each_chunk(chunk_size=2**16)
218
+ yield read(chunk_size) until eof?
219
+ end
220
+ end