Module: Okcoin::HttpUtils

Included in:
Base
Defined in:
lib/okcoin/http_utils.rb

Instance Method Summary collapse

Instance Method Details

#http_get(url, params = {}) ⇒ Object

result = http_get(“www.okcoin.cn/api/v1/trades.do”, { symbol: ‘btc_usd’, since: nil })



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/okcoin/http_utils.rb', line 4

def http_get(url, params={})
  uri = URI(url)
  uri.query = URI.encode_www_form(params)
  Okcoin::logger.debug( 'GET: ' + (uri.query.blank? ? url : "#{url}?#{uri.query}") )

  response = Net::HTTP.start(uri.host, uri.port, :use_ssl => true) do |http|
    request = Net::HTTP::Get.new uri
    http.request request
  end

  if response.is_a?(Net::HTTPOK)
    JSON.parse(response.body)
  else
    Okcoin::logger.error('response code is not 200!')
    nil # todo: 报个异常
  end
end

#http_post(url, params = {}) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/okcoin/http_utils.rb', line 22

def http_post(url, params={})
  uri = URI(url)
  Okcoin::logger.debug( "POST: #{url}?#{URI.encode_www_form(params)}" )
  response = Net::HTTP.start(uri.hostname, uri.port, :use_ssl => true) do |http|
      request = Net::HTTP::Post.new(uri)
      request.set_form_data(params)
      http.request request
  end

  if response.is_a?(Net::HTTPOK)
    JSON.parse(response.body)
  else
    Okcoin::logger.error('response code is not 200!')
    nil # todo: 报个异常
  end
end