Class: OData::Service
- Inherits:
-
Object
- Object
- OData::Service
- Defined in:
- lib/odata/service.rb
Overview
Encapsulates the basic details and functionality needed to interact with an OData service.
Instance Attribute Summary collapse
-
#options ⇒ Object
readonly
Options to pass around.
-
#service_url ⇒ Object
readonly
The OData Service’s URL.
Class Method Summary collapse
-
.open(service_url, options = {}) ⇒ OData::Service
Opens the service based on the requested URL and adds the service to Registry.
Instance Method Summary collapse
-
#[](entity_set_name) ⇒ OData::EntitySet
Retrieves the EntitySet associated with a specific EntityType by name.
-
#complex_types ⇒ Object
Returns a list of ComplexTypes used by the service.
-
#entity_sets ⇒ Object
Returns a hash of EntitySet names keyed to their respective EntityType name.
-
#entity_types ⇒ Object
Returns a list of entities exposed by the service.
-
#execute(url_chunk, additional_options = {}) ⇒ Typhoeus::Response
Execute a request against the service.
-
#find_entities(results) ⇒ Nokogiri::XML::NodeSet
Find entity entries in a result set.
-
#find_node(results, node_name) ⇒ Nokogiri::XML::Element
Find a specific node in the given result set.
-
#get_property_type(entity_name, property_name) ⇒ String
Get the property type for an entity from metadata.
-
#get_summary_property_name(entity_name) ⇒ String
Get the property used as the summary for an entity from metadata.
-
#get_title_property_name(entity_name) ⇒ String
Get the property used as the title for an entity from metadata.
-
#initialize(service_url, options = {}) ⇒ OData::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.
-
#name ⇒ String
Returns user supplied name for service, or its URL.
-
#namespace ⇒ Object
Returns the namespace defined on the service’s schema.
-
#primary_key_for(entity_name) ⇒ String
Get the primary key for the supplied Entity.
-
#properties_for_complex_type(type_name) ⇒ Hash
private
Get list of properties and their various options for the supplied ComplexType name.
-
#properties_for_entity(entity_name) ⇒ Hash
private
Get the list of properties and their various options for the supplied Entity name.
Constructor Details
#initialize(service_url, options = {}) ⇒ OData::Service
Opens the service based on the requested URL and adds the service to Registry
16 17 18 19 20 21 |
# File 'lib/odata/service.rb', line 16 def initialize(service_url, = {}) @service_url = service_url @options = .merge() OData::ServiceRegistry.add(self) self end |
Instance Attribute Details
#options ⇒ Object (readonly)
Options to pass around
8 9 10 |
# File 'lib/odata/service.rb', line 8 def @options end |
#service_url ⇒ Object (readonly)
The OData Service’s URL
6 7 8 |
# File 'lib/odata/service.rb', line 6 def service_url @service_url end |
Class Method Details
.open(service_url, options = {}) ⇒ OData::Service
Opens the service based on the requested URL and adds the service to Registry
29 30 31 |
# File 'lib/odata/service.rb', line 29 def self.open(service_url, = {}) Service.new(service_url, ) end |
Instance Method Details
#[](entity_set_name) ⇒ OData::EntitySet
Retrieves the EntitySet associated with a specific EntityType by name
73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/odata/service.rb', line 73 def [](entity_set_name) xpath_query = "//EntityContainer/EntitySet[@Name='#{entity_set_name}']" entity_set_node = .xpath(xpath_query).first raise ArgumentError, "Unknown Entity Set: #{entity_set_name}" if entity_set_node.nil? container_name = entity_set_node.parent.attributes['Name'].value entity_type_name = entity_set_node.attributes['EntityType'].value.gsub(/#{namespace}\./, '') OData::EntitySet.new(name: entity_set_name, namespace: namespace, type: entity_type_name.to_s, service_name: name, container: container_name) end |
#complex_types ⇒ Object
Returns a list of ComplexTypes used by the service
55 56 57 |
# File 'lib/odata/service.rb', line 55 def complex_types @complex_types ||= .xpath('//ComplexType').collect {|entity| entity.attributes['Name'].value} end |
#entity_sets ⇒ Object
Returns a hash of EntitySet names keyed to their respective EntityType name
45 46 47 48 49 50 51 52 |
# File 'lib/odata/service.rb', line 45 def entity_sets @entity_sets ||= Hash[.xpath('//EntityContainer/EntitySet').collect {|entity| [ entity.attributes['EntityType'].value.gsub("#{namespace}.", ''), entity.attributes['Name'].value ] }] end |
#entity_types ⇒ Object
Returns a list of entities exposed by the service
40 41 42 |
# File 'lib/odata/service.rb', line 40 def entity_types @entity_types ||= .xpath('//EntityType').collect {|entity| entity.attributes['Name'].value} end |
#execute(url_chunk, additional_options = {}) ⇒ Typhoeus::Response
Execute a request against the service
91 92 93 94 95 96 97 98 99 100 |
# File 'lib/odata/service.rb', line 91 def execute(url_chunk, = {}) request = ::Typhoeus::Request.new( URI.escape("#{service_url}/#{url_chunk}"), [:typhoeus].merge({ method: :get }).merge() ) request.run request.response end |
#find_entities(results) ⇒ Nokogiri::XML::NodeSet
Find entity entries in a result set
116 117 118 119 120 |
# File 'lib/odata/service.rb', line 116 def find_entities(results) document = ::Nokogiri::XML(results.body) document.remove_namespaces! document.xpath('//entry') end |
#find_node(results, node_name) ⇒ Nokogiri::XML::Element
Find a specific node in the given result set
106 107 108 109 110 |
# File 'lib/odata/service.rb', line 106 def find_node(results, node_name) document = ::Nokogiri::XML(results.body) document.remove_namespaces! document.xpath("//#{node_name}").first end |
#get_property_type(entity_name, property_name) ⇒ String
Get the property type for an entity from metadata.
127 128 129 |
# File 'lib/odata/service.rb', line 127 def get_property_type(entity_name, property_name) .xpath("//EntityType[@Name='#{entity_name}']/Property[@Name='#{property_name}']").first.attributes['Type'].value end |
#get_summary_property_name(entity_name) ⇒ String
Get the property used as the summary for an entity from metadata.
143 144 145 146 147 |
# File 'lib/odata/service.rb', line 143 def get_summary_property_name(entity_name) .xpath("//EntityType[@Name='#{entity_name}']/Property[@FC_TargetPath='SyndicationSummary']").first.attributes['Name'].value rescue NoMethodError nil end |
#get_title_property_name(entity_name) ⇒ String
Get the property used as the title for an entity from metadata.
135 136 137 |
# File 'lib/odata/service.rb', line 135 def get_title_property_name(entity_name) .xpath("//EntityType[@Name='#{entity_name}']/Property[@FC_TargetPath='SyndicationTitle']").first.attributes['Name'].value end |
#inspect ⇒ Object
Returns a more compact inspection of the service object
65 66 67 |
# File 'lib/odata/service.rb', line 65 def inspect "#<#{self.class.name}:#{self.object_id} name='#{name}' service_url='#{self.service_url}'>" end |
#name ⇒ String
Returns user supplied name for service, or its URL
35 36 37 |
# File 'lib/odata/service.rb', line 35 def name @name ||= [:name] || service_url end |
#namespace ⇒ Object
Returns the namespace defined on the service’s schema
60 61 62 |
# File 'lib/odata/service.rb', line 60 def namespace @namespace ||= .xpath('//Schema').first.attributes['Namespace'].value end |
#primary_key_for(entity_name) ⇒ String
Get the primary key for the supplied Entity.
153 154 155 |
# File 'lib/odata/service.rb', line 153 def primary_key_for(entity_name) .xpath("//EntityType[@Name='#{entity_name}']/Key/PropertyRef").first.attributes['Name'].value end |
#properties_for_complex_type(type_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 list of properties and their various options for the supplied ComplexType name.
178 179 180 181 182 183 184 185 186 187 |
# File 'lib/odata/service.rb', line 178 def properties_for_complex_type(type_name) type_definition = .xpath("//ComplexType[@Name='#{type_name}']").first raise ArgumentError, "Unknown ComplexType: #{type_name}" if type_definition.nil? properties_to_return = {} type_definition.xpath('./Property').each do |property_xml| property_name, property = process_property_from_xml(property_xml) properties_to_return[property_name] = property end properties_to_return 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.
162 163 164 165 166 167 168 169 170 171 |
# File 'lib/odata/service.rb', line 162 def properties_for_entity(entity_name) type_definition = .xpath("//EntityType[@Name='#{entity_name}']").first raise ArgumentError, "Unknown EntityType: #{entity_name}" if type_definition.nil? properties_to_return = {} type_definition.xpath('./Property').each do |property_xml| property_name, property = process_property_from_xml(property_xml) properties_to_return[property_name] = property end properties_to_return end |