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'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(env = :uat) ⇒ Pgyer

Returns a new instance of Pgyer.



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

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

Instance Attribute Details

#api_keyObject (readonly)

Returns the value of attribute api_key.



14
15
16
# File 'lib/lhj/helper/pgyer_helper.rb', line 14

def api_key
  @api_key
end

#passwordObject (readonly)

Returns the value of attribute password.



14
15
16
# File 'lib/lhj/helper/pgyer_helper.rb', line 14

def password
  @password
end

#resultObject (readonly)

Returns the value of attribute result.



14
15
16
# File 'lib/lhj/helper/pgyer_helper.rb', line 14

def result
  @result
end

#user_keyObject (readonly)

Returns the value of attribute user_key.



14
15
16
# File 'lib/lhj/helper/pgyer_helper.rb', line 14

def user_key
  @user_key
end

Instance Method Details

#http_clientFaraday::Connection

Returns:

  • (Faraday::Connection)


24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/lhj/helper/pgyer_helper.rb', line 24

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



65
66
67
68
# File 'lib/lhj/helper/pgyer_helper.rb', line 65

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



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/lhj/helper/pgyer_helper.rb', line 39

def upload_file(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...'
    response = http_client.post API_HOST, params
    @result = response.body
    puts @result
    puts 'upload success'
    if @result['data'] && @result['data']['appUpdateDescription']
      @result['data']['appUpdateDescription'] = Lhj::TbHelper.trans_tb(@result['data']['appUpdateDescription'])
    end
  rescue Faraday::TimeoutError
    puts 'upload fail'
  end
end