Class: ContentfulModel::Query

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

Overview

Class to wrap query parameters

Constant Summary collapse

SYS_PROPERTIES =
%w[type id space contentType linkType revision createdAt updatedAt locale]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(referenced_class, parameters = nil) ⇒ Query

Returns a new instance of Query.



7
8
9
10
# File 'lib/contentful_model/query.rb', line 7

def initialize(referenced_class, parameters = nil)
  @parameters = parameters || {}
  @referenced_class = referenced_class
end

Instance Attribute Details

#parametersObject

Returns the value of attribute parameters.



6
7
8
# File 'lib/contentful_model/query.rb', line 6

def parameters
  @parameters
end

Instance Method Details

#<<(parameters) ⇒ Object



12
13
14
# File 'lib/contentful_model/query.rb', line 12

def <<(parameters)
  @parameters.merge!(parameters)
end

#clientObject



162
163
164
# File 'lib/contentful_model/query.rb', line 162

def client
  @client ||= @referenced_class.client
end

#default_parametersObject



146
147
148
# File 'lib/contentful_model/query.rb', line 146

def default_parameters
  { 'content_type' => @referenced_class.content_type_id }
end

#discover_includesObject



170
171
172
# File 'lib/contentful_model/query.rb', line 170

def discover_includes
  @referenced_class.discovered_include_level
end

#each_entry(per_page = 100, order_field = 'sys.updatedAt', additional_options = {}, &block) ⇒ Object



61
62
63
64
65
66
67
# File 'lib/contentful_model/query.rb', line 61

def each_entry(per_page = 100, order_field = 'sys.updatedAt', additional_options = {}, &block)
  each_page(per_page, order_field, additional_options) do |page|
    page.each do |entry|
      block[entry]
    end
  end
end

#each_page(per_page = 100, order_field = 'sys.updatedAt', additional_options = {}, &block) ⇒ Object



52
53
54
55
56
57
58
59
# File 'lib/contentful_model/query.rb', line 52

def each_page(per_page = 100, order_field = 'sys.updatedAt', additional_options = {}, &block)
  total = self.class.new(@referenced_class).limit(1).load_children(0).params(additional_options).execute.total

  ((total / per_page) + 1).times do |i|
    page = self.class.new(@referenced_class).paginate(i, per_page, order_field, additional_options).execute
    block[page]
  end
end

#executeObject Also known as: load



150
151
152
153
154
155
156
157
158
159
# File 'lib/contentful_model/query.rb', line 150

def execute
  query = @parameters.merge(default_parameters)

  discovered_includes = discover_includes
  query['include'] = discovered_includes unless query.key?('include') || discovered_includes == 1

  result = client.entries(query)
  result.items.reject!(&:invalid?)
  result
end

#find(id) ⇒ Object



92
93
94
95
# File 'lib/contentful_model/query.rb', line 92

def find(id)
  self << { 'sys.id' => id }
  load.first
end

#find_by(find_query = {}) ⇒ Object Also known as: where



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/contentful_model/query.rb', line 97

def find_by(find_query = {})
  find_query.each do |field, value|
    key = if field.to_s.include?('sys.') || field.to_s.include?('fields.')
            field
          elsif SYS_PROPERTIES.include?(field.to_s)
            "sys.#{field}"
          else
            "fields.#{field}"
          end

    case value
    when Array # we need to do an 'in' query
      self << { "#{key}[in]" => value.join(',') }
    when String, Numeric, true, false
      self << { key.to_s => value }
    when Hash
      # if the search is a hash, use the key to specify the search field operator
      # For example
      # Model.search(start_date: {gte: DateTime.now}) => "fields.start_date[gte]" => DateTime.now
      value.each do |search_predicate, search_value|
        self << { "#{key}[#{search_predicate}]" => search_value }
      end
    end
  end

  self
end

#firstObject



21
22
23
24
# File 'lib/contentful_model/query.rb', line 21

def first
  self << { 'limit' => 1 }
  load.first
end

#limit(n) ⇒ Object



32
33
34
35
# File 'lib/contentful_model/query.rb', line 32

def limit(n)
  self << { 'limit' => n }
  self
end

#load_children(n) ⇒ Object



69
70
71
72
# File 'lib/contentful_model/query.rb', line 69

def load_children(n)
  self << { 'include' => n }
  self
end

#locale(locale_code) ⇒ Object



37
38
39
40
# File 'lib/contentful_model/query.rb', line 37

def locale(locale_code)
  self << { 'locale' => locale_code }
  self
end

#offset(n) ⇒ Object Also known as: skip



26
27
28
29
# File 'lib/contentful_model/query.rb', line 26

def offset(n)
  self << { 'skip' => n }
  self
end

#order(args) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/contentful_model/query.rb', line 74

def order(args)
  prefix = ''
  if args.is_a?(Hash)
    column = args.first.first.to_s
    prefix = '-' if args.first.last == :desc
  elsif args.is_a?(Symbol)
    column = args.to_s
    prefix = ''
  else
    column = args.to_s
  end
  property_name = column.camelize(:lower).to_sym
  property_type = SYS_PROPERTIES.include?(property_name.to_s) ? 'sys' : 'fields'

  self << { 'order' => "#{prefix}#{property_type}.#{property_name}" }
  self
end

#paginate(page = 1, per_page = 100, order_field = 'sys.updatedAt', additional_options = {}) ⇒ Object



42
43
44
45
46
47
48
49
50
# File 'lib/contentful_model/query.rb', line 42

def paginate(page = 1, per_page = 100, order_field = 'sys.updatedAt', additional_options = {})
  page = 1 if page.nil? || !page.is_a?(Numeric) || page <= 0
  per_page = 100 if per_page.nil? || !per_page.is_a?(Numeric) || per_page <= 0

  skip_records_count = (page - 1) * per_page
  self << { 'limit' => per_page, 'skip' => skip_records_count, 'order' => order_field }
  self << additional_options
  self
end

#params(options) ⇒ Object



16
17
18
19
# File 'lib/contentful_model/query.rb', line 16

def params(options)
  self << options
  self
end

#resetObject



166
167
168
# File 'lib/contentful_model/query.rb', line 166

def reset
  @parameters = default_parameters
end

#search(parameters) ⇒ Object



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/contentful_model/query.rb', line 126

def search(parameters)
  if parameters.is_a?(Hash)
    parameters.each do |field, search|
      # if the search is a hash, use the key to specify the search field operator
      # For example
      # Model.search(start_date: {gte: DateTime.now}) => "fields.start_date[gte]" => DateTime.now
      if search.is_a?(Hash)
        search_key, search_value = *search.flatten
        self << { "fields.#{field.to_s.camelize(:lower)}[#{search_key}]" => search_value }
      else
        self << { "fields.#{field.to_s.camelize(:lower)}[match]" => search }
      end
    end
  elsif parameters.is_a?(String)
    self << { 'query' => parameters }
  end

  self
end