Class: OData::Service
- Inherits:
-
Object
- Object
- OData::Service
- Defined in:
- lib/odata/service.rb
Instance Attribute Summary collapse
-
#base_url ⇒ Object
readonly
Returns the value of attribute base_url.
-
#metadata ⇒ Object
readonly
Returns the value of attribute metadata.
Instance Method Summary collapse
- #actions ⇒ Object
- #complex_types ⇒ Object
- #delete(path) ⇒ Object
- #entity_set_by_name(name) ⇒ Object
- #entity_sets ⇒ Object
- #entity_types ⇒ Object
- #enum_types ⇒ Object
- #functions ⇒ Object
- #get(path, *select_properties) ⇒ Object
- #get_type_by_name(type_name) ⇒ Object
- #get_type_for_odata_response(parsed_response) ⇒ Object
-
#initialize(options = {}, &block) ⇒ Service
constructor
A new instance of Service.
- #inspect ⇒ Object
- #namespace ⇒ Object
- #navigation_properties_for_type(type_name) ⇒ Object
- #patch(path, data) ⇒ Object
- #populate_primitive_types ⇒ Object
- #post(path, data) ⇒ Object
- #properties_for_type(type_name) ⇒ Object
- #request(options = {}) ⇒ Object
- #singletons ⇒ Object
Constructor Details
#initialize(options = {}, &block) ⇒ Service
Returns a new instance of Service.
6 7 8 9 10 11 12 13 |
# File 'lib/odata/service.rb', line 6 def initialize( = {}, &block) @auth_callback = [:auth_callback] || block @base_url = [:base_url] @metadata_file = [:metadata_file] @type_name_map = {} @metadata = end |
Instance Attribute Details
#base_url ⇒ Object (readonly)
Returns the value of attribute base_url.
3 4 5 |
# File 'lib/odata/service.rb', line 3 def base_url @base_url end |
#metadata ⇒ Object (readonly)
Returns the value of attribute metadata.
4 5 6 |
# File 'lib/odata/service.rb', line 4 def @metadata end |
Instance Method Details
#actions ⇒ Object
109 110 111 112 113 |
# File 'lib/odata/service.rb', line 109 def actions .xpath("//Action").map do |action| build_operation(action) end end |
#complex_types ⇒ Object
72 73 74 75 76 77 78 79 80 |
# File 'lib/odata/service.rb', line 72 def complex_types @complex_types ||= .xpath("//ComplexType").map do |complex_type| @type_name_map["#{namespace}.#{complex_type["Name"]}"] = ComplexType.new( name: "#{namespace}.#{complex_type["Name"]}", base_type: complex_type["BaseType"], service: self, ) end end |
#delete(path) ⇒ Object
43 44 45 46 47 48 |
# File 'lib/odata/service.rb', line 43 def delete(path) request( method: :delete, uri: "#{base_url}#{path}" ) end |
#entity_set_by_name(name) ⇒ Object
174 175 176 |
# File 'lib/odata/service.rb', line 174 def entity_set_by_name(name) entity_sets.find { |entity_set| entity_set.name == name } end |
#entity_sets ⇒ Object
147 148 149 150 151 152 153 154 155 |
# File 'lib/odata/service.rb', line 147 def entity_sets @entity_sets ||= .xpath("//EntitySet").map do |entity_set| EntitySet.new( name: entity_set["Name"], member_type: entity_set["EntityType"], service: self ) end end |
#entity_types ⇒ Object
82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/odata/service.rb', line 82 def entity_types @entity_types ||= .xpath("//EntityType").map do |entity| = { name: "#{namespace}.#{entity["Name"]}", abstract: entity["Abstract"] == "true", base_type: entity["BaseType"], open_type: entity["OpenType"] == "true", has_stream: entity["HasStream"] == "true", service: self, } @type_name_map["#{namespace}.#{entity["Name"]}"] = EntityType.new() end end |
#enum_types ⇒ Object
96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/odata/service.rb', line 96 def enum_types @enum_types ||= .xpath("//EnumType").map do |type| members = type.xpath("./Member").map do |m, i| value = m['Value'] && m['Value'].to_i || i { name: m["Name"], value: value, } end @type_name_map["#{namespace}.#{type["Name"]}"] = EnumType.new({name: "#{namespace}.#{type["Name"]}", members: members}) end end |
#functions ⇒ Object
115 116 117 118 119 |
# File 'lib/odata/service.rb', line 115 def functions .xpath("//Function").map do |function| build_operation(function) end end |
#get(path, *select_properties) ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/odata/service.rb', line 24 def get(path, *select_properties) camel_case_select_properties = select_properties.map do |prop| OData.convert_to_camel_case(prop) end if ! camel_case_select_properties.empty? encoded_select_properties = URI.encode_www_form( '$select' => camel_case_select_properties.join(',') ) path = "#{path}?#{encoded_select_properties}" end response = request( method: :get, uri: "#{base_url}#{path}" ) {type: get_type_for_odata_response(response), attributes: response} end |
#get_type_by_name(type_name) ⇒ Object
170 171 172 |
# File 'lib/odata/service.rb', line 170 def get_type_by_name(type_name) @type_name_map[type_name] || build_collection(type_name) end |
#get_type_for_odata_response(parsed_response) ⇒ Object
157 158 159 160 161 162 163 164 165 166 167 168 |
# File 'lib/odata/service.rb', line 157 def get_type_for_odata_response(parsed_response) if odata_type_string = parsed_response["@odata.type"] get_type_by_name(type_name_from_odata_type_field(odata_type_string)) elsif context = parsed_response["@odata.context"] singular, segments = segments_from_odata_context_field(context) first_entity_type = get_type_by_name("Collection(#{entity_set_by_name(segments.shift).member_type})") entity_type = segments.reduce(first_entity_type) do |last_entity_type, segment| last_entity_type.member_type.(segment).type end singular && entity_type.respond_to?(:member_type) ? entity_type.member_type : entity_type end end |
#inspect ⇒ Object
20 21 22 |
# File 'lib/odata/service.rb', line 20 def inspect "#<#{self.class} #{base_url}>" end |
#namespace ⇒ Object
15 16 17 18 |
# File 'lib/odata/service.rb', line 15 def namespace schema_defintion = .xpath("//Schema") && .xpath("//Schema").first schema_defintion["Namespace"] if schema_defintion end |
#navigation_properties_for_type(type_name) ⇒ Object
191 192 193 194 195 196 197 198 199 200 201 202 203 204 |
# File 'lib/odata/service.rb', line 191 def (type_name) raw_type_name = remove_namespace(type_name) type_definition = .xpath("//EntityType[@Name='#{raw_type_name}']|//ComplexType[@Name='#{raw_type_name}']") type_definition.xpath("./NavigationProperty").map do |property| = { name: property["Name"], nullable: property["Nullable"] != "false", type: get_type_by_name(property["Type"]), contains_target: property["ContainsTarget"], partner: property["Partner"], } OData::NavigationProperty.new() end end |
#patch(path, data) ⇒ Object
58 59 60 61 62 63 64 |
# File 'lib/odata/service.rb', line 58 def patch(path, data) request( method: :patch, uri: "#{base_url}#{path}", data: data ) end |
#populate_primitive_types ⇒ Object
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/odata/service.rb', line 121 def populate_primitive_types @type_name_map.merge!( "Edm.Binary" => OData::BinaryType.new, "Edm.Date" => OData::DateType.new, "Edm.Double" => OData::DoubleType.new, "Edm.Guid" => OData::GuidType.new, "Edm.Int16" => OData::Int16Type.new, "Edm.Int32" => OData::Int32Type.new, "Edm.Int64" => OData::Int64Type.new, "Edm.Stream" => OData::StreamType.new, "Edm.String" => OData::StringType.new, "Edm.Boolean" => OData::BooleanType.new, "Edm.DateTimeOffset" => OData::DateTimeOffsetType.new ) end |
#post(path, data) ⇒ Object
50 51 52 53 54 55 56 |
# File 'lib/odata/service.rb', line 50 def post(path, data) request( method: :post, uri: "#{base_url}#{path}", data: data ) end |
#properties_for_type(type_name) ⇒ Object
178 179 180 181 182 183 184 185 186 187 188 189 |
# File 'lib/odata/service.rb', line 178 def properties_for_type(type_name) raw_type_name = remove_namespace(type_name) type_definition = .xpath("//EntityType[@Name='#{raw_type_name}']|//ComplexType[@Name='#{raw_type_name}']") type_definition.xpath("./Property").map do |property| = { name: property["Name"], nullable: property["Nullable"] != "false", type: get_type_by_name(property["Type"]), } OData::Property.new() end end |