Class: Payrix::Http::RequestParams

Inherits:
Object
  • Object
show all
Defined in:
lib/payrix/http/request_params.rb

Instance Method Summary collapse

Constructor Details

#initialize(sort = '', expand = [], page = 1, totals = {}) ⇒ RequestParams

Returns a new instance of RequestParams.



4
5
6
7
8
9
# File 'lib/payrix/http/request_params.rb', line 4

def initialize(sort = '', expand = [], page = 1, totals = {})
  self.sort = sort
  self.expand = expand
  self.page = page
  self.totals = totals
end

Instance Method Details

#expandObject



23
24
25
26
27
28
# File 'lib/payrix/http/request_params.rb', line 23

def expand
  @expand.map do |s|
    fields = s.split(',').map { |field| "[#{field.strip}]" }
    "expand#{fields.join}[]"
  end.join('&')
end

#expand=(expand) ⇒ Object



30
31
32
33
34
# File 'lib/payrix/http/request_params.rb', line 30

def expand=(expand)
  if expand.is_a?(Array)
    @expand = expand
  end
end

#go_next_pageObject



62
63
64
# File 'lib/payrix/http/request_params.rb', line 62

def go_next_page
  @page = @page.nil? || @page == 0 ? 1 : @page + 1
end

#pageObject



54
55
56
# File 'lib/payrix/http/request_params.rb', line 54

def page
  @page.nil? || @page == 0 ? "" : "page[number]=#{@page}"
end

#page=(page) ⇒ Object



58
59
60
# File 'lib/payrix/http/request_params.rb', line 58

def page=(page)
  @page = page
end

#sortObject



11
12
13
# File 'lib/payrix/http/request_params.rb', line 11

def sort
  @sort
end

#sort=(sort) ⇒ Object



15
16
17
18
19
20
21
# File 'lib/payrix/http/request_params.rb', line 15

def sort=(sort)
  if sort.is_a?(Array) && sort.size == 2
    @sort = "#{sort[0]}[sort]=#{sort[1]}"
  else
    @sort = sort
  end
end

#totalsObject

Retrieve the totals header string



37
38
39
40
41
42
43
44
45
# File 'lib/payrix/http/request_params.rb', line 37

def totals
  @totals.map do |action, values|
    if values.is_a?(Array) && !values.empty?
      values.map do |value|
        "#{action}[]=#{value}"
      end
    end
  end.join('&')
end

#totals=(totals) ⇒ Object

Set the totals hash



48
49
50
51
52
# File 'lib/payrix/http/request_params.rb', line 48

def totals=(totals)
  if totals.is_a?(Hash)
    @totals = totals
  end
end