Class: Testin::Network

Inherits:
Object
  • Object
show all
Defined in:
lib/testin.rb

Direct Known Subclasses

TestinNetwork

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Network

Returns a new instance of Network.



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/testin.rb', line 29

def initialize(options)
  @options = options

  @conn_options = {
      request: {
          timeout:      30,
          open_timeout: 300
      },
      #proxy: "http://127.0.0.1:8888" # for debug
  }
  @testin_client = Faraday.new(self.class.testin_hostname, @conn_options) do |c|
    c.request :url_encoded
    c.adapter :net_http
    c.response :json, :content_type => /\bjson$/
  end

end

Class Method Details

.testin_hostnameObject



25
26
27
# File 'lib/testin.rb', line 25

def self.testin_hostname
  "http://fileupload.pro.testin.cn"
end

Instance Method Details

#requestObject



47
48
49
50
51
52
53
54
55
# File 'lib/testin.rb', line 47

def request
  path = ''
  @testin_client.post do |req|
    req.url path
    req.headers['Host'] = 'openapi.pro.testin.cn'
    req.headers['Content-Type'] = 'text/plain'
    req.body = @options[:param].to_json
  end
end

#validation_response(response_data) ⇒ Object



57
58
59
60
61
62
# File 'lib/testin.rb', line 57

def validation_response response_data
  error_code = response_data['code'].to_i
  return if error_code.zero?
  raise 'validation_response error' if response_data['msg'].empty?
  raise response_data['msg']
end