Sha256: 4dd5f5bc2675913c861de8af6e40b4be997d0e331bc48363b2145581ff93a8c9

Contents?: true

Size: 1.62 KB

Versions: 3

Compression:

Stored size: 1.62 KB

Contents

module DeployGate
  module API
    module V1
      class Base
        BASE_URL     = ENV['DG_DEVELOP_URL'] || 'https://deploygate.com'
        API_BASE_URL = "#{BASE_URL}/api"

        # @param [String] token
        # @return [DeployGate::API::V1::Base]
        def initialize(token = nil)
          @token = token
        end

        # @param [String] path
        # @param [Hash] params
        # @return [Hash] Responce parse json
        def get(path, params)
          url = API_BASE_URL + path

          res = client.get(url, params, headers)
          JSON.parse(res.body)
        end

        # @param [String] path
        # @param [Hash] params
        # @yield Upload process block
        # @return [Hash] Responce parse json
        def post(path, params, &process_block)
          url = API_BASE_URL + path

          connection = client.post_async(url, params, headers)
          while true
            break if connection.finished?
            process_block.call unless process_block.nil?
          end
          io = connection.pop.content
          body = ''
          while str = io.read(40)
            body += str
          end

          JSON.parse(body)
        end

        private

        def client
          timeout = 60 * 5 # 5 minutes
          HTTPClient.new(agent_name: "dg/#{DeployGate::VERSION}").tap do |c|
            c.receive_timeout = timeout
            c.send_timeout = timeout
          end
        end

        def headers
          extheaders = []
          unless @token.nil?
            extheaders.push(['AUTHORIZATION', @token])
          end

          extheaders
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
deploygate-0.8.4 lib/deploygate/api/v1/base.rb
deploygate-0.8.3 lib/deploygate/api/v1/base.rb
deploygate-0.8.2 lib/deploygate/api/v1/base.rb