Class: ActivePublicResources::RequestCriteria

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Serialization
Defined in:
lib/active_public_resources/request_criteria.rb

Constant Summary collapse

SORTS =
[
  SORT_RELEVANCE = 'relevance',
  SORT_RECENT    = 'recent',
  SORT_POPULAR   = 'popular'
]
CONTENT_FILTERS =
[
  CONTENT_FILTER_NONE   = 'none',
  CONTENT_FILTER_STRICT = 'strict'
]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ RequestCriteria

Returns a new instance of RequestCriteria.



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/active_public_resources/request_criteria.rb', line 18

def initialize(args={})
  args.each do |k,v|
    if k.to_s == 'sort' && !SORTS.include?(v)
      raise ArgumentError.new("sort is invalid. Must be in [#{SORTS.join(', ')}]")
    end
    if k.to_s == 'content_filter' && !CONTENT_FILTERS.include?(v)
      raise ArgumentError.new("content_filter is invalid. Must be in [#{CONTENT_FILTERS.join(', ')}]")
    end
    instance_variable_set("@#{k}", v) unless v.nil?
  end
end

Instance Attribute Details

#channel_nameObject

Returns the value of attribute channel_name.



16
17
18
# File 'lib/active_public_resources/request_criteria.rb', line 16

def channel_name
  @channel_name
end

#content_filterObject

Returns the value of attribute content_filter.



16
17
18
# File 'lib/active_public_resources/request_criteria.rb', line 16

def content_filter
  @content_filter
end

#folderObject

Returns the value of attribute folder.



16
17
18
# File 'lib/active_public_resources/request_criteria.rb', line 16

def folder
  @folder
end

#pageObject

Returns the value of attribute page.



16
17
18
# File 'lib/active_public_resources/request_criteria.rb', line 16

def page
  @page
end

#per_pageObject

Returns the value of attribute per_page.



16
17
18
# File 'lib/active_public_resources/request_criteria.rb', line 16

def per_page
  @per_page
end

#queryObject

Returns the value of attribute query.



16
17
18
# File 'lib/active_public_resources/request_criteria.rb', line 16

def query
  @query
end

#remote_ipObject

Returns the value of attribute remote_ip.



16
17
18
# File 'lib/active_public_resources/request_criteria.rb', line 16

def remote_ip
  @remote_ip
end

#sortObject

Returns the value of attribute sort.



16
17
18
# File 'lib/active_public_resources/request_criteria.rb', line 16

def sort
  @sort
end

Instance Method Details

#attributesObject



68
69
70
# File 'lib/active_public_resources/request_criteria.rb', line 68

def attributes
  instance_values
end

#set_default_criteria!(default_criteria) ⇒ Object



62
63
64
65
66
# File 'lib/active_public_resources/request_criteria.rb', line 62

def set_default_criteria!(default_criteria)
  default_criteria.map do |k, v|
    instance_variable_set("@#{k}", v) unless validate_presence(k)
  end
end

#validate_presence(attr_name, *attr_names) ⇒ Object



44
45
46
47
48
49
50
51
52
# File 'lib/active_public_resources/request_criteria.rb', line 44

def validate_presence(attr_name, *attr_names)
  attr_names.unshift(attr_name)
  attr_names.each do |k|
    if instance_variable_get("@#{k}").blank?
      return false
    end
  end
  true
end

#validate_presence!(attr_names) ⇒ Object



54
55
56
57
58
59
60
# File 'lib/active_public_resources/request_criteria.rb', line 54

def validate_presence!(attr_names)
  attr_names.each do |k|
    if instance_variable_get("@#{k}").blank?
      raise ArgumentError.new("must include #{attr_names.join(', ')}")
    end
  end
end