Class: OData4::Service

Inherits:
Object
  • Object
show all
Defined in:
lib/odata4/service.rb,
lib/odata4/service/request.rb,
lib/odata4/service/response.rb,
lib/odata4/service/response/xml.rb,
lib/odata4/service/response/atom.rb,
lib/odata4/service/response/json.rb,
lib/odata4/service/response/plain.rb

Overview

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

Defined Under Namespace

Classes: Request, Response

Constant Summary collapse

HTTP_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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(service_url, options = {}) ⇒ OData4::Service

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

Parameters:

  • service_url (String)

    the URL to the desired OData4 service

  • options (Hash) (defaults to: {})

    options to pass to the service



30
31
32
33
34
35
# File 'lib/odata4/service.rb', line 30

def initialize(service_url, options = {})
  @service_url = service_url
  @options = default_options.merge(options)
  OData4::ServiceRegistry.add(self)
  register_custom_types
end

Instance Attribute Details

#optionsObject (readonly)

Options to pass around



11
12
13
# File 'lib/odata4/service.rb', line 11

def options
  @options
end

#service_urlObject (readonly)

The OData4 Service’s URL



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

def service_url
  @service_url
end

Class Method Details

.open(service_url, options = {}) ⇒ OData4::Service

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

Parameters:

  • service_url (String)

    the URL to the desired OData4 service

  • options (Hash) (defaults to: {})

    options to pass to the service

Returns:



43
44
45
# File 'lib/odata4/service.rb', line 43

def self.open(service_url, options = {})
  Service.new(service_url, options)
end

Instance Method Details

#[](entity_set_name) ⇒ OData4::EntitySet

Retrieves the EntitySet associated with a specific EntityType by name

Parameters:

  • entity_set_name (to_s)

    the name of the EntitySet desired

Returns:



92
93
94
# File 'lib/odata4/service.rb', line 92

def [](entity_set_name)
  entity_container[entity_set_name]
end

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

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

Returns:



115
116
117
118
119
120
121
# File 'lib/odata4/service.rb', line 115

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)

Returns:

  • OData4::EntityContainer



78
79
80
# File 'lib/odata4/service.rb', line 78

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

#entity_setsObject

Returns a hash of EntitySet names and their respective EntityType names



84
85
86
# File 'lib/odata4/service.rb', line 84

def entity_sets
  entity_container.entity_sets
end

#entity_typesObject

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



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

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, OData4::Schema::EnumType>

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

Returns:



125
126
127
128
129
130
131
# File 'lib/odata4/service.rb', line 125

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 = {}) ⇒ OData4::Service::Response

Execute a request against the service

Parameters:

  • url_chunk (to_s)

    string to append to service URL

  • options (Hash) (defaults to: {})

    additional request options

Returns:



143
144
145
# File 'lib/odata4/service.rb', line 143

def execute(url_chunk, options = {})
  Request.new(self, url_chunk, options).execute
end

#get_property_type(entity_name, property_name) ⇒ String

Get the property type for an entity from metadata.

Parameters:

  • entity_name (to_s)

    the fully qualified entity name

  • property_name (to_s)

    the property name needed

Returns:

  • (String)

    the name of the property’s type

Raises:

  • (ArgumentError)


152
153
154
155
156
# File 'lib/odata4/service.rb', line 152

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



134
135
136
# File 'lib/odata4/service.rb', line 134

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

#log_levelFixnum|Symbol

Returns the log level set via initial options, or the default log level (‘Logger::WARN`) if none was specified.

Returns:

  • (Fixnum|Symbol)

See Also:

  • Logger


183
184
185
# File 'lib/odata4/service.rb', line 183

def log_level
  options[:log_level] || Logger::WARN
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.

Returns:

  • (Logger)

See Also:



196
197
198
199
200
201
202
203
204
205
206
# File 'lib/odata4/service.rb', line 196

def logger
  @logger ||= lambda do
    if defined?(Rails)
      Rails.logger
    else
      logger = Logger.new(STDOUT)
      logger.level = log_level
      logger
    end
  end.call
end

#logger=(custom_logger) ⇒ Object

Allows the logger to be set to a custom ‘Logger` instance.

Parameters:

  • custom_logger (Logger)


210
211
212
# File 'lib/odata4/service.rb', line 210

def logger=(custom_logger)
  @logger = custom_logger
end

#metadataNokogiri::XML

Returns the service’s metadata definition.

Returns:

  • (Nokogiri::XML)


61
62
63
# File 'lib/odata4/service.rb', line 61

def 
  @metadata ||= lambda {  }.call
end

#metadata_urlString

Returns the service’s metadata URL.

Returns:

  • (String)


55
56
57
# File 'lib/odata4/service.rb', line 55

def 
  "#{service_url}/$metadata"
end

#nameString

Returns user supplied name for service, or its URL

Returns:

  • (String)


49
50
51
# File 'lib/odata4/service.rb', line 49

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.

Returns:

  • (String)


99
100
101
# File 'lib/odata4/service.rb', line 99

def namespace
  entity_container.namespace
end

#primary_key_for(entity_name) ⇒ String

Get the primary key for the supplied Entity.

Parameters:

  • entity_name (to_s)

    The fully qualified entity name

Returns:

  • (String)

Raises:

  • (ArgumentError)


162
163
164
165
166
# File 'lib/odata4/service.rb', line 162

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.

Parameters:

  • entity_name (to_s)

Returns:

  • (Hash)

Raises:

  • (ArgumentError)


173
174
175
176
177
# File 'lib/odata4/service.rb', line 173

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.



67
68
69
70
71
72
73
74
# File 'lib/odata4/service.rb', line 67

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