Class: OData::Service

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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


6
7
8
9
10
11
12
13
14
15
16
# File 'lib/odata/service.rb', line 6

def initialize(options = {}, &block)
  @api_version   = {
    'api-version': options[:api_version]
  } if options[:api_version]
  @auth_callback = options[:auth_callback] || block
  @base_url      = options[:base_url]
  @metadata_file = options[:metadata_file]
  @type_name_map = {}
  @metadata      = 
  
end

Instance Attribute Details

#base_urlObject (readonly)

Returns the value of attribute base_url


3
4
5
# File 'lib/odata/service.rb', line 3

def base_url
  @base_url
end

#metadataObject (readonly)

Returns the value of attribute metadata


4
5
6
# File 'lib/odata/service.rb', line 4

def 
  @metadata
end

Instance Method Details

#actionsObject


122
123
124
125
126
# File 'lib/odata/service.rb', line 122

def actions
  .xpath("//Action").map do |action|
    build_operation(action)
  end
end

#complex_typesObject


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

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


46
47
48
49
50
51
# File 'lib/odata/service.rb', line 46

def delete(path)
  request(
    method: :delete,
    uri: "#{base_url}#{path}"
  )
end

#entity_set_by_name(name) ⇒ Object


187
188
189
# File 'lib/odata/service.rb', line 187

def entity_set_by_name(name)
  entity_sets.find { |entity_set| entity_set.name == name }
end

#entity_setsObject


160
161
162
163
164
165
166
167
168
# File 'lib/odata/service.rb', line 160

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_typesObject


95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/odata/service.rb', line 95

def entity_types
  @entity_types ||= .xpath("//EntityType").map do |entity|
    options = {
      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(options)
  end
end

#enum_typesObject


109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/odata/service.rb', line 109

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

#functionsObject


128
129
130
131
132
# File 'lib/odata/service.rb', line 128

def functions
  .xpath("//Function").map do |function|
    build_operation(function)
  end
end

#get(path, *select_properties) ⇒ Object


27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/odata/service.rb', line 27

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


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

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


170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/odata/service.rb', line 170

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.navigation_property_by_name(segment).type
    end
    singular && entity_type.respond_to?(:member_type) ? entity_type.member_type : entity_type
  end
end

#inspectObject


23
24
25
# File 'lib/odata/service.rb', line 23

def inspect
  "#<#{self.class} #{base_url}>"
end

#namespaceObject


18
19
20
21
# File 'lib/odata/service.rb', line 18

def namespace
  schema_defintion = .xpath("//Schema") && .xpath("//Schema").first
  schema_defintion["Namespace"] if schema_defintion
end

204
205
206
207
208
209
210
211
212
213
214
215
216
217
# File 'lib/odata/service.rb', line 204

def navigation_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("./NavigationProperty").map do |property|
    options = {
      name:            property["Name"],
      nullable:        property["Nullable"] != "false",
      type:            get_type_by_name(property["Type"]),
      contains_target: property["ContainsTarget"],
      partner:         property["Partner"],
    }
    OData::NavigationProperty.new(options)
  end
end

#patch(path, data) ⇒ Object


61
62
63
64
65
66
67
# File 'lib/odata/service.rb', line 61

def patch(path, data)
  request(
    method: :patch,
    uri: "#{base_url}#{path}",
    data: data
  )
end

#populate_primitive_typesObject


134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/odata/service.rb', line 134

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


53
54
55
56
57
58
59
# File 'lib/odata/service.rb', line 53

def post(path, data)
  request(
    method: :post,
    uri: "#{base_url}#{path}",
    data: data
  )
end

#properties_for_type(type_name) ⇒ Object


191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/odata/service.rb', line 191

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|
    options = {
      name:      property["Name"],
      nullable:  property["Nullable"] != "false",
      type:      get_type_by_name(property["Type"]),
    }
    OData::Property.new(options)
  end
end

#request(options = {}) ⇒ Object


69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/odata/service.rb', line 69

def request(options = {})
  uri = options[:uri]

  if @api_version then
    parsed_uri = URI(uri)
    params = URI.decode_www_form(parsed_uri.query || '')
                .concat(@api_version.to_a)
    parsed_uri.query = URI.encode_www_form params
    uri = parsed_uri.to_s
  end

  req = Request.new(options[:method], uri, options[:data])
  @auth_callback.call(req) if @auth_callback
  req.perform
end

#singletonsObject


150
151
152
153
154
155
156
157
158
# File 'lib/odata/service.rb', line 150

def singletons
  .xpath("//Singleton").map do |singleton|
    Singleton.new(
      name:    singleton["Name"],
      type:    singleton["Type"],
      service: self
    )
  end
end