Sha256: 7be3e0797d2e08ec4c30b1d9fe781d0911ebc7e058cb7e7e4adfe1df029d12e7

Contents?: true

Size: 1.8 KB

Versions: 1

Compression:

Stored size: 1.8 KB

Contents

require 'faraday'
require 'faraday_middleware'
require 'lhj/helper/pgyer_config'
require 'lhj/command/yapi/formatters/service'
require 'lhj/config'
require_relative 'tb_config'

module Lhj
  # pgyer upload
  class Pgyer

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

    attr_reader :api_key, :user_key, :password, :result

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

    # @return [Faraday::Connection]
    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

    def upload_file(file, des)
      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
        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

    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
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
lhj-tools-0.1.18 lib/lhj/helper/pgyer_helper.rb