Module: ChinaShop::Api

Included in:
Client
Defined in:
lib/chinashop/api.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#api_uriObject (readonly)

Returns the value of attribute api_uri.



9
10
11
# File 'lib/chinashop/api.rb', line 9

def api_uri
  @api_uri
end

Instance Method Details

#get(uri) ⇒ Object



37
38
39
40
41
42
43
44
45
# File 'lib/chinashop/api.rb', line 37

def get(uri)
  uri = URI.parse(uri)
  http = Net::HTTP.new(uri.host, uri.port)
  http.read_timeout = 15
  http.open_timeout = 5
  http.use_ssl = true
  req = Net::HTTP::Get.new(uri.request_uri)
  JSON.parse(http.request(req).body)
end

#initializeObject



11
12
13
# File 'lib/chinashop/api.rb', line 11

def initialize
  @api_uri = 'https://vip.btcchina.com/api_trade_v1.php'
end

#post(opts = {}) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/chinashop/api.rb', line 15

def post(opts = {})
  method = opts[:method]
  params = opts[:params] || []
  tonce = (Time.now.to_f * 1000000).to_i
  query_string = "tonce=#{tonce}&accesskey=#{key}&requestmethod=post&id=#{tonce}&method=#{method}&params=#{params.join(',')}"
  hash = OpenSSL::HMAC.hexdigest('sha1', secret, query_string)
  auth = Base64.encode64("#{key}:#{hash}").gsub("\n", '')
  uri = URI.parse(api_uri)
  http = Net::HTTP.new(uri.host, uri.port)
  http.read_timeout = 15
  http.open_timeout = 5
  http.use_ssl = true
  headers = {
              'Content-Type' => 'application/json-rpc',
              'Authorization' => "Basic #{auth}",
              'Json-Rpc-Tonce' => "#{tonce}"
            }
  req = Net::HTTP::Post.new(api_uri, initheader = headers)
  req.body = JSON.generate('method' => method, 'params' => params, 'id' => tonce)
  JSON.parse(http.request(req).body)
end