lib/okcoin/http_utils.rb in okcoin-0.1.3 vs lib/okcoin/http_utils.rb in okcoin-0.1.4

- old
+ new

@@ -1,39 +1,39 @@ -module HttpUtils +module Okcoin + module HttpUtils + # result = http_get("https://www.okcoin.cn/api/v1/trades.do", { symbol: 'btc_usd', since: nil }) + 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}") ) - # result = http_get("https://www.okcoin.cn/api/v1/trades.do", { symbol: 'btc_usd', since: nil }) - def http_get(url, params={}) - uri = URI(url) - uri.query = URI.encode_www_form(params) - 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 - response = Net::HTTP.start(uri.host, uri.port, :use_ssl => true) do |http| - request = Net::HTTP::Get.new uri - http.request request + if response.is_a?(Net::HTTPOK) + JSON.parse(response.body) + else + Okcoin::logger.error('response code is not 200!') + nil # todo: 报个异常 + end end - if response.is_a?(Net::HTTPOK) - JSON.parse(response.body) - else - logger.error('response code is not 200!') - nil # todo: 报个异常 - end - end + 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 - def http_post(url, params={}) - uri = URI(url) - 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 + if response.is_a?(Net::HTTPOK) + JSON.parse(response.body) + else + Okcoin::logger.error('response code is not 200!') + nil # todo: 报个异常 + end end - - if response.is_a?(Net::HTTPOK) - JSON.parse(response.body) - else - logger.error('response code is not 200!') - nil # todo: 报个异常 - end end end -