Module: HuobiApi::Network
- Included in:
- Client
- Defined in:
- lib/huobi_api/network.rb
Constant Summary collapse
- BASE_URL =
'https://api.huobi.pro'.freeze
- SIGNATURE_VERSION =
2- HEADERS =
{ 'Content-Type'=> 'application/json', 'Accept' => 'application/json', 'Accept-Language' => 'en-GB', }.freeze
Instance Method Summary collapse
- #build_params(endpoint, method, data) ⇒ Object
- #get(endpoint, data) ⇒ Object
- #hash_sort(ha) ⇒ Object
- #post(endpoint, data) ⇒ Object
- #request(endpoint, method, data) ⇒ Object
- #sign(data) ⇒ Object
Instance Method Details
#build_params(endpoint, method, data) ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/huobi_api/network.rb', line 43 def build_params(endpoint, method, data) # things huobi want you to send (and sign) params = { 'AccessKeyId' => HuobiApi.key, 'SignatureMethod' => 'HmacSHA256', 'SignatureVersion' => SIGNATURE_VERSION, 'Timestamp' => Time.now.getutc.strftime("%Y-%m-%dT%H:%M:%S") } # add in what we're sending, if it's a get request params.merge!(data) if method.to_s.upcase == "GET" # alphabetical order, as that's what huobi likes sorted_params = hash_sort(params) # now mash into a query string query_string = Rack::Utils.build_query(sorted_params) # now add some other random shit to_sign = "#{method.to_s.upcase}\napi.huobi.pro\n#{endpoint}\n#{query_string}" # now sign in sig = sign(to_sign) # and mash it into the params params['Signature'] = sig params end |
#get(endpoint, data) ⇒ Object
22 23 24 |
# File 'lib/huobi_api/network.rb', line 22 def get(endpoint, data) request(endpoint, :GET, data) end |
#hash_sort(ha) ⇒ Object
72 73 74 75 |
# File 'lib/huobi_api/network.rb', line 72 def hash_sort(ha) puts ha Hash[ha.sort_by{ |k, _| k }] end |
#post(endpoint, data) ⇒ Object
18 19 20 |
# File 'lib/huobi_api/network.rb', line 18 def post(endpoint, data) request(endpoint, :POST, data) end |
#request(endpoint, method, data) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/huobi_api/network.rb', line 26 def request(endpoint, method, data) uri = URI.parse(BASE_URL) http = Net::HTTP.new(uri.host, uri.port) http.use_ssl = true params = build_params(endpoint, method, data) url = "#{BASE_URL}#{endpoint}?#{Rack::Utils.build_query(params)}" begin JSON.parse http.send_request(method, url, JSON.dump(data), HEADERS).body rescue Net::HTTPExceptions => e {"message" => 'error' ,"request_error" => e.} rescue JSON::ParserError => e {"message" => 'error' ,"request_error" => e.} end end |