Class: InsightsAPI::Login
- Inherits:
-
Object
- Object
- InsightsAPI::Login
- Defined in:
- lib/insights_api/login.rb
Instance Attribute Summary collapse
-
#api_token ⇒ Object
Returns the value of attribute api_token.
-
#url ⇒ Object
Returns the value of attribute url.
Instance Method Summary collapse
- #data_export_insights(objecttype, segmentuuid, startDate, endDate, tries: 30) ⇒ Object
- #data_export_insights_file(objecttype, segmentuuid, startDate, endDate, tries: 30) ⇒ Object
- #dateFormat(date: nil) ⇒ Object
- #describe(type: "ACCOUNT", object: "attributes") ⇒ Object
- #get_file(file_name: nil, url: nil, basic: {:username => nil, :password => nil}, headers: {}, count: 3, file_type: "zip") ⇒ Object
- #gzip_file(filePath) ⇒ Object
-
#initialize(api_token: nil, url: nil, **keyword_args) ⇒ Login
constructor
A new instance of Login.
- #insight_getstatus(uuid) ⇒ Object
- #insights_fetch_all(objecttype, segmentuuid, startDate, endDate) ⇒ Object
- #upload_into_insights(dataSourceName, recordType, batchDate, filePath) ⇒ Object
Constructor Details
#initialize(api_token: nil, url: nil, **keyword_args) ⇒ Login
Returns a new instance of Login.
7 8 9 10 11 |
# File 'lib/insights_api/login.rb', line 7 def initialize(api_token: nil, url: nil, **keyword_args) @api_token = api_token @url = url @status = "Active" end |
Instance Attribute Details
#api_token ⇒ Object
Returns the value of attribute api_token.
5 6 7 |
# File 'lib/insights_api/login.rb', line 5 def api_token @api_token end |
#url ⇒ Object
Returns the value of attribute url.
5 6 7 |
# File 'lib/insights_api/login.rb', line 5 def url @url end |
Instance Method Details
#data_export_insights(objecttype, segmentuuid, startDate, endDate, tries: 30) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/insights_api/login.rb', line 34 def data_export_insights(objecttype, segmentuuid, startDate, endDate, tries: 30) status = insights_fetch_all(objecttype, segmentuuid, startDate, endDate) if status['uuid'] == nil return "Failed: #{status["response"]}" else fileid = status['uuid'] end for retries in 1..tries status = insight_getstatus(fileid) if status['status']== "COMPLETE" signedUrl = status['signedUrl'] return status elsif status['status'] == "FAILED" return status else sleep(60) retries+=1 end if retries > tries - 1 return "Timeout" end end return signedUrl end |
#data_export_insights_file(objecttype, segmentuuid, startDate, endDate, tries: 30) ⇒ Object
24 25 26 27 28 29 30 31 32 |
# File 'lib/insights_api/login.rb', line 24 def data_export_insights_file(objecttype, segmentuuid, startDate, endDate, tries: 30) status = data_export_insights(objecttype, segmentuuid, startDate, endDate, tries: 30) if status['status']== "COMPLETE" signedUrl = status['signedUrl'] return get_file(file_name: "insights-#{startDate}-#{endDate}.csv", url: signedUrl, headers: {}, count: 3, file_type: "zip") else return status end end |
#dateFormat(date: nil) ⇒ Object
92 93 94 95 |
# File 'lib/insights_api/login.rb', line 92 def dateFormat(date: nil) date ||= DateTime.now return (date.to_date).to_s end |
#describe(type: "ACCOUNT", object: "attributes") ⇒ Object
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/insights_api/login.rb', line 126 def describe(type: "ACCOUNT", object: "attributes") url = "https://#{@url}/export/#{object}/#{type}" uri = URI.parse(url) http = Net::HTTP.new(uri.host, uri.port) http.use_ssl = true http.verify_mode = OpenSSL::SSL::VERIFY_NONE begin request = Net::HTTP::Get.new(uri.request_uri) request.basic_auth(@api_token, "") return http.request(request) rescue Exception => e Rails.logger.debug "[ZuoraGem]: While describing Zoura Insights objects: #{e}" return "Failed" end end |
#get_file(file_name: nil, url: nil, basic: {:username => nil, :password => nil}, headers: {}, count: 3, file_type: "zip") ⇒ Object
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 |
# File 'lib/insights_api/login.rb', line 142 def get_file(file_name: nil, url: nil, basic: {:username => nil, :password => nil}, headers: {}, count: 3, file_type: "zip") tries ||= 2 temp_file = nil uri = URI(url) Net::HTTP.start(uri.host, uri.port, :use_ssl => uri.scheme == 'https') do |http| request = Net::HTTP::Get.new(uri) headers.each do |k,v| request["#{k}"] = v end request.basic_auth(basic[:username], basic[:password]) if (!basic[:username].blank? && !basic[:password].blank?) http.request request do |response| case response when Net::HTTPNotFound Rails.logger.fatal("[ZuoraGem]: 404 - Not Found") raise response when Net:: raise ZuoraAPI::Exceptions::ZuoraAPISessionError.new(zuora_client.current_error) if count <= 0 Rails.logger.fatal("[ZuoraGem]: Retry") zuora_client.new_session return get_file(:url => url, :count => count - 1, :headers => headers) when Net::HTTPClientError Rails.logger.debug("[ZuoraGem]: #{response}") raise response when Net::HTTPOK Tempfile.open([file_name.rpartition('.').first, ".#{file_name.rpartition('.').last}"], "#{Rails.root}/tmp") do |tmp_file| temp_file ||= tmp_file tmp_file.binmode if (response.to_hash["content-type"].include?("application/zip") || response.to_hash["content-type"] == "application/zip") response.read_body do |chunk| tmp_file.write chunk.force_encoding("UTF-8") end end end end end rescue => ex if !(tries -= 1).zero? sleep 3 retry else raise ex end else return temp_file end |
#gzip_file(filePath) ⇒ Object
191 192 193 194 195 196 197 198 199 |
# File 'lib/insights_api/login.rb', line 191 def gzip_file(filePath) Zlib::GzipWriter.open(filePath + ".gz") do |gzip| open(filePath, "rb") do |f| f.each_chunk() {|chunk| gzip.write chunk } end gzip.close end return filePath+ ".gz" end |
#insight_getstatus(uuid) ⇒ Object
13 14 15 16 17 18 19 20 21 22 |
# File 'lib/insights_api/login.rb', line 13 def insight_getstatus(uuid) response = HTTParty.get( "https://#{@url}/export/status/#{uuid}", :basic_auth => { :username => @api_token }) if response.code == 200 parsed = JSON.parse(response.body) return parsed #error handing here end end |
#insights_fetch_all(objecttype, segmentuuid, startDate, endDate) ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/insights_api/login.rb', line 59 def insights_fetch_all(objecttype, segmentuuid, startDate, endDate) if segmentuuid.is_a? Array segmentsForAPI = segmentuuid.join('","') elsif segmentuuid.is_a? String segmentsForAPI = segmentuuid elsif segmentuuid.is_a? Integer segmentsForAPI = segmentuuid.to_s else raise "Error fetching Insights data: Segmentuuid must be either an array of uuids or an single uuid in string or interger format." end response = HTTParty.post( "https://#{@url}/export/type/#{objecttype}", :basic_auth => { :username => @api_token }, :headers => {'Content-Type'=> "Application/json"}, :body => ' { "endDate": "' + (dateFormat(date: endDate)).to_s + '", "startDate":"' + (dateFormat(date: startDate)).to_s + '", "segments": [ "'+segmentsForAPI+'" ] } ') if response.code == 200 parsed = JSON.parse(response.body) return parsed else return {"uuid"=> nil, "status"=>"Error", "signedUrl"=>"signedUrl", "response" => response.body} end end |
#upload_into_insights(dataSourceName, recordType, batchDate, filePath) ⇒ Object
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/insights_api/login.rb', line 97 def upload_into_insights(dataSourceName, recordType, batchDate, filePath) begin response = HTTParty.post( "https://#{@url}/files/upload", :basic_auth => { :username => @api_token }, :body => { :dataSource => dataSourceName, :recordType => recordType, :batchDate => dateFormat(date: batchDate) + "T00:00:00Z"}) parsed = JSON.parse(response.body) signedUrl = parsed['signedUrl'] if !File.extname(filePath) == ".gz" zipPath = gzip_file(filePath) else zipPath = filePath end gzippedFile = File.open(zipPath) post = HTTParty.put(signedUrl, :body => gzippedFile.read) if post.code == 200 return {"status"=>"COMPLETE", "signedUrl"=>"signedUrl", "response" => post.code} else return {"status"=>"Error", "signedUrl"=>"signedUrl", "response" => post.code} end rescue Exception => e Rails.logger.debug "[ZuoraGem]: While uploading to insights Error: #{e}" raise e end end |