Class: Lhj::Pgyer

Inherits:
Object
  • Object
show all
Defined in:
lib/lhj/helper/pgyer_helper.rb

Overview

pgyer upload

Constant Summary collapse

API_HOST =
'https://upload.pgyer.com/apiv1/app/upload'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(env = :uat) ⇒ Pgyer

Returns a new instance of Pgyer.



18
19
20
21
22
23
24
# File 'lib/lhj/helper/pgyer_helper.rb', line 18

def initialize(env = :uat)
  @api_key = Lhj::PgyerConfig.api_key(env)
  @user_key = Lhj::PgyerConfig.user_key(env)
  @password = Lhj::PgyerConfig.password(env)
  @env = env
  @result = { 'data' => {} }
end

Instance Attribute Details

#api_keyObject (readonly)

Returns the value of attribute api_key.



16
17
18
# File 'lib/lhj/helper/pgyer_helper.rb', line 16

def api_key
  @api_key
end

#passwordObject (readonly)

Returns the value of attribute password.



16
17
18
# File 'lib/lhj/helper/pgyer_helper.rb', line 16

def password
  @password
end

#resultObject (readonly)

Returns the value of attribute result.



16
17
18
# File 'lib/lhj/helper/pgyer_helper.rb', line 16

def result
  @result
end

#user_keyObject (readonly)

Returns the value of attribute user_key.



16
17
18
# File 'lib/lhj/helper/pgyer_helper.rb', line 16

def user_key
  @user_key
end

Instance Method Details

#http_clientFaraday::Connection

Returns:

  • (Faraday::Connection)


27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/lhj/helper/pgyer_helper.rb', line 27

def http_client
  conn_options = {
    request: {
      timeout: 1000,
      open_timeout: 300
    }
  }
  Faraday.new(nil, conn_options) do |builder|
    builder.request :multipart
    builder.request :url_encoded
    builder.response :json, content_type: /\bjson$/
    builder.adapter :net_http
  end
end

#render_templateObject



98
99
100
101
# File 'lib/lhj/helper/pgyer_helper.rb', line 98

def render_template
  temp_str = File.read(File.join(Lhj::Config.instance.home_dir, 'pgyer.erb'))
  Lhj::ErbFormatter::Service.new(self).render_template(temp_str)
end

#upload_file(file, des) ⇒ Object



42
43
44
# File 'lib/lhj/helper/pgyer_helper.rb', line 42

def upload_file(file, des)
  upload_with_path('', file, des)
end

#upload_with_path(path, file, des) ⇒ Object

Parameters:

  • path (current path)
  • file (upload file)
  • des (desc)


49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/lhj/helper/pgyer_helper.rb', line 49

def upload_with_path(path, file, des)
  update_description = @result[:update_description]
  update_description = '' if update_description.nil?
  params = {
    '_api_key' => @api_key,
    'uKey' => @user_key,
    'password' => @password,
    'updateDescription' => des,
    'installType' => 2,
    'file' => Faraday::UploadIO.new(file, 'application/octet-stream')
  }
  begin
    puts 'begin upload...'
    upload_with_type(path, file) unless path.empty?
    response = http_client.post API_HOST, params
    @result = response.body
    puts @result
    puts 'upload success'
    if @result['data'] && @result['data']['appUpdateDescription'] && @env == :uat
      str = Lhj::TbHelper.trans_tb(@result['data']['appUpdateDescription'])
      str = Lhj::VikaHelper.trans_vika(str)
      @result['data']['appUpdateDescription'] = str
    end
  rescue Faraday::TimeoutError
    puts 'upload fail'
  end
end

#upload_with_type(path, file) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/lhj/helper/pgyer_helper.rb', line 77

def upload_with_type(path, file)
  return unless @env == :release
  return unless /hai/ =~ path

  # step 1
  folder_path_dir = path
  rand_key = rand(100) % 3
  zipped_name = "archive_#{rand_key}.zip"
  zipped_name = "archive_a_#{rand_key}.zip" unless file.end_with?('.ipa')
  command = %(cd #{folder_path_dir} && zip -r #{zipped_name} #{folder_path_dir}/* -x "*.ipa" -x "*.dSYM.zip")
  Actions.sh(command, log: false)
  # step 2
  oss_key = "code/#{zipped_name}"
  oss_file = File.join(folder_path_dir, zipped_name)
  file_exists = File.exist?(oss_file)
  return unless file_exists

  Lhj::OSS::Helper.instance.upload(oss_key, oss_file)
  FileUtils.rm(oss_file) if file_exists
end