aboutsummaryrefslogtreecommitdiff
path: root/src/guff/results.cr
blob: 0dbc3513103b5bcf2a502129fd8e453db83f7674 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
module Guff
  class Results(R)
    getter :page
    getter :limit
    getter :num_rows
    getter :rows

    def initialize(
      @page = 1       : Int32,
      @limit = 1      : Int32,
      @num_rows = 0   : Int32,
      @rows = [] of R : Array(R),
    )
    end

    def to_json
      { 
        meta: {
          page:       @page,
          limit:      @limit,
          num_pages:  (@num_rows / @limit).ceil,
          num_rows:   @num_rows,
        },

        rows: @rows.map { |row|
          row.reduce({} of String => String) do |r, k, v|
            r[k] = v.to_s
            r
          end
        },
      }.to_json
    end
  end
end