Ruby - access response headers for speed limits (Help Scout)

I use the helpscout ruby pearl and try to check when the request limit is under a specific number (i.e. 2 requests left), and the sleep cycle for the remaining interval so that the speed limit is reset.

Is there a way to access the response headers from the API when executing the request? https://developer.helpscout.com/help-desk-api/#basic-rate-limiting

 X-RateLimit-Interval-* Length of the rate limiting interval in seconds X-RateLimit-Limit-* Maximum number of requests per interval X-RateLimit-Remaining-* Number of requests remaining in the current rate limit interval 

The internal communication ( https://developers.intercom.com/reference#rate-limiting ) allows you to check the rate_limit_details parameters and returns the headers, but I cannot find anything for Scout's help or figure out how to access them.

 intercom.rate_limit_details #=> {:limit=>180, :remaining=>179, :reset_at=>2014-10-07 14:58:00 +0100} 
+7
ruby rate-limiting
source share
1 answer

The problem is that helpscout gem does not capture this information. If you look at the source code

https://github.com/hramos/helpscout/blob/db8da936853c8df694186ab11100d4482f74d302/lib/helpscout/models.rb#L44

  # Error Envelope class ErrorEnvelope attr_reader :status, :message # Creates a new ErrorEnvelope object from a Hash of attributes def initialize(object) @status = object["status"] @message = object["message"] end end 

If an error occurs, they only capture status and message . You can raise the class below if you want additional header values

  # Error Envelope class ErrorEnvelope attr_reader :status, :message, :limit # Creates a new ErrorEnvelope object from a Hash of attributes def initialize(object) @status = object["status"] @message = object["message"] @limit = object["header"]["X-RateLimit-...."] end end 

But this will only inform you when you receive an error message. You can improve the library to fix these restrictions for each call. You will need to change client.rb

https://github.com/hramos/helpscout/blob/2449bc2604667edfca5ed934c8e61cd129b17af5/lib/helpscout/client.rb

 module HelpScout class Client include HTTParty @@last_headers def self.get(*more) response = HTTParty.get(*more) @@last_headers = response.headers return response end def self.last_headers @@last_headers end .... .... end 

So doing HelpScout.last_headers will give you the headers from the last answer, and then you can grab whatever field you need from the same

+5
source share

All Articles