Module: Paychex::Client::Companies

Included in:
Paychex::Client
Defined in:
lib/paychex/client/companies.rb

Instance Method Summary collapse

Instance Method Details

#company_contact_types(company_id) ⇒ Object

Get company contact types



49
50
51
# File 'lib/paychex/client/companies.rb', line 49

def company_contact_types(company_id)
  get("companies/#{company_id}/contacttypes")
end

#company_status(company_id) ⇒ Object

Get company’s linked status



55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/paychex/client/companies.rb', line 55

def company_status(company_id)
  begin
    content = linked_company(company_id).body.fetch('content')
    return 'linked' if content[0]&.fetch('companyId') == company_id
  rescue Paychex::NoAccess => e
    return 'not-linked'
  rescue Paychex::NotFound => e
    return 'invalid'
  rescue StandardError => e
    p 'Paychex Gem: Handle more errors'
    p e
  end
  'unsupported'
end

#details_by_display_id(display_id) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/paychex/client/companies.rb', line 70

def details_by_display_id(display_id)
  begin
    content = get("companies?displayid=#{display_id}").body.fetch('content')
    company = content[0]
    return {
      "company": company,
      "message": 'found'
    }
  rescue Paychex::NotFound => e
    return {
      "company": nil,
      "message": 'not-found'
    }
  rescue Paychex::NoAccess => e
    return {
      "company": nil,
      "message": 'not-found'
    }
  rescue StandardError => e
    p 'Paychex Gem: Handle more errors'
    p e
  end
  {
    "company": nil,
    "message": 'unsupported'
  }
end

#details_by_display_ids(display_ids) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/paychex/client/companies.rb', line 98

def details_by_display_ids(display_ids)
  ret = {}
  begin
    display_ids.each do |display_id|
      content = details_by_display_id(display_id)
      company = content[:company]
      ret[display_id.to_s] = {
        "company": company,
        "message": company.nil? ? 'not-found' : 'found'
      }
    end
    return ret
  rescue Paychex::NoAccess => e
    ret['message'] = 'unknown'
    return ret
  rescue StandardError => e
    p 'Paychex Gem: Handle more errors'
    p e
  end
  ret['message'] = 'unsupported'
  ret
end

#linked_companiesObject

Get a list of all the linked companies



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
35
36
37
38
39
40
41
# File 'lib/paychex/client/companies.rb', line 5

def linked_companies
  limit = per_page
  opts = { limit: limit, offset: 0 }
  response_content = []
  current_page = 1

  response = companies(opts)

  begin
    while response && response.body
      begin
        companies_count = response.body.fetch('metadata').fetch('pagination').fetch('total')

        no_of_pages = (companies_count.to_f / limit).ceil
      rescue KeyError => e
        # Consider no_of_pages = 1 unless pagination - total node is missing
        no_of_pages = 1
      end

      companies_content = response.body.fetch('content').to_a
      companies_content = companies_content.select { |c| c['hasPermission'] } if companies_content
      response_content += companies_content

      current_page += 1
      if current_page <= no_of_pages
        opts = { limit: limit, offset: (current_page - 1) * limit }
        response = companies(opts)
      else
        break
      end
    end
  rescue StandardError => e
    return response_content
  end

  response_content
end

#linked_company(company_id) ⇒ Object

Get profile of a linked company



44
45
46
# File 'lib/paychex/client/companies.rb', line 44

def linked_company(company_id)
  get("companies/#{company_id}")
end