module Xeroizer::Record::BaseModelHttpProxy::InstanceMethods

Protected Instance Methods

extract_expression_from_attribute_name(key) click to toggle source

Extract the attribute name and expression from the attribute.

@return [Array] containing [actual_attribute_name, expression]

# File lib/xeroizer/record/base_model_http_proxy.rb, line 69
def extract_expression_from_attribute_name(key)
  case key.to_s
    when /(_is_not|\<\>)$/
      [
        key.to_s.gsub(/(_is_not|\<\>)$/, '').to_sym,
        '<>'
      ]

    when /(_is_greater_than|\>)$/
      [
        key.to_s.gsub(/(_is_greater_than|\>)$/, '').to_sym,
        '>'
      ]

    when /(_is_greater_than_or_equal_to|\>\=)$/
      [
        key.to_s.gsub(/(_is_greater_than_or_equal_to|\>\=)$/, '').to_sym,
        '>='
      ]

    when /(_is_less_than|\<)$/
      [
        key.to_s.gsub(/(_is_less_than|\<)$/, '').to_sym,
        '<'
      ]

    when /(_is_less_than_or_equal_to|\<\=)$/
      [
        key.to_s.gsub(/(_is_less_than_or_equal_to|\<\=)$/, '').to_sym,
        '<='
      ]

    else
      [key, '==']

  end
end
parse_params(options) click to toggle source

Parse parameters for GET requests.

# File lib/xeroizer/record/base_model_http_proxy.rb, line 17
def parse_params(options)
  params = {}
  params[:ModifiedAfter]  = options[:modified_since] if options[:modified_since]
  params[:includeArchived]  = options[:include_archived] if options[:include_archived]
  params[:order]        = options[:order] if options[:order]
  params[:createdByMyApp] = options[:createdByMyApp] if options[:createdByMyApp]

  params[:IDs]            = filterize(options[:IDs]) if options[:IDs]
  params[:InvoiceNumbers] = filterize(options[:InvoiceNumbers]) if options[:InvoiceNumbers]
  params[:ContactIDs]     = filterize(options[:ContactIDs]) if options[:ContactIDs]
  params[:Statuses]       = filterize(options[:Statuses]) if options[:Statuses]

  if options[:where]
    params[:where] =  case options[:where]
                        when String   then options[:where]
                        when Hash     then parse_where_hash(options[:where])
                      end
  end
  params[:offset] = options[:offset] if options[:offset]
  params[:Status] = options[:status] if options[:status]
  params[:DateFrom] = options[:date_from] if options[:date_from]
  params[:DateTo] = options[:date_to] if options[:date_to]
  params[:page] = options[:page] if options[:page]
  params
end
parse_where_hash(where) click to toggle source

Parse the :where part of the options for GET parameters and construct a valid .Net version of the criteria to pass to Xero.

Attribute names can be modified as follows to change the expression used:

{attribute_name}_is_greater_than or {attribute_name}> uses '>'
{attribute_name}_is_greater_than_or_equal_to or {attribute_name}>= uses '>='
{attribute_name}_is_less_than or {attribute_name}< uses '<'
{attribute_name}_is_less_than_or_equal_to or {attribute_name}< uses '<='
DEFAULT: '=='
# File lib/xeroizer/record/base_model_http_proxy.rb, line 52
def parse_where_hash(where)
  conditions = []
  where.each do | key, value |
    (attribute_name, expression) = extract_expression_from_attribute_name(key)
    (_, field) = model_class.fields.find { | k, v | v[:internal_name] == attribute_name }
    if field
      conditions << where_condition_part(field, expression, value)
    else
      raise InvalidAttributeInWhere.new(model_name, attribute_name)
    end
  end
  conditions.map { | (attr, expression, value) | "#{attr}#{expression}#{value}"}.join('&&')
end
where_condition_part(field, expression, value) click to toggle source

Creates a condition part array containing the:

* Field's API name
* Expression
* .Net formatted value.
# File lib/xeroizer/record/base_model_http_proxy.rb, line 111
def where_condition_part(field, expression, value)
  case field[:type]
    when :guid        then ["#{field[:api_name]}.ToString()", expression, "\"#{value}\""]
    when :string      then [field[:api_name], expression, "\"#{value}\""]
    when :boolean     then [field[:api_name], expression, value ? 'true' : 'false']
    when :integer     then [field[:api_name], expression, value.to_s]
    when :decimal     then [field[:api_name], expression, value.to_s]
    when :date        then [field[:api_name], expression, "DateTime.Parse(\"#{value.strftime("%Y-%m-%d")}\")"]
    when :datetime    then [field[:api_name], expression, "DateTime.Parse(\"#{value.utc.strftime("%Y-%m-%dT%H:%M:%S")}\")"]
    when :datetime_utc then [field[:api_name], expression, "DateTime.Parse(\"#{value.utc.strftime("%Y-%m-%dT%H:%M:%S")}\")"]
    when :belongs_to  then
    when :has_many    then
  end
end

Private Instance Methods

filterize(values) click to toggle source

Filtering params expect a comma separated list of strings

# File lib/xeroizer/record/base_model_http_proxy.rb, line 129
def filterize(values)
  case values
    when String then values
    when Array  then values.join(',')
  end
end