paychex_api 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA256:
3
- metadata.gz: 9f78afb440d84e9a5176bd9c870017eaff629de329dd03231ba2341765907dcb
4
- data.tar.gz: 9130a0af28b1b20dca4e83c66a7ded516f53e2762d153eced828d00543873bda
2
+ SHA1:
3
+ metadata.gz: 9de0f7cad757b537924ef7f2f44d900c35e5012d
4
+ data.tar.gz: d309d3026ae13f12cfb9007522e8740a207278c8
5
5
  SHA512:
6
- metadata.gz: a02a59856b12ec65389025d56d38fb34031a3de2b00f0aee78248a51594c3a765d9d2f3225fd54e047ffb7a7dc4fc46c26d51df1363fbe478724675266a8befb
7
- data.tar.gz: b2b057ee0cdf27aa2a7ef7c664f2775e53c15da03c7c8acc275b5d6c6fcc8d78252c298b96cf94bd2b7ab4afb6c10039edcc0d63540a347e1ec787a6bfe6ae47
6
+ metadata.gz: e41269cc0be06d85a2e81a1ea1322ec56c49e887febe6aeb6bb35cada13976a6500085c70f91f9f5033780da7b4d5fedddf1ad4cc0447213652a0bf2600cb3a7
7
+ data.tar.gz: bd2845c4f9e0682e580cc1b9a6657702f3771cf195cbae783cad649ab713864709b6cb7dd94a56a2b5b6f8b2a4cf8e556a2e7528eb64249e2a923acde928b36d
data/.gitignore CHANGED
@@ -5,3 +5,4 @@ bridge_api.iml
5
5
  .byebug.*
6
6
  .byebug_history
7
7
  .*.gem
8
+ coverage
data/.rubocop.yml ADDED
@@ -0,0 +1,24 @@
1
+ AllCops:
2
+ Exclude:
3
+ - 'bin/**/*'
4
+ - 'node_modules/**/*'
5
+ - 'vendor/**/*'
6
+ Metrics/ClassLength:
7
+ Max: 150
8
+ Metrics/LineLength:
9
+ Max: 120
10
+ Style/Documentation:
11
+ Enabled: false
12
+ Metrics/BlockLength:
13
+ Exclude:
14
+ - 'paychex_api.gemspec'
15
+ - 'spec/**/*'
16
+ Style/FrozenStringLiteralComment:
17
+ Enabled: false
18
+ Style/SymbolArray:
19
+ Enabled: false
20
+ Metrics/MethodLength:
21
+ Exclude:
22
+ - spec/**/*
23
+ Naming/AccessorMethodName:
24
+ Enabled: false
data/.simplecov ADDED
@@ -0,0 +1,7 @@
1
+ # This file is copied to spec/ when you run 'rails generate rspec:install'
2
+ SimpleCov.minimum_coverage 80
3
+ SimpleCov.minimum_coverage_by_file 80
4
+ SimpleCov.start do
5
+ add_group 'lib', 'lib'
6
+ add_filter 'spec'
7
+ end
data/Dockerfile ADDED
@@ -0,0 +1,14 @@
1
+ FROM ruby:2.5.0
2
+
3
+ RUN apt-get update
4
+
5
+ RUN mkdir -p /usr/src/app
6
+ COPY Gemfile /usr/src/app/
7
+ COPY Gemfile.lock /usr/src/app/
8
+ COPY paychex_api.gemspec /usr/src/app/
9
+ COPY /lib/paychex_api/version.rb /usr/src/app/lib/paychex_api/
10
+
11
+ WORKDIR /usr/src/app
12
+ RUN bundle install --system
13
+
14
+ COPY . /usr/src/app/
data/Gemfile CHANGED
@@ -1,2 +1,2 @@
1
1
  source 'https://rubygems.org'
2
- gemspec
2
+ gemspec
data/Gemfile.lock CHANGED
@@ -1,27 +1,29 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- bridge_api (0.0.1)
5
- faraday (~> 0.9.0)
6
- faraday_middleware (~> 0.9.0)
4
+ paychex_api (0.0.3)
5
+ faraday (~> 0.9.2)
6
+ faraday_middleware (~> 0.9.2)
7
7
  footrest (>= 0.5.1)
8
8
 
9
9
  GEM
10
10
  remote: https://rubygems.org/
11
11
  specs:
12
- activesupport (5.1.4)
12
+ activesupport (5.2.0)
13
13
  concurrent-ruby (~> 1.0, >= 1.0.2)
14
- i18n (~> 0.7)
14
+ i18n (>= 0.7, < 2)
15
15
  minitest (~> 5.1)
16
16
  tzinfo (~> 1.1)
17
17
  addressable (2.5.2)
18
18
  public_suffix (>= 2.0.2, < 4.0)
19
+ ast (2.4.0)
19
20
  byebug (8.2.5)
20
21
  coderay (1.1.2)
21
22
  concurrent-ruby (1.0.5)
22
23
  crack (0.4.3)
23
24
  safe_yaml (~> 1.0.0)
24
25
  diff-lcs (1.3)
26
+ docile (1.3.0)
25
27
  faraday (0.9.2)
26
28
  multipart-post (>= 1.2, < 3)
27
29
  faraday_middleware (0.9.2)
@@ -30,13 +32,24 @@ GEM
30
32
  activesupport (>= 3.0.0)
31
33
  faraday (~> 0.9.0)
32
34
  link_header (>= 0.0.7)
35
+ gergich (0.2.2)
36
+ httparty (~> 0.6)
37
+ sqlite3 (~> 1.3)
33
38
  hashdiff (0.3.7)
34
- i18n (0.9.1)
39
+ httparty (0.16.2)
40
+ multi_xml (>= 0.5.2)
41
+ i18n (1.0.1)
35
42
  concurrent-ruby (~> 1.0)
43
+ json (2.1.0)
36
44
  link_header (0.0.8)
37
45
  method_source (0.9.0)
38
- minitest (5.11.1)
46
+ minitest (5.11.3)
47
+ multi_xml (0.6.0)
39
48
  multipart-post (2.0.0)
49
+ parallel (1.12.1)
50
+ parser (2.5.1.0)
51
+ ast (~> 2.4.0)
52
+ powerpack (0.1.1)
40
53
  pry (0.11.3)
41
54
  coderay (~> 1.1.0)
42
55
  method_source (~> 0.9.0)
@@ -44,6 +57,7 @@ GEM
44
57
  rack (1.6.8)
45
58
  rack-protection (1.5.3)
46
59
  rack
60
+ rainbow (3.0.0)
47
61
  rake (0.9.6)
48
62
  rspec (2.99.0)
49
63
  rspec-core (~> 2.99.0)
@@ -53,15 +67,30 @@ GEM
53
67
  rspec-expectations (2.99.2)
54
68
  diff-lcs (>= 1.1.3, < 2.0)
55
69
  rspec-mocks (2.99.4)
70
+ rubocop (0.54.0)
71
+ parallel (~> 1.10)
72
+ parser (>= 2.5)
73
+ powerpack (~> 0.1)
74
+ rainbow (>= 2.2.2, < 4.0)
75
+ ruby-progressbar (~> 1.7)
76
+ unicode-display_width (~> 1.0, >= 1.0.1)
77
+ ruby-progressbar (1.9.0)
56
78
  safe_yaml (1.0.4)
79
+ simplecov (0.16.1)
80
+ docile (~> 1.1)
81
+ json (>= 1.8, < 3)
82
+ simplecov-html (~> 0.10.0)
83
+ simplecov-html (0.10.2)
57
84
  sinatra (1.4.8)
58
85
  rack (~> 1.5)
59
86
  rack-protection (~> 1.4)
60
87
  tilt (>= 1.3, < 3)
88
+ sqlite3 (1.3.13)
61
89
  thread_safe (0.3.6)
62
90
  tilt (1.4.1)
63
- tzinfo (1.2.4)
91
+ tzinfo (1.2.5)
64
92
  thread_safe (~> 0.1)
93
+ unicode-display_width (1.3.2)
65
94
  webmock (1.22.6)
66
95
  addressable (>= 2.3.6)
67
96
  crack (>= 0.3.2)
@@ -71,15 +100,18 @@ PLATFORMS
71
100
  ruby
72
101
 
73
102
  DEPENDENCIES
74
- bridge_api!
75
103
  bundler (~> 1.0, >= 1.0.0)
76
104
  byebug (~> 8.2.2)
105
+ gergich (~> 0.2.2)
106
+ paychex_api!
77
107
  pry (~> 0)
78
108
  rake (~> 0)
79
109
  rspec (~> 2.6)
110
+ rubocop (~> 0.54.0)
111
+ simplecov (~> 0.16.1)
80
112
  sinatra (~> 1.0)
81
113
  tilt (~> 1.3, >= 1.3.4)
82
114
  webmock (~> 1.22.6)
83
115
 
84
116
  BUNDLED WITH
85
- 1.16.0
117
+ 1.16.1
data/bin/jenkins ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env bash
2
+
3
+ set +e
4
+ EXIT_CODES=0
5
+
6
+ echo "Analyzing ruby code with rubocop"
7
+ bundle exec gergich capture rubocop "bundle exec rubocop --fail-level autocorrect"
8
+ EXIT_CODES=$(($EXIT_CODES + $?))
9
+
10
+ echo 'Running ruby specs'
11
+ bundle exec rspec
12
+ EXIT_CODES=$(($EXIT_CODES + $?))
13
+
14
+ bundle exec gergich publish
15
+ exit $EXIT_CODES
data/build.sh ADDED
@@ -0,0 +1,5 @@
1
+ set -e
2
+
3
+ docker-compose build
4
+ docker-compose up -d
5
+ docker-compose run -T web bin/jenkins
@@ -0,0 +1,18 @@
1
+ version: '2'
2
+
3
+ services:
4
+ web:
5
+ build:
6
+ context: .
7
+ dockerfile: ./Dockerfile
8
+ environment:
9
+ APP_DOMAIN: web
10
+ GERGICH_KEY: "${GERGICH_KEY}"
11
+ GERRIT_PROJECT: "${GERRIT_PROJECT}"
12
+ GERRIT_HOST: "${GERRIT_HOST}"
13
+ GERRIT_BRANCH: "${GERRIT_BRANCH}"
14
+ volumes:
15
+ - "coverage:/usr/src/app/coverage"
16
+
17
+ volumes:
18
+ coverage: {}
data/lib/paychex_api.rb CHANGED
@@ -1,2 +1,11 @@
1
1
  require 'paychex_api/version'
2
2
  require 'paychex_api/client'
3
+
4
+ module PaychexAPI
5
+ class << self
6
+ attr_accessor :proxy
7
+ def configure
8
+ yield self if block_given?
9
+ end
10
+ end
11
+ end
@@ -3,24 +3,27 @@ module PaychexAPI
3
3
  class ApiArray
4
4
  include Enumerable
5
5
 
6
+ MAX_PAGES = 100
7
+
6
8
  @next_page = nil
7
9
  @prev_page = nil
8
10
 
9
- attr_reader :status, :headers, :members, :links, :metadata, :members
11
+ attr_reader :status, :headers, :members, :links, :metadata
12
+ attr_writer :next_page
10
13
 
11
14
  def self.process_response(response, api_client)
12
15
  ApiArray.new(response, api_client)
13
16
  end
14
17
 
15
18
  def initialize(response, api_client)
16
- @meta_fields = %w(metadata links)
19
+ @meta_fields = %w[metadata links]
17
20
  @api_client = api_client
18
21
  @links = []
19
22
  @metadata = {}
20
23
  case response.status
21
- when *((200..206).to_a + [302])
22
- apply_response_metadata(response)
23
- @members = get_response_content(response)
24
+ when *((200..206).to_a + [302])
25
+ apply_response_metadata(response)
26
+ @members = get_response_content(response)
24
27
  end
25
28
  end
26
29
 
@@ -28,16 +31,16 @@ module PaychexAPI
28
31
  @members.length
29
32
  end
30
33
 
31
- def [](i)
32
- @members[i]
34
+ def [](index)
35
+ @members[index]
33
36
  end
34
37
 
35
38
  def last
36
39
  @members.last
37
40
  end
38
41
 
39
- def each(&block)
40
- @members.each { |member| block.call(member) }
42
+ def each
43
+ @members.each { |member| yield(member) }
41
44
  end
42
45
 
43
46
  def pages?
@@ -48,33 +51,40 @@ module PaychexAPI
48
51
  load_page(@next_page)
49
52
  end
50
53
 
51
- def each_page(&block)
52
- block.call(@members, @linked, @meta)
54
+ def each_page
55
+ yield(@members, @linked, @meta)
53
56
  while @next_page
54
57
  response = get_page(@next_page)
55
58
  apply_response_metadata(response, false)
56
59
  @members = get_response_content(response)
57
- block.call(@members, @linked, @meta)
60
+ yield(@members, @linked, @meta)
58
61
  end
59
62
  @link_hash = {}
60
63
  end
61
64
 
62
65
  def all_pages!
63
- if pages?
64
- response = get_page(@next_page)
65
- apply_response_metadata(response)
66
- @members.concat(get_response_content(response))
67
- while @next_page
68
- response = get_page(@next_page)
69
- apply_response_metadata(response)
70
- @members.concat(get_response_content(response))
71
- end
72
- end
66
+ return self unless pages?
67
+ combine_page
68
+ combine_pages
73
69
  self
74
70
  end
75
71
 
76
72
  private
77
73
 
74
+ def combine_pages
75
+ counter = 0
76
+ while @next_page && counter <= MAX_PAGES
77
+ combine_page
78
+ counter += 1
79
+ end
80
+ end
81
+
82
+ def combine_page
83
+ response = get_page(@next_page)
84
+ apply_response_metadata(response)
85
+ @members.concat(get_response_content(response))
86
+ end
87
+
78
88
  def get_page(url, params = {})
79
89
  query = URI.parse(url).query
80
90
  p = CGI.parse(query).merge(params)
@@ -93,15 +103,13 @@ module PaychexAPI
93
103
  def get_response_content(response)
94
104
  return [] unless response.body.is_a?(Hash)
95
105
  content = response.body['content']
96
- if content.length > 0
97
- return content
98
- end
106
+ return content unless content.empty?
99
107
  []
100
108
  end
101
109
 
102
110
  def apply_response_metadata(response, concat = true)
103
111
  unless concat
104
- @links = {}
112
+ @links = []
105
113
  @metadata = {}
106
114
  end
107
115
  @status = response.status
@@ -113,31 +121,26 @@ module PaychexAPI
113
121
  end
114
122
 
115
123
  def init_linked(response)
116
- if response.body.is_a?(Hash) && response.body.key?('links')
117
- @links = @links.concat(response.body['links'])
118
- end
124
+ return nil unless response.body.is_a?(Hash) && response.body.key?('links')
125
+ @links = @links.concat(response.body['links'])
119
126
  end
120
127
 
121
128
  def init_meta(response)
122
- if response.body.is_a?(Hash) && response.body.key?('metadata')
123
- @metadata = response.body['metadata']
124
- end
129
+ return nil unless response.body.is_a?(Hash) && response.body.key?('metadata')
130
+ @metadata = response.body['metadata']
125
131
  end
126
132
 
127
133
  def init_pages(response)
128
- @next_page = nil
129
- @prev_page = nil
130
- if response.body.is_a?(Hash) && response.body.key?('links')
131
- response.body['links'].each do |link|
132
- case link['rel']
133
- when 'next'
134
- @next_page = link['href']
135
- when 'prev'
136
- @prev_page = link['href']
137
- end
134
+ return nil unless response.body.is_a?(Hash) && response.body.key?('links')
135
+ @next_page, @prev_page = nil
136
+ response.body['links'].each do |link|
137
+ case link['rel']
138
+ when 'next'
139
+ @next_page = link['href']
140
+ when 'prev'
141
+ @prev_page = link['href']
138
142
  end
139
143
  end
140
144
  end
141
-
142
145
  end
143
146
  end
@@ -11,68 +11,98 @@ require 'paychex_api/api_array'
11
11
 
12
12
  module PaychexAPI
13
13
  class Client < Footrest::Client
14
-
15
- API_PATH = ""
16
- COMPANIES_PATH = '/companies'
17
- ORGANIZATIONS_PATH = '/organizations'
18
- JOB_TITLES_PATH = '/jobtitles'
19
- WORKER_STATUSES_PATH = '/workerstatuses'
20
- WORKERS_PATH = '/workers'
21
- COMMUNICATIONS_PATH = '/communications'
22
- COMPENSATION_PATH = '/compensation'
23
- DIRECT_DEPOSITS_PATH = '/directdeposits'
24
- PAY_PERIODS_PATH = '/payperiods'
25
- PAY_COMPONENTS_PATH = '/paycomponents'
26
- CHECKS_PATH = '/checks'
27
- OAUTH_TOKEN_PATH = '/auth/oauth/v2/token'
14
+ API_PATH = ''.freeze
15
+ COMPANIES_PATH = '/companies'.freeze
16
+ ASSOCIATIONS_PATH = '/'.freeze
17
+ ORGANIZATIONS_PATH = '/organizations'.freeze
18
+ JOB_TITLES_PATH = '/jobtitles'.freeze
19
+ WORKER_STATUSES_PATH = '/workerstatuses'.freeze
20
+ WORKERS_PATH = '/workers'.freeze
21
+ COMMUNICATIONS_PATH = '/communications'.freeze
22
+ COMPENSATION_PATH = '/compensation'.freeze
23
+ DIRECT_DEPOSITS_PATH = '/directdeposits'.freeze
24
+ PAY_PERIODS_PATH = '/payperiods'.freeze
25
+ PAY_COMPONENTS_PATH = '/paycomponents'.freeze
26
+ CHECKS_PATH = '/checks'.freeze
27
+ OAUTH_TOKEN_PATH = '/auth/oauth/v2/token'.freeze
28
28
 
29
29
  attr_reader :authorization
30
30
 
31
31
  Dir[File.dirname(__FILE__) + '/client/*.rb'].each do |file|
32
32
  require file
33
- include self.const_get("#{File.basename(file).gsub('.rb','').split("_").map{|ea| ea.capitalize}.join('')}")
33
+ include const_get(File.basename(file).gsub('.rb', '').split('_').map(&:capitalize).join('').to_s)
34
34
  end
35
35
 
36
36
  # Override Footrest request for ApiArray support
37
37
  def request(method, &block)
38
- if @authorization.blank? || @authorization['expiration'] <= DateTime.now - 30.seconds
39
- temp_client = get_default_faraday
40
- response = temp_client.post(
41
- OAUTH_TOKEN_PATH,
42
- {
43
- client_id: config[:client_id],
44
- client_secret: config[:client_secret],
45
- grant_type: 'client_credentials'
46
- }
47
- )
48
- @authorization = response.body
49
- @authorization['expiration'] = DateTime.now + @authorization['expires_in'].to_i.seconds
50
- end
51
- connection.headers[:authorization] = "Bearer #{@authorization['access_token']}"
52
- ApiArray::process_response(connection.send(method, &block), self)
38
+ authorize if @authorization.blank? || @authorization['expiration'] <= Time.now - 30.seconds
39
+ connection.headers[:authorization] = "Bearer #{@authorization['access_token']}"
40
+ ApiArray.process_response(connection.send(method, &block), self)
41
+ end
42
+
43
+ def authorize
44
+ response = default_faraday.post(
45
+ OAUTH_TOKEN_PATH,
46
+ client_id: config[:client_id],
47
+ client_secret: config[:client_secret],
48
+ grant_type: 'client_credentials'
49
+ )
50
+ @authorization = response.body
51
+ @authorization['expiration'] = Time.now + @authorization['expires_in'].to_i.seconds
53
52
  end
54
53
 
55
54
  def set_connection(config)
56
55
  config[:logger] = config[:logging] if config[:logging]
57
- @connection = get_default_faraday
56
+ @connection = default_faraday
57
+ end
58
+
59
+ def faraday_default_use(faraday)
60
+ faraday.use Footrest::ParseJson, content_type: /\bjson$/
61
+ faraday.use Footrest::RaiseFootrestErrors
62
+ faraday.use Footrest::Pagination
63
+ faraday.use Footrest::FollowRedirects, limit: 5 if config[:follow_redirects]
64
+ faraday
65
+ end
66
+
67
+ def faraday_default_headers(faraday)
68
+ faraday.headers[:accept] = 'application/json'
69
+ faraday.headers[:user_agent] = 'Footrest'
70
+ faraday
71
+ end
72
+
73
+ def faraday_default_request(faraday)
74
+ faraday.request :multipart
75
+ faraday.request :url_encoded
76
+ faraday
77
+ end
78
+
79
+ def faraday_logger(faraday)
80
+ if config[:logger] == true
81
+ faraday.response :logger
82
+ elsif config[:logger]
83
+ faraday.use Faraday::Response::Logger, config[:logger]
84
+ end
85
+ faraday
86
+ end
87
+
88
+ def faraday_proxy(faraday)
89
+ faraday.proxy PaychexAPI.proxy if PaychexAPI.proxy
90
+ faraday
91
+ end
92
+
93
+ def faraday_config(faraday)
94
+ faraday = faraday_logger(faraday)
95
+ faraday = faraday_default_request(faraday)
96
+ faraday = faraday_default_use(faraday)
97
+ faraday = faraday_default_headers(faraday)
98
+ faraday = faraday_proxy(faraday)
99
+ faraday.adapter Faraday.default_adapter
100
+ faraday
58
101
  end
59
102
 
60
- def get_default_faraday
103
+ def default_faraday
61
104
  Faraday.new(url: config[:prefix]) do |faraday|
62
- faraday.request :multipart
63
- faraday.request :url_encoded
64
- if config[:logger] == true
65
- faraday.response :logger
66
- elsif config[:logger]
67
- faraday.use Faraday::Response::Logger, config[:logger]
68
- end
69
- faraday.use Footrest::FollowRedirects, limit: 5 unless config[:follow_redirects] == false
70
- faraday.adapter Faraday.default_adapter
71
- faraday.use Footrest::ParseJson, :content_type => /\bjson$/
72
- faraday.use Footrest::RaiseFootrestErrors
73
- faraday.use Footrest::Pagination
74
- faraday.headers[:accept] = "application/json"
75
- faraday.headers[:user_agent] = "Footrest"
105
+ faraday_config(faraday)
76
106
  end
77
107
  end
78
108
  end