Sha256: 2907adfd502a5234c35f8fc180224b2b0b459339d3232c1f9855131285c7bf23

Contents?: true

Size: 1.57 KB

Versions: 2

Compression:

Stored size: 1.57 KB

Contents

require 'faraday'
require 'faraday_middleware'
require 'lhj/helper/pgyer_config'
require 'lhj/command/yapi/formatters/service'
require 'lhj/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 ''
      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

2 entries across 2 versions & 1 rubygems

Version Path
lhj-tools-0.1.17 lib/lhj/helper/pgyer_helper.rb
lhj-tools-0.1.16 lib/lhj/helper/pgyer_helper.rb