class Struggle::Http

Attributes

request[R]
uri[R]

Public Class Methods

new(url) click to toggle source
# File lib/struggle/http.rb, line 9
def initialize(url)
  @uri = URI.parse(url)
  @http = Net::HTTP.new(@uri.host, @uri.port)
  if @uri.scheme == 'https'
    @http.use_ssl = true
    @http.verify_mode = OpenSSL::SSL::VERIFY_NONE
  end
end

Public Instance Methods

encode(html) click to toggle source
# File lib/struggle/http.rb, line 46
def encode(html)
  html.force_encoding("gb2312").encode("utf-8")
end
get(params = nil, header = nil) click to toggle source

get请求,params为携带的参数,header为报文头数据,两个参数都是hash类型,示例 http = Struggle::Http.new(“xxx.com/xxx”) result = http.post({name: 'lily', age: 18}).body

# File lib/struggle/http.rb, line 21
def get(params = nil, header = nil)
  @request = Net::HTTP::Get.new(@uri.request_uri, header)
  if !params.blank?
    @request.form_data = params
  end
  @http.request(@request)
end
post(params = nil, header = nil) click to toggle source

post请求,params为携带的参数为json类型,header为报文头数据为hash类型,示例: http = Struggle::Http.new(“xxx.com/xxx”) data = {name: 'lily', age: 18} || '{“aa”:“bb”}'json类型(字符串) result = http.post(data, {'Content-Type' => 'application/json'}).body eval(result) => {status: 200, msg: 'success'}

# File lib/struggle/http.rb, line 34
def post(params = nil, header = nil)
  @request = Net::HTTP::Post.new(@uri.request_uri, header)
  if !params.blank?
    if params.class == Hash
      @request.set_form_data(params)
    else
      @request.body = params
    end
  end
  @http.request(@request)
end