Class: Cin7API::BranchResource

Inherits:
Resource
  • Object
show all
Defined in:
lib/cin7_api/resources/branch_resource.rb

Instance Attribute Summary

Attributes inherited from Resource

#client

Instance Method Summary collapse

Methods inherited from Resource

#get_request, #handle_response, #initialize, #post_request, #put_request

Constructor Details

This class inherits a constructor from Cin7API::Resource

Instance Method Details

#where(**params) ⇒ Object

Use 1 for active branches Use 0 for inactive branches @client.branch.where(active: 1)



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
# File 'lib/cin7_api/resources/branch_resource.rb', line 8

def where(**params)
  page = 1
  paginated_branches = []

  response_body = get_request("Branches", params: params).body
  branches = response_body.map { |attributes| Branch.new(attributes) }
  paginated_branches += branches

  while branches.length == 50
    page += 1
    response_body = get_request("Branches", params: params, page: page).body
    branches = response_body.map { |attributes| Branch.new(attributes) }
    paginated_branches += branches
  end

  # Doing a filter here because Cin7 is doing a binary OR check for the filter params
  paginated_branches.filter do |branch|
    params.reduce(true) do |acc, (key, value)|
      if key == :active
        parsed_value = value == 1
        branch["is_active"] == parsed_value
      else
        acc && branch[key] == value
      end
    end
  end
end