Class: Lhj::AppleAnalyticsHelper
- Inherits:
-
Object
- Object
- Lhj::AppleAnalyticsHelper
- Defined in:
- lib/lhj/helper/apple/analytics/apple_analytics_helper.rb
Class Method Summary collapse
- .download_csv(url, path) ⇒ Object
- .notify(keys, conversation_id) ⇒ Object
- .read_all_report(id, date, conversation_id) ⇒ Object
- .read_instances(url, date, path) ⇒ Object
- .read_reports(id, url, date) ⇒ Array
- .read_segments(url, path) ⇒ Object
- .save_file(url, file) ⇒ Object
Class Method Details
.download_csv(url, path) ⇒ Object
61 62 63 64 65 66 67 68 69 |
# File 'lib/lhj/helper/apple/analytics/apple_analytics_helper.rb', line 61 def self.download_csv(url, path) name = "#{rand(36 ** 8).to_s(36)}.csv.gz" key = "#{path}/#{name}" file = File.join(Lhj::Config.instance.home_dir, key) save_file(url, file) Lhj::OSS::Helper.instance.upload(key, file) File.delete(file) if File.exist?(file) key end |
.notify(keys, conversation_id) ⇒ Object
81 82 83 84 85 86 87 88 89 90 |
# File 'lib/lhj/helper/apple/analytics/apple_analytics_helper.rb', line 81 def self.notify(keys, conversation_id) title = 'AppStore数据分析' markdown = "### 数据来源于App Store \n" keys.each do |k| a = k.split('/') name = a[3].gsub('_', ' ') markdown += "- [#{name}](#{Lhj::OSS::Helper.instance.object_url(k)}) \n" end Lhj::Dingtalk.(conversation_id, title, markdown, '') end |
.read_all_report(id, date, conversation_id) ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/lhj/helper/apple/analytics/apple_analytics_helper.rb', line 11 def self.read_all_report(id, date, conversation_id) keys = [] url = "https://api.appstoreconnect.apple.com/v1/apps/#{id}/analyticsReportRequests" reports_res = Lhj::ConnectAPI.get(url) reports_res_body = JSON.parse(reports_res.body) reports_res_body['data'].each do |json| if json['attributes']['accessType'] == 'ONGOING' keys += read_reports(id, json['relationships']['reports']['links']['related'], date) end end notify(keys, conversation_id) if keys.length.positive? end |
.read_instances(url, date, path) ⇒ Object
40 41 42 43 44 45 46 47 48 49 |
# File 'lib/lhj/helper/apple/analytics/apple_analytics_helper.rb', line 40 def self.read_instances(url, date, path) keys = [] req_url = "#{url}?filter%5BprocessingDate%5D=#{date}&filter%5Bgranularity%5D=DAILY" instances_res = Lhj::ConnectAPI.get(req_url) instances_res_body = JSON.parse(instances_res.body) instances_res_body['data'].each do |json| keys += read_segments(json['relationships']['segments']['links']['related'], path) end keys end |
.read_reports(id, url, date) ⇒ Array
28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/lhj/helper/apple/analytics/apple_analytics_helper.rb', line 28 def self.read_reports(id, url, date) keys = [] reports_res = Lhj::ConnectAPI.get(url) reports_res_body = JSON.parse(reports_res.body) reports_res_body['data'].each do |json| name = json['attributes']['name'].gsub(/\s/, '_') path = "analytics/#{id}/#{json['attributes']['category']}/#{name}/#{date}" keys += read_instances(json['relationships']['instances']['links']['related'], date, path) end keys end |
.read_segments(url, path) ⇒ Object
51 52 53 54 55 56 57 58 59 |
# File 'lib/lhj/helper/apple/analytics/apple_analytics_helper.rb', line 51 def self.read_segments(url, path) keys = [] segment_res = Lhj::ConnectAPI.get(url) segment_res_body = JSON.parse(segment_res.body) segment_res_body['data'].each do |json| keys << download_csv(json['attributes']['url'], path) end keys end |
.save_file(url, file) ⇒ Object
71 72 73 74 75 76 77 78 79 |
# File 'lib/lhj/helper/apple/analytics/apple_analytics_helper.rb', line 71 def self.save_file(url, file) http_client = Faraday.new response = http_client.get(url) do |req| req.[:timeout] = 300 req.[:open_timeout] = 20 end FileUtils.mkdir_p(File.dirname(file)) unless File.directory?(File.dirname(file)) File.open(file, 'wb') { |fp| fp.write(response.body) } end |