Class: AgendrixApiService

Inherits:
Object
  • Object
show all
Defined in:
app/services/agendrix_api_service.rb

Constant Summary collapse

CLIENT_ID =
Rails.application.credentials.agendrix_client_id
CLIENT_SECRET =
Rails.application.credentials.agendrix_client_secret
AUTH_URL =
Rails.application.credentials.agendrix_auth_url
RESOURCES_URL =
Rails.application.credentials.agendrix_ressources_url
MINIMUM_TOKEN_REMAINING_TIME =
15.minutes.to_i
PAGE_SIZE =
100
DEFAULT_PARAMS =
{
  page: 1,
  page_size: PAGE_SIZE,
  limit: PAGE_SIZE
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(company_id:) ⇒ AgendrixApiService

Returns a new instance of AgendrixApiService.



38
39
40
# File 'app/services/agendrix_api_service.rb', line 38

def initialize(company_id:)
  @company_id = company_id
end

Instance Attribute Details

#company_idObject (readonly)

Returns the value of attribute company_id.



36
37
38
# File 'app/services/agendrix_api_service.rb', line 36

def company_id
  @company_id
end

Class Method Details



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'app/services/agendrix_api_service.rb', line 17

def consent_url
  uri = URI(AUTH_URL)
  uri.path = '/oauth/authorize'
  query_params = {
    client_id: CLIENT_ID,
    # redirect_uri: AppMode.payroll.web_uri,
    redirect_uri: 'https://omni.edi2xml.com/app/integrations/agendrix',
    response_type: 'code'
    # scope: 'read+write'
  }

  # Didn't use a built-in query param builder
  # because we want '+' not to be encoded
  # this is not good practice but we can do it because we control the values
  uri.query = query_params.map { |k, v| "#{k}=#{v}" }.join('&')
  uri.to_s
end

Instance Method Details

#company_integrationObject



111
112
113
# File 'app/services/agendrix_api_service.rb', line 111

def company_integration
  Integration.agendrix_company_integration(company_id: company_id)
end

#get(path:, params: DEFAULT_PARAMS) ⇒ Object



55
56
57
58
# File 'app/services/agendrix_api_service.rb', line 55

def get(path:, params: DEFAULT_PARAMS)
  response = get_page(path: path, params: params)
  response.body['data']
end

#get_each_page(path:, params: {}) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'app/services/agendrix_api_service.rb', line 67

def get_each_page(path:, params: {})
  current_page = params[:page] || 1

  loop do
    current_params = params.merge(
      page: current_page,
      page_size: PAGE_SIZE,
      limit: PAGE_SIZE
    )

    response = get_page(path: path, params: current_params)
    body = response.body

    yield body['data'], current_page, body['pagination']

    current_page = body['pagination']['current_page']
    total_pages = body['pagination']['total_pages']
    break if current_page >= total_pages

    current_page += 1
  end
end

#get_each_paginated_resource(path:, params: DEFAULT_PARAMS, &block) ⇒ Object



90
91
92
93
94
# File 'app/services/agendrix_api_service.rb', line 90

def get_each_paginated_resource(path:, params: DEFAULT_PARAMS, &block)
  get_each_page(path: path, params: params) do |resource_page, _, _|
    (resource_page || []).each(&block)
  end
end

#get_page(path:, params: DEFAULT_PARAMS) ⇒ Object



60
61
62
63
64
65
# File 'app/services/agendrix_api_service.rb', line 60

def get_page(path:, params: DEFAULT_PARAMS)
  query_string = URI.encode_www_form(params)
  url = "/v1/#{path}?#{query_string}"

  resource_conn(access_token: valid_tokens['access_token']).get(url)
end

#integrate(code:) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
# File 'app/services/agendrix_api_service.rb', line 42

def integrate(code:)
  instance = CompanyIntegration.find_or_initialize_by(
    company_id: company_id,
    integration_id: Integration.agendrix.id
  )
  instance.assign_attributes(
    credentials: tokens_from_code(code),
    active: true
  )
  instance.save!
  instance
end

#tokens_from_code(code) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'app/services/agendrix_api_service.rb', line 96

def tokens_from_code(code)
  response = auth_conn.post('/oauth/token', {
                              client_id: CLIENT_ID,
                              client_secret: CLIENT_SECRET,
                              # redirect_uri: AppMode.payroll.web_uri,
                              redirect_uri: 'https://omni.edi2xml.com/app/integrations/agendrix',
                              grant_type: 'authorization_code',
                              code: code
                            })
  tokens = response.body
  validate_token_response!(tokens)

  tokens
end