Class: Prato::Query::DefaultParser

Inherits:
Object
  • Object
show all
Defined in:
lib/prato/query/default_parser.rb

Instance Method Summary collapse

Instance Method Details

#extract_fields(input) ⇒ Object



98
99
100
# File 'lib/prato/query/default_parser.rb', line 98

def extract_fields(input)
  hash_access(input, "fields")
end

#extract_filters(input) ⇒ Object



40
41
42
# File 'lib/prato/query/default_parser.rb', line 40

def extract_filters(input)
  hash_access(input, "filters")
end

#extract_page(input) ⇒ Object



24
25
26
# File 'lib/prato/query/default_parser.rb', line 24

def extract_page(input)
  hash_access(input, "page")
end

#extract_per_page(input) ⇒ Object



32
33
34
# File 'lib/prato/query/default_parser.rb', line 32

def extract_per_page(input)
  hash_access(input, "per_page")
end

#extract_sorts(input) ⇒ Object



80
81
82
# File 'lib/prato/query/default_parser.rb', line 80

def extract_sorts(input)
  hash_access(input, "sorts")
end

#hash_access(entry, key) ⇒ Object



138
139
140
141
142
143
144
145
# File 'lib/prato/query/default_parser.rb', line 138

def hash_access(entry, key)
  value = entry[key]
  if value.nil?
     entry[key.to_sym]
  else
    value
  end
end

#normalize_entries_to_hash(input) ⇒ Object



127
128
129
130
131
132
133
134
135
136
# File 'lib/prato/query/default_parser.rb', line 127

def normalize_entries_to_hash(input)
  case input
  when String
    JSON.parse(input)
  when Array, Hash
    input
  else
    raise ArgumentError, "Invalid filters type: #{input.class}"
  end
end

#parse_field(field, field_resolver) ⇒ Object



112
113
114
115
# File 'lib/prato/query/default_parser.rb', line 112

def parse_field(field, field_resolver)
  fields = field.split(".")
  field_resolver.call(fields)
end

#parse_fields(input, field_lookup) ⇒ Object



102
103
104
105
106
107
108
109
110
# File 'lib/prato/query/default_parser.rb', line 102

def parse_fields(input, field_lookup)
  return nil if input.nil?

  entries = normalize_entries_to_hash(input)

  Array.wrap(entries).map do |entry|
    parse_field(entry, field_lookup)
  end
end

#parse_filter_entries(entries, field_lookup, depth = 0) ⇒ Object

Raises:

  • (ArgumentError)


52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/prato/query/default_parser.rb', line 52

def parse_filter_entries(entries, field_lookup, depth = 0)
  raise ArgumentError, "Filter nesting too deep (maximum depth: 10)" if depth == 10

  Array.wrap(entries).map do |entry|
    if hash_access(entry, "or")
      nested = parse_filter_entries(hash_access(entry, "or"), field_lookup, depth + 1)
      next if nested.nil? || nested.empty?

      Prato::Query::OrFilter.new(nested)
    elsif hash_access(entry, "and")
      nested = parse_filter_entries(hash_access(entry, "and"), field_lookup, depth + 1)
      next if nested.nil? || nested.empty?

      Prato::Query::AndFilter.new(nested)
    else
      field = hash_access(entry, "field")
      operator = hash_access(entry, "operator")
      value = hash_access(entry, "value")

      Prato::Query::Filter.new(
        parse_field(field, field_lookup),
        operator.to_sym,
        value
      )
    end
  end.compact
end

#parse_filters(input, field_lookup) ⇒ Object



44
45
46
47
48
49
50
# File 'lib/prato/query/default_parser.rb', line 44

def parse_filters(input, field_lookup)
  return nil if input.nil?

  entries = normalize_entries_to_hash(input)
  filters = parse_filter_entries(entries, field_lookup)
  filters.nil? || filters.empty? ? nil : filters
end

#parse_page(raw_value) ⇒ Object



28
29
30
# File 'lib/prato/query/default_parser.rb', line 28

def parse_page(raw_value)
  safe_parse_integer(raw_value)
end

#parse_parameters(input, field_lookup) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/prato/query/default_parser.rb', line 8

def parse_parameters(input, field_lookup)
  page = extract_page(input)
  per_page = extract_per_page(input)
  filters = extract_filters(input)
  sorts = extract_sorts(input)
  fields = extract_fields(input)

  Prato::Query::Parameters.new(
    page: parse_page(page),
    per_page: parse_per_page(per_page),
    filters: parse_filters(filters, field_lookup),
    sorts: parse_sorts(sorts, field_lookup),
    fields: parse_fields(fields, field_lookup)
  )
end

#parse_per_page(raw_value) ⇒ Object



36
37
38
# File 'lib/prato/query/default_parser.rb', line 36

def parse_per_page(raw_value)
  safe_parse_integer(raw_value)
end

#parse_sorts(input, field_lookup) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/prato/query/default_parser.rb', line 84

def parse_sorts(input, field_lookup)
  return nil if input.nil?

  entries = normalize_entries_to_hash(input)

  Array.wrap(entries).map do |entry|
    field = hash_access(entry, "field")
    order = hash_access(entry, "order")
    is_desc = %w[desc descending].include?(order)

    Prato::Query::Sort.new(parse_field(field, field_lookup), is_desc)
  end
end

#safe_parse_integer(number) ⇒ Object



117
118
119
120
121
122
123
124
125
# File 'lib/prato/query/default_parser.rb', line 117

def safe_parse_integer(number)
  return nil if number.nil?

  begin
    Integer(number)
  rescue ArgumentError
    nil
  end
end