lib/odata4/service.rb in odata4-0.8.2 vs lib/odata4/service.rb in odata4-0.9.0

- old
+ new

@@ -3,16 +3,18 @@ module OData4 # Encapsulates the basic details and functionality needed to interact with an # OData4 service. class Service + # The Faraday connection object used by the service to make requests + attr_reader :connection # The OData4 Service's URL attr_reader :service_url - # Options to pass around + # Service options attr_reader :options - HTTP_TIMEOUT = 20 + DEFAULT_TIMEOUT = 20 METADATA_TIMEOUTS = [20, 60] MIME_TYPES = { atom: 'application/atom+xml', @@ -22,28 +24,36 @@ } # Opens the service based on the requested URL and adds the service to # {OData4::Registry} # - # @param service_url [String] the URL to the desired OData4 service + # @param service_url [String|Faraday::Connection] + # The URL to the desired OData4 service, or a Faraday connection object # @param options [Hash] options to pass to the service # @return [OData4::Service] an instance of the service - def initialize(service_url, options = {}) - @service_url = service_url - @options = default_options.merge(options) + def initialize(service_url, options = {}, &block) + if service_url.is_a? Faraday::Connection + @connection = service_url + @service_url = connection.url_prefix + else + @service_url = service_url + @connection = Faraday.new(service_url, options[:connection], &block) + end + @options = default_options.merge(options) OData4::ServiceRegistry.add(self) register_custom_types end # Opens the service based on the requested URL and adds the service to # {OData4::Registry} + # @deprecated Use {Service.new} instead. # # @param service_url [String] the URL to the desired OData4 service # @param options [Hash] options to pass to the service # @return [OData4::Service] an instance of the service - def self.open(service_url, options = {}) - Service.new(service_url, options) + def self.open(service_url, options = {}, &block) + Service.new(service_url, options, &block) end # Returns user supplied name for service, or its URL # @return [String] def name @@ -174,37 +184,24 @@ namespace, _, entity_name = entity_name.rpartition('.') raise ArgumentError, 'Namespace missing' if namespace.nil? || namespace.empty? schemas[namespace].properties_for_entity(entity_name) end - # Returns the log level set via initial options, or the - # default log level (`Logger::WARN`) if none was specified. - # @see Logger - # @return [Fixnum|Symbol] - def log_level - options[:log_level] || Logger::WARN - end - # Returns the logger instance used by the service. # When Ruby on Rails has been detected, the service will # use `Rails.logger`. The log level will NOT be changed. # # When no Rails has been detected, a default logger will # be used that logs to STDOUT with the log level supplied # via options, or the default log level if none was given. - # @see #log_level # @return [Logger] def logger - @logger ||= lambda do - if defined?(Rails) - Rails.logger - else - logger = Logger.new(STDOUT) - logger.level = log_level - logger - end - end.call + @logger ||= options[:logger] || if defined?(Rails) + Rails.logger + else + default_logger + end end # Allows the logger to be set to a custom `Logger` instance. # @param custom_logger [Logger] def logger=(custom_logger) @@ -213,15 +210,20 @@ private def default_options { - typhoeus: { - headers: { 'OData-Version' => '4.0' }, - timeout: HTTP_TIMEOUT + request: { + timeout: DEFAULT_TIMEOUT }, strict: true # strict property validation } + end + + def default_logger + Logger.new(STDOUT).tap do |logger| + logger.level = options[:log_level] || Logger::WARN + end end def read_metadata # From file, good for debugging if options[:metadata_file]