Class: Frodo::Service
- Inherits:
-
Object
- Object
- Frodo::Service
- Defined in:
- lib/frodo/service.rb
Overview
Encapsulates the basic details and functionality needed to interact with an Frodo service.
Instance Attribute Summary collapse
-
#options ⇒ Object
readonly
Service options.
-
#service_url ⇒ Object
readonly
The Frodo Service’s URL.
Instance Method Summary collapse
-
#[](entity_set_name) ⇒ Frodo::EntitySet
Retrieves the EntitySet associated with a specific EntityType by name.
-
#complex_types ⇒ Hash<String, Frodo::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, Frodo::Schema::EnumType>
Returns a list of ‘EnumType`s used by the service.
-
#get_property_type(entity_name, property_name) ⇒ String
Get the property type for an entity from metadata.
-
#initialize(service_url, options = {}, &block) ⇒ Frodo::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.
-
#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.
- #with_metadata? ⇒ Boolean
Constructor Details
#initialize(service_url, options = {}, &block) ⇒ Frodo::Service
Opens the service based on the requested URL and adds the service to Registry
18 19 20 21 22 23 24 |
# File 'lib/frodo/service.rb', line 18 def initialize(service_url, = {}, &block) @options = .merge() @service_url = service_url Frodo::ServiceRegistry.add(self) register_custom_types if @options[:with_metadata] end |
Instance Attribute Details
#options ⇒ Object (readonly)
Service options
8 9 10 |
# File 'lib/frodo/service.rb', line 8 def @options end |
#service_url ⇒ Object (readonly)
The Frodo Service’s URL
6 7 8 |
# File 'lib/frodo/service.rb', line 6 def service_url @service_url end |
Instance Method Details
#[](entity_set_name) ⇒ Frodo::EntitySet
Retrieves the EntitySet associated with a specific EntityType by name
71 72 73 74 75 76 77 |
# File 'lib/frodo/service.rb', line 71 def [](entity_set_name) if entity_container[entity_set_name] else EntitySet.new(name: entity_set_name) end end |
#complex_types ⇒ Hash<String, Frodo::Schema::ComplexType>
Returns a list of ‘ComplexType`s used by the service.
98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/frodo/service.rb', line 98 def complex_types @complex_types ||= schemas.map do |namespace, schema| schema_hash = {} schema.complex_types.map do |name, complex_type| schema_hash["#{namespace}.#{name}"] = complex_type if schema.alias schema_hash["#{schema.alias}.#{name}"] = complex_type end end schema_hash end.reduce({}, :merge) end |
#entity_container ⇒ Object
Returns the service’s EntityContainer (singleton)
57 58 59 |
# File 'lib/frodo/service.rb', line 57 def entity_container @entity_container ||= EntityContainer.new(self) end |
#entity_sets ⇒ Object
Returns a hash of EntitySet names and their respective EntityType names
63 64 65 |
# File 'lib/frodo/service.rb', line 63 def entity_sets entity_container.entity_sets end |
#entity_types ⇒ Object
Returns a list of ‘EntityType`s exposed by the service
88 89 90 91 92 93 94 |
# File 'lib/frodo/service.rb', line 88 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, Frodo::Schema::EnumType>
Returns a list of ‘EnumType`s used by the service
113 114 115 116 117 118 119 |
# File 'lib/frodo/service.rb', line 113 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 |
#get_property_type(entity_name, property_name) ⇒ String
Get the property type for an entity from metadata.
131 132 133 134 135 |
# File 'lib/frodo/service.rb', line 131 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
122 123 124 |
# File 'lib/frodo/service.rb', line 122 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.
166 167 168 169 170 171 172 |
# File 'lib/frodo/service.rb', line 166 def logger @logger ||= [:logger] || if defined?(Rails) Rails.logger else default_logger end end |
#metadata ⇒ Nokogiri::XML
Returns the service’s metadata definition.
40 41 42 |
# File 'lib/frodo/service.rb', line 40 def @metadata ||= lambda { }.call end |
#metadata_url ⇒ String
Returns the service’s metadata URL.
34 35 36 |
# File 'lib/frodo/service.rb', line 34 def "#{service_url}/$metadata" end |
#name ⇒ String
Returns user supplied name for service, or its URL
28 29 30 |
# File 'lib/frodo/service.rb', line 28 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.
82 83 84 |
# File 'lib/frodo/service.rb', line 82 def namespace entity_container.namespace end |
#primary_key_for(entity_name) ⇒ String
Get the primary key for the supplied Entity.
141 142 143 144 145 |
# File 'lib/frodo/service.rb', line 141 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.
152 153 154 155 156 |
# File 'lib/frodo/service.rb', line 152 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 |
#schemas ⇒ Object
Returns all of the service’s schemas.
46 47 48 49 50 51 52 53 |
# File 'lib/frodo/service.rb', line 46 def schemas @schemas ||= .xpath('//Schema').map do |schema_xml| [ schema_xml.attributes['Namespace'].value, Schema.new(schema_xml, self, [:navigation_properties]) ] end.to_h end |
#with_metadata? ⇒ Boolean
174 175 176 |
# File 'lib/frodo/service.rb', line 174 def !@options.key?(:with_metadata) || @options[:with_metadata] end |