lib/ably/rest/client.rb in ably-0.1.4 vs lib/ably/rest/client.rb in ably-0.1.5

- old
+ new

@@ -1,10 +1,10 @@ -require "json" -require "faraday" +require 'faraday' +require 'json' +require 'logger' -require "ably/rest/middleware/exceptions" -require "ably/rest/middleware/parse_json" +require 'ably/rest/middleware/exceptions' module Ably module Rest # Client for the Ably REST API # @@ -23,21 +23,22 @@ include Ably::Modules::HttpHelpers extend Forwardable DOMAIN = "rest.ably.io" - attr_reader :tls, :environment, :auth, :channels + attr_reader :tls, :environment, :protocol, :auth, :channels, :log_level def_delegators :auth, :client_id, :auth_options # Creates a {Ably::Rest::Client Rest Client} and configures the {Ably::Auth} object for the connection. # # @param [Hash,String] options an options Hash used to configure the client and the authentication, or String with an API key # @option options (see Ably::Auth#authorise) - # @option options [Boolean] :tls TLS is used by default, providing a value of false disbles TLS. Please note Basic Auth is disallowed without TLS as secrets cannot be transmitted over unsecured connections. - # @option options [String] :api_key API key comprising the key ID and key secret in a single string - # @option options [String] :environment Specify 'sandbox' when testing the client library against an alternate Ably environment - # @option options [Boolean] :debug_http Send HTTP debugging information from Faraday for all HTTP requests to STDOUT + # @option options [Boolean] :tls TLS is used by default, providing a value of false disbles TLS. Please note Basic Auth is disallowed without TLS as secrets cannot be transmitted over unsecured connections. + # @option options [String] :api_key API key comprising the key ID and key secret in a single string + # @option options [String] :environment Specify 'sandbox' when testing the client library against an alternate Ably environment + # @option options [Symbol] :protocol Protocol used to communicate with Ably, :json and :msgpack currently supported. Defaults to :msgpack. + # @option options [Logger::Severity] :log_level Log level for the standard Logger that outputs to STDOUT. Defaults to Logger::WARN, can be set to Logger::FATAL, Logger::ERROR, Logger::WARN, Logger::INFO, Logger::DEBUG # # @yield (see Ably::Auth#authorise) # @yieldparam (see Ably::Auth#authorise) # @yieldreturn (see Ably::Auth#authorise) # @@ -57,12 +58,16 @@ options = { api_key: options } end @tls = options.delete(:tls) == false ? false : true @environment = options.delete(:environment) # nil is production + @protocol = options.delete(:protocol) || :json # TODO: Default to :msgpack when protocol MsgPack support added @debug_http = options.delete(:debug_http) + @log_level = options.delete(:log_level) || Logger::WARN + raise ArgumentError, 'Protocol is invalid. Must be either :msgpack or :json' unless [:msgpack, :json].include?(@protocol) + @auth = Auth.new(self, options, &auth_block) @channels = Ably::Rest::Channels.new(self) end # Return a REST {Ably::Rest::Channel} for the given name @@ -83,11 +88,13 @@ :by => :minute } response = get("/stats", default_params.merge(params)) - response.body + response.body.map do |stat| + IdiomaticRubyWrapper(stat) + end end # Return the Ably service time # # @return [Time] The time as reported by the Ably service @@ -126,15 +133,26 @@ scheme: use_tls? ? "https" : "http", host: [@environment, DOMAIN].compact.join('-') ) end - # When true, will send HTTP debugging information from Faraday for all HTTP requests to STDOUT + def logger + @logger ||= Logger.new(STDOUT).tap do |logger| + logger.level = log_level + end + end + + # Mime type used for HTTP requests # - # @return [Boolean] - def debug_http? - !!@debug_http + # @return [String] + def mime_type + case protocol + when :json + 'application/json' + else + 'application/x-msgpack' + end end private def request(method, path, params = {}, options = {}) reauthorise_on_authorisation_failure do @@ -173,11 +191,11 @@ # @return [Hash] def connection_options @connection_options ||= { builder: middleware, headers: { - accept: "application/json", + accept: mime_type, user_agent: user_agent }, request: { open_timeout: 5, timeout: 10 @@ -188,20 +206,18 @@ # Return a Faraday middleware stack to initiate the Faraday::Connection with # # @see http://mislav.uniqpath.com/2011/07/faraday-advanced-http/ def middleware @middleware ||= Faraday::RackBuilder.new do |builder| - # Convert request params to "www-form-urlencoded" - builder.use Faraday::Request::UrlEncoded + setup_middleware builder - # Parse JSON response bodies - builder.use Ably::Rest::Middleware::ParseJson - - # Log HTTP requests if debug_http option set - builder.response :logger if @debug_http - # Raise exceptions if response code is invalid builder.use Ably::Rest::Middleware::Exceptions + + + # Log HTTP requests if log level is DEBUG option set + builder.response :logger if log_level == Logger::DEBUG + # Set Faraday's HTTP adapter builder.adapter Faraday.default_adapter end end