class OceanPackage::Pgy

Attributes

api_key[RW]

# 用户Key,用来标识当前用户的身份 attr_accessor :user_key API Key,用来识别API调用者的身份,如不特别说明,每个接口中都需要含有此参数。

change_log[RW]

# 表示一个App组的唯一Key。 attr_accessor :app_key 更新日志

ipa_file_path[RW]

ipa文件包路径

qr_code_url[RW]

二维码链接

short_url[RW]

短链接

Public Class Methods

new(api_key, change_log, ipa_file_path) click to toggle source

初始化

# File lib/ocean_package/pgy.rb, line 25
def initialize(api_key, change_log, ipa_file_path)
  @api_key = api_key
  @change_log = change_log
  @ipa_file_path = ipa_file_path
  @short_url = ""
  @qr_code_url = ""
end

Public Instance Methods

check() click to toggle source

校验

# File lib/ocean_package/pgy.rb, line 34
def check

  # user_key_value = "#{@user_key}"
  # if user_key_value.empty?
  #   Log.error("pgy user key is empty, please check !!!")
  #   return false
  # end

  api_key_value = "#{@api_key}"
  if api_key_value.empty?
    Log.error("pgy api key is empty, please check !!!")
    return false
  end

  # app_key_value = "#{@app_key}"
  # if app_key_value.empty?
  #   Log.error("pgy app key is empty, please check !!!")
  #   return false
  # end

  ipa_file_path_value = "#{@ipa_file_path}"
  if ipa_file_path_value.empty?
    Log.error("ipa file path is empty, please check !!!")
    return false
  end

  return true
end
get_download_url() click to toggle source

获取下载链接

# File lib/ocean_package/pgy.rb, line 129
def get_download_url
  # 需要有固定的前缀
  url = "https://www.pgyer.com/" + @short_url

  Log.divider
  Log.info(url)

  url
end
get_qr_code_url() click to toggle source

获取二维码链接

# File lib/ocean_package/pgy.rb, line 140
def get_qr_code_url
  url = "#{@qr_code_url}"

  Log.divider
  Log.info(url)

  url
end
publish() click to toggle source

上传

# File lib/ocean_package/pgy.rb, line 72
def publish
  url = URI.parse('https://www.pgyer.com/apiv2/app/upload')
  File.open("#{@ipa_file_path}") do |ipa|

    # 构建必要的参数
    ipa_file_name = Time.new.strftime("%Y-%m-%d_%H-%M-%S") + ".ipa"
    require_params = {
        "file" => UploadIO.new(ipa, "multipart/form-data", ipa_file_name),
        "_api_key" => "#{@api_key}",
        "buildUpdateDescription" => "#{@change_log}",
    }

    # 构造请求
    req = Net::HTTP::Post::Multipart.new(url.path, require_params)

    # 网络http
    net = Net::HTTP.new(url.host, url.port)
    # 这里需要设置,否则会有 EPIPE: Broken pipe 错误
    net.use_ssl = true
    net.verify_mode = OpenSSL::SSL::VERIFY_NONE
    net.set_debug_output($stdout)

    # 发起请求
    res = net.start do |http|
      http.use_ssl = true
      http.verify_mode = OpenSSL::SSL::VERIFY_NONE
      http.request(req)
    end

    Log.divider
    Log.info(res)

    # 具体参考 https://www.pgyer.com/doc/view/api#paramInfo
    case res
    when Net::HTTPSuccess
      # 响应是字符串类型,需要转为json进行处理
      json_value = JSON.parse(res.body)
      Log.divider
      Log.info('pgy upload response: ')
      Log.info(json_value)
      # 成功
      data = json_value["data"]
      short_url = data["buildShortcutUrl"]
      qr_code_url = data["buildQRCodeURL"]
      @short_url = short_url
      @qr_code_url = qr_code_url
      Log.error(@short_url)
      Log.error(@qr_code_url)
    else
      # 失败
      Log.error("pgy publish fail, please check !!!")
      exit(1)
    end
  end
end
run() click to toggle source

运行

# File lib/ocean_package/pgy.rb, line 64
def run
  unless check
    return
  end
  publish
end