Class: FrOData::Service
- Inherits:
-
Object
- Object
- FrOData::Service
- Defined in:
- lib/frodata/service.rb,
lib/frodata/service/request.rb,
lib/frodata/service/response.rb,
lib/frodata/service/response/xml.rb,
lib/frodata/service/response/atom.rb,
lib/frodata/service/response/json.rb,
lib/frodata/service/response/plain.rb
Overview
Encapsulates the basic details and functionality needed to interact with an FrOData service.
Defined Under Namespace
Constant Summary collapse
- DEFAULT_TIMEOUT =
20
- METADATA_TIMEOUTS =
[20, 60]
- MIME_TYPES =
{ atom: 'application/atom+xml', json: 'application/json', xml: 'application/xml', plain: 'text/plain' }
Instance Attribute Summary collapse
-
#connection ⇒ Object
readonly
The Faraday connection object used by the service to make requests.
-
#options ⇒ Object
readonly
Service options.
-
#service_url ⇒ Object
readonly
The FrOData Service’s URL.
Class Method Summary collapse
-
.open(service_url, options = {}, &block) ⇒ FrOData::Service
deprecated
Deprecated.
Use Service.new instead.
Instance Method Summary collapse
-
#[](entity_set_name) ⇒ FrOData::EntitySet
Retrieves the EntitySet associated with a specific EntityType by name.
-
#complex_types ⇒ Hash<String, FrOData::Schema::ComplexType>
Returns a list of ‘ComplexType`s used by the service.
-
#entity_container ⇒ Object
Returns the service’s EntityContainer (singleton).
-
#entity_sets ⇒ Object
Returns a hash of EntitySet names and their respective EntityType names.
-
#entity_types ⇒ Object
Returns a list of ‘EntityType`s exposed by the service.
-
#enum_types ⇒ Hash<String, FrOData::Schema::EnumType>
Returns a list of ‘EnumType`s used by the service.
-
#execute(url_chunk, options = {}) ⇒ FrOData::Service::Response
Execute a request against the service.
-
#get_property_type(entity_name, property_name) ⇒ String
Get the property type for an entity from metadata.
-
#initialize(service_url, options = {}, &block) ⇒ FrOData::Service
constructor
Opens the service based on the requested URL and adds the service to Registry.
-
#inspect ⇒ Object
Returns a more compact inspection of the service object.
-
#logger ⇒ Logger
Returns the logger instance used by the service.
-
#logger=(custom_logger) ⇒ Object
Allows the logger to be set to a custom ‘Logger` instance.
-
#metadata ⇒ Nokogiri::XML
Returns the service’s metadata definition.
-
#metadata_url ⇒ String
Returns the service’s metadata URL.
-
#name ⇒ String
Returns user supplied name for service, or its URL.
-
#namespace ⇒ String
Returns the default namespace, that is, the namespace of the schema that contains the service’s EntityContainer.
-
#primary_key_for(entity_name) ⇒ String
Get the primary key for the supplied Entity.
-
#properties_for_entity(entity_name) ⇒ Hash
private
Get the list of properties and their various options for the supplied Entity name.
-
#schemas ⇒ Object
Returns all of the service’s schemas.
Constructor Details
#initialize(service_url, options = {}, &block) ⇒ FrOData::Service
Opens the service based on the requested URL and adds the service to Registry
33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/frodata/service.rb', line 33 def initialize(service_url, = {}, &block) @options = .merge() if service_url.is_a? Faraday::Connection @connection = service_url @service_url = connection.url_prefix.to_s else @service_url = service_url @connection = default_connection(&block) end FrOData::ServiceRegistry.add(self) register_custom_types end |
Instance Attribute Details
#connection ⇒ Object (readonly)
The Faraday connection object used by the service to make requests
9 10 11 |
# File 'lib/frodata/service.rb', line 9 def connection @connection end |
#options ⇒ Object (readonly)
Service options
13 14 15 |
# File 'lib/frodata/service.rb', line 13 def @options end |
#service_url ⇒ Object (readonly)
The FrOData Service’s URL
11 12 13 |
# File 'lib/frodata/service.rb', line 11 def service_url @service_url end |
Class Method Details
.open(service_url, options = {}, &block) ⇒ FrOData::Service
Use Service.new instead.
Opens the service based on the requested URL and adds the service to Registry
53 54 55 |
# File 'lib/frodata/service.rb', line 53 def self.open(service_url, = {}, &block) Service.new(service_url, , &block) end |
Instance Method Details
#[](entity_set_name) ⇒ FrOData::EntitySet
Retrieves the EntitySet associated with a specific EntityType by name
102 103 104 |
# File 'lib/frodata/service.rb', line 102 def [](entity_set_name) entity_container[entity_set_name] end |
#complex_types ⇒ Hash<String, FrOData::Schema::ComplexType>
Returns a list of ‘ComplexType`s used by the service.
125 126 127 128 129 130 131 |
# File 'lib/frodata/service.rb', line 125 def complex_types @complex_types ||= schemas.map do |namespace, schema| schema.complex_types.map do |name, complex_type| [ "#{namespace}.#{name}", complex_type ] end.to_h end.reduce({}, :merge) end |
#entity_container ⇒ Object
Returns the service’s EntityContainer (singleton)
88 89 90 |
# File 'lib/frodata/service.rb', line 88 def entity_container @entity_container ||= EntityContainer.new(self) end |
#entity_sets ⇒ Object
Returns a hash of EntitySet names and their respective EntityType names
94 95 96 |
# File 'lib/frodata/service.rb', line 94 def entity_sets entity_container.entity_sets end |
#entity_types ⇒ Object
Returns a list of ‘EntityType`s exposed by the service
115 116 117 118 119 120 121 |
# File 'lib/frodata/service.rb', line 115 def entity_types @entity_types ||= schemas.map do |namespace, schema| schema.entity_types.map do |entity_type| "#{namespace}.#{entity_type}" end end.flatten end |
#enum_types ⇒ Hash<String, FrOData::Schema::EnumType>
Returns a list of ‘EnumType`s used by the service
135 136 137 138 139 140 141 |
# File 'lib/frodata/service.rb', line 135 def enum_types @enum_types ||= schemas.map do |namespace, schema| schema.enum_types.map do |name, enum_type| [ "#{namespace}.#{name}", enum_type ] end.to_h end.reduce({}, :merge) end |
#execute(url_chunk, options = {}) ⇒ FrOData::Service::Response
Execute a request against the service
153 154 155 156 |
# File 'lib/frodata/service.rb', line 153 def execute(url_chunk, = {}) = (@options[:request] || {}).merge() Request.new(self, url_chunk, ).execute end |
#get_property_type(entity_name, property_name) ⇒ String
Get the property type for an entity from metadata.
163 164 165 166 167 |
# File 'lib/frodata/service.rb', line 163 def get_property_type(entity_name, property_name) namespace, _, entity_name = entity_name.rpartition('.') raise ArgumentError, 'Namespace missing' if namespace.nil? || namespace.empty? schemas[namespace].get_property_type(entity_name, property_name) end |
#inspect ⇒ Object
Returns a more compact inspection of the service object
144 145 146 |
# File 'lib/frodata/service.rb', line 144 def inspect "#<#{self.class.name}:#{self.object_id} name='#{name}' service_url='#{self.service_url}'>" end |
#logger ⇒ Logger
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.
198 199 200 201 202 203 204 |
# File 'lib/frodata/service.rb', line 198 def logger @logger ||= [:logger] || if defined?(Rails) Rails.logger else default_logger end end |
#logger=(custom_logger) ⇒ Object
Allows the logger to be set to a custom ‘Logger` instance.
208 209 210 |
# File 'lib/frodata/service.rb', line 208 def logger=(custom_logger) @logger = custom_logger end |
#metadata ⇒ Nokogiri::XML
Returns the service’s metadata definition.
71 72 73 |
# File 'lib/frodata/service.rb', line 71 def @metadata ||= lambda { }.call end |
#metadata_url ⇒ String
Returns the service’s metadata URL.
65 66 67 |
# File 'lib/frodata/service.rb', line 65 def "#{service_url}/$metadata" end |
#name ⇒ String
Returns user supplied name for service, or its URL
59 60 61 |
# File 'lib/frodata/service.rb', line 59 def name @name ||= [:name] || service_url end |
#namespace ⇒ String
Returns the default namespace, that is, the namespace of the schema that contains the service’s EntityContainer.
109 110 111 |
# File 'lib/frodata/service.rb', line 109 def namespace entity_container.namespace end |
#primary_key_for(entity_name) ⇒ String
Get the primary key for the supplied Entity.
173 174 175 176 177 |
# File 'lib/frodata/service.rb', line 173 def primary_key_for(entity_name) namespace, _, entity_name = entity_name.rpartition('.') raise ArgumentError, 'Namespace missing' if namespace.nil? || namespace.empty? schemas[namespace].primary_key_for(entity_name) end |
#properties_for_entity(entity_name) ⇒ Hash
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Get the list of properties and their various options for the supplied Entity name.
184 185 186 187 188 |
# File 'lib/frodata/service.rb', line 184 def properties_for_entity(entity_name) namespace, _, entity_name = entity_name.rpartition('.') raise ArgumentError, 'Namespace missing' if namespace.nil? || namespace.empty? schemas[namespace].properties_for_entity(entity_name) end |