Module: Elmas::Resource::UriMethods
- Included in:
- Elmas::Resource
- Defined in:
- lib/elmas/uri.rb
Instance Method Summary collapse
- #apply_filters ⇒ Object
- #apply_order ⇒ Object
- #apply_select ⇒ Object
- #base_filter(attribute) ⇒ Object
- #base_path ⇒ Object
- #basic_identifier_uri ⇒ Object
-
#query_attribute(attribute) ⇒ Object
Sanitize an attribute in symbol format to the ExactOnline style.
-
#sanitize_value(value) ⇒ Object
Convert a value to something usable in an ExactOnline request.
- #uri(options = {}) ⇒ Object
Instance Method Details
#apply_filters ⇒ Object
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_order ⇒ Object
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_select ⇒ Object
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_path ⇒ Object
6 7 8 |
# File 'lib/elmas/uri.rb', line 6 def base_path Utils.collection_path self.class.name end |
#basic_identifier_uri ⇒ Object
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( = {}) .each do |option| send("apply_#{option}".to_sym) end uri = if .include?(:id) URI("#{base_path}(guid'#{id}')") else URI(base_path) end uri.query = URI.encode_www_form(@query) uri end |