Module: Elmas::Resource::UriMethods

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

Instance Method Summary collapse

Instance Method Details

#apply_filtersObject



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

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

#apply_orderObject



78
79
80
# File 'lib/elmas/uri.rb', line 78

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

#apply_selectObject



82
83
84
# File 'lib/elmas/uri.rb', line 82

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

#base_filter(attribute) ⇒ Object



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

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



6
7
8
# File 'lib/elmas/uri.rb', line 6

def base_path
  Utils.collection_path self.class.name
end

#basic_identifier_uriObject



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

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

#query_attribute(attribute) ⇒ Object

Sanitize an attribute in symbol format to the ExactOnline style



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

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



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/elmas/uri.rb', line 50

def sanitize_value(value)
  case value
  when 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

  when Date, Time
    "datetime'#{value.strftime('%FT%TZ')}'"

  else
    value
  end
end

#uri(options = {}) ⇒ Object



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

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