Class: Eagleplatform::Filter

Inherits:
Object
  • Object
show all
Defined in:
lib/eagleplatform/filter.rb

Overview

Filters

Examples:

# Simple request:
f = Eagleplatform::Filter.find(54321) 

# With custom records count per page: 
f = Eagleplatform::Filter.find(54321, 100 ) 

# With custom records count per page and custom page number: 
f = Eagleplatform::Filter.find(54321, 50, 3 )

f.current_page 
=> '3'
f.records.count 
=> '50'

f.next_page
=> '+ 50 records loaded'

 f.current_page 
=> '4'
# f.records.count 
=> '100'

# List of records name with create date :
f.records.each { |rec| puts "name: #{rec.name} created_at: #{rec.created_at}" }

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(result) ⇒ Filter

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of Filter.



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/eagleplatform/filter.rb', line 38

def initialize(result)
  @id = result['id']
  @name  = result['name']
  @total_entries = result['total_entries']
  @total_pages = result['total_pages']
  @current_page = result['current_page']
  
  @records = []
  result['records'].each do |record|
    rec = Eagleplatform::Record.new
    rec.each_pair { |k,v| rec[k] = record[k.to_s]}
    @records.push(rec) 
  end
  
  notice = <<-EOF
  #####################################################  
    ATTENTION - There is more then one page
    method 'next_page' load more records
    See: total_pages, current_page and total_enteries
  #####################################################
  EOF
  puts notice if @total_pages > 1
end

Instance Attribute Details

#current_pageObject (readonly)

Returns the value of attribute current_page.



29
30
31
# File 'lib/eagleplatform/filter.rb', line 29

def current_page
  @current_page
end

#idObject (readonly)

Returns the value of attribute id.



29
30
31
# File 'lib/eagleplatform/filter.rb', line 29

def id
  @id
end

#nameObject (readonly)

Returns the value of attribute name.



29
30
31
# File 'lib/eagleplatform/filter.rb', line 29

def name
  @name
end

#per_pageObject (readonly)

Returns the value of attribute per_page.



29
30
31
# File 'lib/eagleplatform/filter.rb', line 29

def per_page
  @per_page
end

#total_entriesObject (readonly)

Returns the value of attribute total_entries.



29
30
31
# File 'lib/eagleplatform/filter.rb', line 29

def total_entries
  @total_entries
end

#total_pagesObject (readonly)

Returns the value of attribute total_pages.



29
30
31
# File 'lib/eagleplatform/filter.rb', line 29

def total_pages
  @total_pages
end

Class Method Details

.find(id, per_page = 50, page = 1) ⇒ Eagleplatform::Filter

Get records form filter

Examples:

# Simple request:
f = Eagleplatform::Filter.find(54321) 

# With custom records count per page: 
f = Eagleplatform::Filter.find(54321, 100 ) 

# With custom records count per page and custom page number: 
f = Eagleplatform::Filter.find(54321, 50, 3 )

Parameters:

  • id (Numeric)

    ID of filter

  • per_page (Numeric) (defaults to: 50)

    Records per page

  • page (Numeric) (defaults to: 1)

    Records page number

Returns:

Raises:

  • (ArgumentError)

    id must be numeric



110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/eagleplatform/filter.rb', line 110

def self.find(id, per_page = 50, page = 1)
  raise 'per_page must be betwen 1 to 1000' unless per_page.between?(1,1000)
  raise ArgumentError, 'id must be numeric' unless id.is_a? Numeric
  params = {
    page: page.to_s,
    per_page: per_page.to_s
  }
  api_method = {method: Methods::FILTER_GET_RECORDS[:method], 
                path: Methods::FILTER_GET_RECORDS[:path].gsub(':id',id.to_s)}
  result = Eagleplatform.call_api(api_method, params)    
  filter = self.new(result)
  filter.instance_eval("@per_page = #{per_page}")
  filter
end

Instance Method Details

#next_pageString

Load records form filter next page

Examples:

f = Eagleplatform::Filter.find(54321)
f.next_page

Returns:

  • (String)

    Count of loaded records



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/eagleplatform/filter.rb', line 68

def next_page
  if @current_page < @total_pages
    begin
      @current_page += 1
      params = {
        page: @current_page.to_s,
        per_page: @per_page.to_s
      }
      api_method = {method: Methods::FILTER_GET_RECORDS[:method], 
                    path: Methods::FILTER_GET_RECORDS[:path].gsub(':id', self.id.to_s)}
      result = Eagleplatform.call_api(api_method, params) 
      result['records'].each do |record|
        rec = Eagleplatform::Record.new
        rec.each_pair { |k,v| rec[k] = record[k.to_s]}
        @records.push(rec) 
      end
      puts "+#{result['records'].count} records loaded."            
    rescue Exception => e
      @current_page -= 1
      puts e
    end
  elsif @current_page == @total_pages
    puts "@current_page == @total_pages. Nothing to load"
  end
end

#recordsArray

List of loaded records

Returns:

  • (Array)

    List of loaded records



33
34
35
# File 'lib/eagleplatform/filter.rb', line 33

def records
  @records
end