Class: AylienTextApi::Connection
- Inherits:
-
Object
- Object
- AylienTextApi::Connection
- Defined in:
- lib/aylien_text_api/connection.rb
Instance Attribute Summary collapse
-
#rate_limits ⇒ Object
readonly
Returns the value of attribute rate_limits.
Instance Method Summary collapse
-
#initialize(endpoint, params, config) ⇒ Connection
constructor
A new instance of Connection.
- #request ⇒ Object
- #request! ⇒ Object
Constructor Details
#initialize(endpoint, params, config) ⇒ Connection
Returns a new instance of Connection.
24 25 26 27 28 29 |
# File 'lib/aylien_text_api/connection.rb', line 24 def initialize(endpoint, params, config) @config = config @uri = URI.join(@config[:base_uri], endpoint) @params = params compile_request_params end |
Instance Attribute Details
#rate_limits ⇒ Object (readonly)
Returns the value of attribute rate_limits.
22 23 24 |
# File 'lib/aylien_text_api/connection.rb', line 22 def rate_limits @rate_limits end |
Instance Method Details
#request ⇒ Object
31 32 33 34 35 36 37 38 39 40 |
# File 'lib/aylien_text_api/connection.rb', line 31 def request begin request! rescue => e logger = Logger.new(STDOUT) logger.level = Logger::ERROR logger.info(e) nil end end |
#request! ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/aylien_text_api/connection.rb', line 42 def request! Net::HTTP.start(@uri.host, @uri.port, :ENV, use_ssl: (@uri.scheme == 'https')) do |http| response = http.request(@request) limit = response["X-RateLimit-Limit"] remaining = response["X-RateLimit-Remaining"] reset = response["X-RateLimit-Reset"] @rate_limits = { limit: limit ? limit.to_i : nil, remaining: remaining ? remaining.to_i : nil, reset: reset ? reset.to_i : nil } if response.kind_of?(Net::HTTPSuccess) JSON.parse(response.body, :symbolize_names => true) else klass = AylienTextApi::Error::ERRORS[response.code.to_i] error = klass.nil? ? AylienTextApi::Error.new(response.body) : klass.from_response(response) raise error end end end |