Module: Elmas::Resource::UriMethods

Included in:
Elmas::Resource
Defined in:
lib/elmas/uri.rb

Instance Method Summary collapse

Instance Method Details

#apply_filtersObject



60
61
62
63
64
65
# File 'lib/elmas/uri.rb', line 60

def apply_filters
  return unless @filters
  @filters.each do |filter|
    @query << base_filter(filter)
  end
end

#apply_orderObject



71
72
73
# File 'lib/elmas/uri.rb', line 71

def apply_order
  @query << ["$orderby", Utils.camelize(@order_by.to_s)] if @order_by
end

#apply_selectObject



75
76
77
# File 'lib/elmas/uri.rb', line 75

def apply_select
  @query << ["$select", (@select.map { |s| Utils.camelize(s) }.join(","))] if @select
end

#base_filter(attribute) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/elmas/uri.rb', line 21

def base_filter(attribute)
  values = @attributes[attribute]
  values = [values] unless values.is_a?(Array)

  filters = values.map do |value|
    if value.is_a?(Hash)
      value.map do |key, val|
        "#{query_attribute(attribute)} #{key} #{sanitize_value(val)}"
      end
    else
      "#{query_attribute(attribute)} eq #{sanitize_value(value)}"
    end
  end.flatten

  ["$filter", filters.join(" or ")]
end

#base_pathObject



4
5
6
# File 'lib/elmas/uri.rb', line 4

def base_path
  Utils.collection_path self.class.name
end

#basic_identifier_uriObject



67
68
69
# File 'lib/elmas/uri.rb', line 67

def basic_identifier_uri
  "#{base_path}(guid'#{id}')"
end

#query_attribute(attribute) ⇒ Object

Sanitize an attribute in symbol format to the ExactOnline style



39
40
41
42
43
44
45
# File 'lib/elmas/uri.rb', line 39

def query_attribute(attribute)
  if attribute == :id
    attribute.to_s.upcase
  else
    Utils.camelize(attribute)
  end
end

#sanitize_value(value) ⇒ Object

Convert a value to something usable in an ExactOnline request



48
49
50
51
52
53
54
55
56
57
58
# File 'lib/elmas/uri.rb', line 48

def sanitize_value(value)
  if value.is_a?(String)
    if value =~ /[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/
      "guid'#{value}'"
    else
      "'#{value}'"
    end
  else
    value
  end
end

#uri(options = {}) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/elmas/uri.rb', line 8

def uri(options = {})
  options.each do |option|
    send("apply_#{option}".to_sym)
  end
  if options.include?(:id)
    uri = URI("#{base_path}(guid'#{id}')")
  else
    uri = URI(base_path)
  end
  uri.query = URI.encode_www_form(@query)
  uri
end