Class: CampBX::API
- Inherits:
-
Object
- Object
- CampBX::API
- Defined in:
- lib/rbtc_arbitrage/campbx.rb
Constant Summary collapse
- @@last =
CampBX API rate limiting probably per IP address (not account) which is why we don’t limit per instance
Time.new(0)
Instance Method Summary collapse
- #api_request(info, post_data = {}) ⇒ Object
-
#initialize(username = nil, password = nil) ⇒ API
constructor
A new instance of API.
- #make_request(http, request) ⇒ Object
Constructor Details
#initialize(username = nil, password = nil) ⇒ API
Returns a new instance of API.
41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/rbtc_arbitrage/campbx.rb', line 41 def initialize( username=nil, password=nil ) @username = username @password = password # Build meta-methods for each API call CALLS.each do |name| define_singleton_method name[0], lambda { |*args| data = CALLS[name[0]] api_request( [data[0], data[1]], Hash[data[2].zip( args )] ) } end end |
Instance Method Details
#api_request(info, post_data = {}) ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/rbtc_arbitrage/campbx.rb', line 54 def api_request( info, post_data={} ) url, auth = info uri = URI.parse(API_BASE + url + '.php') http = Net::HTTP.new(uri.host, uri.port) http.use_ssl=TRUE # CampBX advises latency can be >4 minutes when markets are volatile http.read_timeout = 300 res = nil request = Net::HTTP::Get.new(uri.request_uri) if auth then post_data.merge!({ 'user' => @username, 'pass' => @password, }) request = Net::HTTP::Post.new(uri.request_uri) request.set_form_data( post_data ) end make_request(http, request) end |
#make_request(http, request) ⇒ Object
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/rbtc_arbitrage/campbx.rb', line 76 def make_request http, request # CampBX API: max 1 request per 500ms delta = Time.now - @@last #puts delta*1000 if delta*1000 <= 500 then #puts "sleeping! for #{0.5 - delta}" sleep(0.5 - delta) end res = http.request(request) @@last = Time.now # Update time after request returns if res. == 'OK' then # HTTP OK JSON.parse( res.body ) else # HTTP ERROR warn "HTTP Error: + #{res.code}" end end |