Class: Frodo::Service

Inherits:
Object
  • Object
show all
Defined in:
lib/frodo/service.rb

Overview

Encapsulates the basic details and functionality needed to interact with an Frodo service.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(service_url, options = {}, &block) ⇒ Frodo::Service

Opens the service based on the requested URL and adds the service to Registry



19
20
21
22
23
24
25
# File 'lib/frodo/service.rb', line 19

def initialize(service_url, options = {}, &block)
  @options = default_options.merge(options)
  @service_url = service_url

  Frodo::ServiceRegistry.add(self)
  register_custom_types
end

Instance Attribute Details

#optionsObject (readonly)

Service options



9
10
11
# File 'lib/frodo/service.rb', line 9

def options
  @options
end

#service_urlObject (readonly)

The Frodo Service’s URL



7
8
9
# File 'lib/frodo/service.rb', line 7

def service_url
  @service_url
end

Instance Method Details

#[](entity_set_name) ⇒ Frodo::EntitySet

Retrieves the EntitySet associated with a specific EntityType by name



72
73
74
# File 'lib/frodo/service.rb', line 72

def [](entity_set_name)
  entity_container[entity_set_name]
end

#complex_typesHash<String, Frodo::Schema::ComplexType>

Returns a list of ‘ComplexType`s used by the service.



95
96
97
98
99
100
101
# File 'lib/frodo/service.rb', line 95

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_containerObject

Returns the service’s EntityContainer (singleton)



58
59
60
# File 'lib/frodo/service.rb', line 58

def entity_container
  @entity_container ||= EntityContainer.new(self)
end

#entity_setsObject

Returns a hash of EntitySet names and their respective EntityType names



64
65
66
# File 'lib/frodo/service.rb', line 64

def entity_sets
  entity_container.entity_sets
end

#entity_typesObject

Returns a list of ‘EntityType`s exposed by the service



85
86
87
88
89
90
91
# File 'lib/frodo/service.rb', line 85

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_typesHash<String, Frodo::Schema::EnumType>

Returns a list of ‘EnumType`s used by the service



105
106
107
108
109
110
111
# File 'lib/frodo/service.rb', line 105

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.

Raises:

  • (ArgumentError)


123
124
125
126
127
# File 'lib/frodo/service.rb', line 123

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

#inspectObject

Returns a more compact inspection of the service object



114
115
116
# File 'lib/frodo/service.rb', line 114

def inspect
  "#<#{self.class.name}:#{self.object_id} name='#{name}' service_url='#{self.service_url}'>"
end

#loggerLogger

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.



158
159
160
161
162
163
164
# File 'lib/frodo/service.rb', line 158

def logger
  @logger ||= options[:logger] || if defined?(Rails)
    Rails.logger
  else
    default_logger
  end
end

#metadataNokogiri::XML

Returns the service’s metadata definition.



41
42
43
# File 'lib/frodo/service.rb', line 41

def 
   ||= lambda {  }.call
end

#metadata_urlString

Returns the service’s metadata URL.



35
36
37
# File 'lib/frodo/service.rb', line 35

def 
  "#{service_url}/$metadata"
end

#nameString

Returns user supplied name for service, or its URL



29
30
31
# File 'lib/frodo/service.rb', line 29

def name
  @name ||= options[:name] || service_url
end

#namespaceString

Returns the default namespace, that is, the namespace of the schema that contains the service’s EntityContainer.



79
80
81
# File 'lib/frodo/service.rb', line 79

def namespace
  entity_container.namespace
end

#primary_key_for(entity_name) ⇒ String

Get the primary key for the supplied Entity.

Raises:

  • (ArgumentError)


133
134
135
136
137
# File 'lib/frodo/service.rb', line 133

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.

Raises:

  • (ArgumentError)


144
145
146
147
148
# File 'lib/frodo/service.rb', line 144

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

#schemasObject

Returns all of the service’s schemas.



47
48
49
50
51
52
53
54
# File 'lib/frodo/service.rb', line 47

def schemas
  @schemas ||= .xpath('//Schema').map do |schema_xml|
    [
      schema_xml.attributes['Namespace'].value,
      Schema.new(schema_xml, self)
    ]
  end.to_h
end