paylocity_web_service 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 687fc5ccf711675bbc2b78b59ea4e14c17bb43299724e8df0c341824fe01d948
4
+ data.tar.gz: c73541d6298ba908050027f73a69dbcfa726ac21d5eedfdd37e7c19a0fbb3472
5
+ SHA512:
6
+ metadata.gz: b31b8fee32111c963201c92c7404544a6baf3efd73d21bb3fba04faaa1570b411db3c95d99ef3f288e63e8968360c991216791ef9b11bae47d46ac092d24397e
7
+ data.tar.gz: 3074459e1e51a24a12b48b7835fcf4a5301f3a7b324101ff45639f70a86a0016cc45c315efbcf06fce33f7ce378f5d638b0db6db0d25da22ca8ff72c30b2e4d0
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,13 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.4
3
+
4
+ Style/StringLiterals:
5
+ Enabled: true
6
+ EnforcedStyle: double_quotes
7
+
8
+ Style/StringLiteralsInInterpolation:
9
+ Enabled: true
10
+ EnforcedStyle: double_quotes
11
+
12
+ Layout/LineLength:
13
+ Max: 120
data/.travis.yml ADDED
@@ -0,0 +1,6 @@
1
+ ---
2
+ language: ruby
3
+ cache: bundler
4
+ rvm:
5
+ - 2.6.6
6
+ before_install: gem install bundler -v 2.2.15
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2021-05-18
4
+
5
+ - Initial release
@@ -0,0 +1,84 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
6
+
7
+ We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
8
+
9
+ ## Our Standards
10
+
11
+ Examples of behavior that contributes to a positive environment for our community include:
12
+
13
+ * Demonstrating empathy and kindness toward other people
14
+ * Being respectful of differing opinions, viewpoints, and experiences
15
+ * Giving and gracefully accepting constructive feedback
16
+ * Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
17
+ * Focusing on what is best not just for us as individuals, but for the overall community
18
+
19
+ Examples of unacceptable behavior include:
20
+
21
+ * The use of sexualized language or imagery, and sexual attention or
22
+ advances of any kind
23
+ * Trolling, insulting or derogatory comments, and personal or political attacks
24
+ * Public or private harassment
25
+ * Publishing others' private information, such as a physical or email
26
+ address, without their explicit permission
27
+ * Other conduct which could reasonably be considered inappropriate in a
28
+ professional setting
29
+
30
+ ## Enforcement Responsibilities
31
+
32
+ Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
33
+
34
+ Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
35
+
36
+ ## Scope
37
+
38
+ This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
39
+
40
+ ## Enforcement
41
+
42
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at xiaorong.ruby@gmail.com. All complaints will be reviewed and investigated promptly and fairly.
43
+
44
+ All community leaders are obligated to respect the privacy and security of the reporter of any incident.
45
+
46
+ ## Enforcement Guidelines
47
+
48
+ Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
49
+
50
+ ### 1. Correction
51
+
52
+ **Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
53
+
54
+ **Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
55
+
56
+ ### 2. Warning
57
+
58
+ **Community Impact**: A violation through a single incident or series of actions.
59
+
60
+ **Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
61
+
62
+ ### 3. Temporary Ban
63
+
64
+ **Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
65
+
66
+ **Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
67
+
68
+ ### 4. Permanent Ban
69
+
70
+ **Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
71
+
72
+ **Consequence**: A permanent ban from any sort of public interaction within the community.
73
+
74
+ ## Attribution
75
+
76
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
77
+ available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
78
+
79
+ Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
80
+
81
+ [homepage]: https://www.contributor-covenant.org
82
+
83
+ For answers to common questions about this code of conduct, see the FAQ at
84
+ https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
data/Gemfile ADDED
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+ source "https://rubygems.org"
3
+
4
+ # All dependencies from the gemspec and Gemfile will be installed by `bundle install`.
5
+ # https://bundler.io/rubygems.html#using-bundler-while-developing-a-gem
6
+ gemspec
7
+
8
+ # The following gems are required for local development and will not be included by `gem install mygem` or `bundle add mygem`.
9
+ group :development, :test do
10
+ gem 'awesome_print', require: 'ap'
11
+ gem 'pry-byebug'
12
+ gem 'rubocop'
13
+ gem 'rake'
14
+ gem 'bundler'
15
+ gem "rspec", "~> 3.0"
16
+ gem 'webmock', '~> 3.8', '>= 3.8.2'
17
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2021 Ryan Lv
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,101 @@
1
+ ![image](https://user-images.githubusercontent.com/1224077/118908609-53d82e00-b954-11eb-8392-91411d4de5e6.png)
2
+
3
+ # Paylocity Ruby Gem
4
+
5
+ Ruby toolkit for paylocity.com API.
6
+
7
+ ## REST APIs and documentation
8
+
9
+ [Paylocity Web Service API Documentation](https://docs.paylocity.com/weblink/guides/Paylocity_Web_Services_API/Paylocity_Web_Services_API.htm#section/Overview)
10
+
11
+ ## Installation
12
+
13
+ Add this line to your application's Gemfile:
14
+
15
+ ```ruby
16
+ gem 'paylocity-ruby-gem'
17
+ ```
18
+
19
+ And then execute:
20
+
21
+ $ bundle install
22
+
23
+ Or install it yourself as:
24
+
25
+ $ gem install paylocity_web_service
26
+
27
+ ## Config the gem
28
+
29
+ ```ruby
30
+ # config/initializers/paylocity.rb
31
+ public_key = <<HEREDOC
32
+ -----BEGIN PUBLIC KEY-----
33
+ your public key
34
+ -----END PUBLIC KEY-----
35
+ HEREDOC
36
+
37
+
38
+ PaylocityWebService.configure do |config|
39
+ config.client_id = 'your_client_id'
40
+ config.client_secret = 'your_client_secret'
41
+ config.public_key = public_key
42
+ end
43
+ ```
44
+
45
+ ## Making requests
46
+
47
+ ```ruby
48
+ # Provide authentication credentials
49
+ client = PaylocityWebService::Client.new(company_id: 'S2222')
50
+
51
+ # retrieve all employees
52
+ client.employees
53
+
54
+ # get the employee by id
55
+ client.employee(1)
56
+
57
+ # create the employee
58
+ client.create_employee(options={})
59
+
60
+ # get the open api document of current company.
61
+ client.company_schema
62
+ ```
63
+
64
+
65
+ ## Development
66
+
67
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
68
+
69
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
70
+
71
+ ## Contributing
72
+
73
+ Bug reports and pull requests are welcome on GitHub at https://github.com/helloworld1812/paylocity-ruby-gem. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
74
+
75
+ Here are some ways you can contribute:
76
+
77
+ - by reporting bugs
78
+ - by suggesting new features
79
+ - by writing or editing documentation
80
+ - by writing test case
81
+ - by writing code (no patch is too small: fix typos, add comments, clean up inconsistent whitespace)
82
+ - by refactoring code
83
+ - by closing issues
84
+ - by reviewing patches
85
+
86
+ ## License
87
+
88
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
89
+
90
+ ## Code of Conduct
91
+
92
+ Everyone interacting in this project’s codebase, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/wotc-ruby-gem/blob/master/CODE_OF_CONDUCT.md).
93
+
94
+ ## Reference
95
+
96
+ **Good examples of API Wrapper**
97
+
98
+ - [Octokit](https://github.com/octokit/octokit.rb/blob/master/lib/octokit/client.rb)
99
+ - [Instagram](https://github.com/Instagram/instagram-ruby-gem/blob/master/lib/instagram/client.rb)
100
+ - [Twitter](https://github.com/sferik/twitter/blob/master/lib/twitter/rest/api.rb)
101
+
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require "rubocop/rake_task"
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[spec rubocop]
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "bundler/setup"
5
+ require "paylocity_web_service"
6
+
7
+ # Disable SSL verification
8
+ OpenSSL::SSL::VERIFY_PEER = OpenSSL::SSL::VERIFY_NONE
9
+
10
+ require 'pry'
11
+ require "irb"
12
+
13
+ IRB.start(__FILE__)
14
+
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "paylocity_web_service/version"
4
+ require_relative 'paylocity_web_service/error'
5
+ require_relative 'paylocity_web_service/connection'
6
+ require_relative 'paylocity_web_service/configuration'
7
+ require_relative 'paylocity_web_service/authentication'
8
+ require_relative 'paylocity_web_service/client'
9
+
10
+ module PaylocityWebService
11
+ extend PaylocityWebService::Configuration
12
+
13
+ class << self
14
+ def client
15
+ @client || PaylocityWebService::Client.new(options)
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,49 @@
1
+ require 'json'
2
+ require 'base64'
3
+ require 'paylocity_web_service/cache'
4
+
5
+ module PaylocityWebService
6
+ module Authentication
7
+ def access_token
8
+ token = PaylocityWebService::Cache.read(access_token_cache_key)
9
+
10
+ if token.nil?
11
+ refresh_token
12
+ else
13
+ token
14
+ end
15
+ end
16
+
17
+ def refresh_token
18
+ resp = Faraday.post(endpoint + '/IdentityServer/connect/token') do |req|
19
+ req.body = {
20
+ grant_type: 'client_credentials',
21
+ scope: 'WebLinkAPI'
22
+ }
23
+ req.headers = {
24
+ 'Content-Type': 'application/x-www-form-urlencoded',
25
+ 'Authorization': "Basic #{ basic_auth_token }"
26
+ }
27
+ end
28
+
29
+
30
+ body = JSON.parse(resp.body)
31
+ token, expires_in = body['access_token'], body['expires_in']
32
+ PaylocityWebService::Cache.write(access_token_cache_key, token, expires_in)
33
+ token
34
+ end
35
+
36
+ def access_token_cache_key
37
+ 'access_token_cache_key'
38
+ end
39
+
40
+ def basic_auth_token
41
+ secret_value = if client_secret.is_a?(Proc)
42
+ client_secret.call
43
+ else
44
+ client_secret
45
+ end
46
+ Base64.strict_encode64("#{ client_id }:#{ secret_value }")
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,25 @@
1
+ module PaylocityWebService
2
+ class Cache
3
+ def self.read(key)
4
+ return if store[key].nil?
5
+ return if store[key][:expires_at].nil?
6
+ return if store[key][:value].nil?
7
+ return if store[key][:expires_at] < Time.now.to_i
8
+
9
+ store[key][:value]
10
+ end
11
+
12
+ def self.write(key, value, expires_in = 3600 )
13
+ expires_at = (Time.now.to_i + expires_in) - 60
14
+ store[key] = { value: value, expires_at: expires_at}
15
+ end
16
+
17
+ def self.store
18
+ @cache_store ||= {}
19
+ end
20
+
21
+ def self.flush!
22
+ @cache_store = {}
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,27 @@
1
+ require 'paylocity_web_service/connection'
2
+ require 'paylocity_web_service/authentication'
3
+ require 'paylocity_web_service/configuration'
4
+ require 'paylocity_web_service/error'
5
+ require 'paylocity_web_service/client/companies'
6
+ require 'paylocity_web_service/client/employees'
7
+ require 'paylocity_web_service/client/credentials'
8
+
9
+ module PaylocityWebService
10
+ class Client
11
+ include PaylocityWebService::Authentication
12
+ include PaylocityWebService::Configuration
13
+ include PaylocityWebService::Connection
14
+ include PaylocityWebService::Encryption
15
+ include PaylocityWebService::Client::Employees
16
+ include PaylocityWebService::Client::Companies
17
+ include PaylocityWebService::Client::Credentials
18
+
19
+ def initialize(options={})
20
+ options = PaylocityWebService.options.merge(options)
21
+ Configuration::VALID_OPTIONS_KEYS.each do |key|
22
+ send("#{key}=", options[key])
23
+ end
24
+ end
25
+
26
+ end
27
+ end
@@ -0,0 +1,28 @@
1
+ module PaylocityWebService
2
+ class Client
3
+ module Companies
4
+ CodeResources = %W(costcenter1 costcenter2 costcenter3 deductions earnings taxes paygrade positions)
5
+
6
+ def company_schema
7
+ get("/api/v2/companies/#{company_id}/openapi")
8
+ end
9
+
10
+ # Available Code Resources:
11
+ # - costcenter1
12
+ # - costcenter2
13
+ # - costcenter3
14
+ # - deductions
15
+ # - earnings
16
+ # - taxes
17
+ # - paygrade
18
+ # - positions
19
+ def company_codes(code_resource)
20
+ get("/api/v2/companies/#{company_id}/codes/#{code_resource}")
21
+ end
22
+
23
+ def code_resources
24
+ CodeResources
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,9 @@
1
+ module PaylocityWebService
2
+ class Client
3
+ module Credentials
4
+ def create_client_credential
5
+ post("/api/v2/credentials/secrets")
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,17 @@
1
+ module PaylocityWebService
2
+ class Client
3
+ module Employees
4
+ def employees
5
+ get("/api/v2/companies/#{company_id}/employees/")
6
+ end
7
+
8
+ def employee(employee_id)
9
+ get("/api/v2/companies/#{company_id}/employees/#{employee_id}")
10
+ end
11
+
12
+ def create_employee(options={})
13
+ post("/api/v2/companies/#{company_id}/employees", options)
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,86 @@
1
+ module PaylocityWebService
2
+ module Configuration
3
+ VALID_OPTIONS_KEYS = [
4
+ :client_id,
5
+ :client_secret,
6
+ :endpoint,
7
+ :public_key,
8
+ :company_id,
9
+ :proxy,
10
+ :ssl_verify_mode,
11
+ :format,
12
+ :connection_options,
13
+ :user_agent
14
+ ]
15
+
16
+ # By default, don't set a user access token
17
+ DEFAULT_ACCESS_TOKEN = nil
18
+
19
+ # The adapter that will be used to connect if none is set
20
+ #
21
+ # @note The default faraday adapter is Net::HTTP.
22
+ DEFAULT_ADAPTER = Faraday.default_adapter
23
+
24
+ # By default, don't set an application ID
25
+ DEFAULT_CLIENT_ID = nil
26
+
27
+ # By default, don't set an application secret
28
+ DEFAULT_CLIENT_SECRET = nil
29
+
30
+ # By default, don't set any connection options
31
+ DEFAULT_CONNECTION_OPTIONS = {}
32
+
33
+ # The endpoint that will be used to connect if none is set
34
+ #
35
+ # @note There is no reason to use any other endpoint at this time
36
+ DEFAULT_ENDPOINT = 'https://apisandbox.paylocity.com'.freeze
37
+
38
+ # The response format appended to the path and sent in the 'Accept' header if none is set
39
+ #
40
+ # @note JSON is the only available format at this time
41
+ DEFAULT_FORMAT = :json
42
+
43
+ # By default, don't use a proxy server
44
+ DEFAULT_PROXY = nil
45
+
46
+ # By default, the ssl_verify_mode is true
47
+ DEFAULT_SSL_VERIFY_MODE = true
48
+
49
+ # The user agent that will be sent to the API endpoint if none is set
50
+ DEFAULT_USER_AGENT = "paylocity-ruby-gem #{PaylocityWebService::VERSION}".freeze
51
+
52
+ attr_accessor *VALID_OPTIONS_KEYS
53
+
54
+ # Sets all configuration options to their default values
55
+ # when this module is extended.
56
+ def self.extended(base)
57
+ base.reset
58
+ end
59
+
60
+ # Set configuration options using a block
61
+ def configure
62
+ yield self
63
+ end
64
+
65
+ # Creates a hash of options and their values.
66
+ def options
67
+ VALID_OPTIONS_KEYS.inject({}) do |option, key|
68
+ option.merge!(key => send(key))
69
+ end
70
+ end
71
+
72
+ # Reset configuration options to default values
73
+ def reset
74
+ self.client_id = DEFAULT_CLIENT_ID
75
+ self.client_secret = DEFAULT_CLIENT_SECRET
76
+ self.connection_options = DEFAULT_CONNECTION_OPTIONS
77
+ self.endpoint = DEFAULT_ENDPOINT
78
+ self.format = DEFAULT_FORMAT
79
+ self.proxy = DEFAULT_PROXY
80
+ self.user_agent = DEFAULT_USER_AGENT
81
+ self.ssl_verify_mode = DEFAULT_SSL_VERIFY_MODE
82
+
83
+ self
84
+ end
85
+ end
86
+ end
@@ -0,0 +1,106 @@
1
+ require 'faraday'
2
+ require 'faraday_middleware'
3
+
4
+ require 'paylocity_web_service/authentication'
5
+ require 'paylocity_web_service/encryption'
6
+ require "paylocity_web_service/middleware/response/raise_error"
7
+
8
+ module PaylocityWebService
9
+ # Network layer for API clients.
10
+ module Connection
11
+ include PaylocityWebService::Authentication
12
+
13
+ # Make a HTTP GET request
14
+ #
15
+ # @param url [String] The path, relative to {#api_endpoint}
16
+ # @return [Sawyer::Resource]
17
+ def get(url)
18
+ request :get, url
19
+ end
20
+
21
+ # Make a HTTP POST request
22
+ #
23
+ # @param url [String] The path, relative to {#api_endpoint}
24
+ # @param options [Hash] Body and header params for request
25
+ # @return [Sawyer::Resource]
26
+ def post(url, options = {})
27
+ request :post, url, options
28
+ end
29
+
30
+ # Make a HTTP PUT request
31
+ #
32
+ # @param url [String] The path, relative to {#api_endpoint}
33
+ # @param options [Hash] Body and header params for request
34
+ # @return [Sawyer::Resource]
35
+ def put(url, options = {})
36
+ request :put, url, options
37
+ end
38
+
39
+ # Make a HTTP PATCH request
40
+ #
41
+ # @param url [String] The path, relative to {#api_endpoint}
42
+ # @param options [Hash] Body and header params for request
43
+ # @return [Sawyer::Resource]
44
+ def patch(url, options = {})
45
+ request :patch, url, options
46
+ end
47
+
48
+ # Make a HTTP DELETE request
49
+ #
50
+ # @param url [String] The path, relative to {#api_endpoint}
51
+ # @param options [Hash] Query and header params for request
52
+ # @return [Sawyer::Resource]
53
+ def delete(url, options = {})
54
+ request :delete, url, options
55
+ end
56
+
57
+ # Make a HTTP HEAD request
58
+ #
59
+ # @param url [String] The path, relative to {#api_endpoint}
60
+ # @param options [Hash] Query and header params for request
61
+ # @return [Sawyer::Resource]
62
+ def head(url, options = {})
63
+ request :head, url, options
64
+ end
65
+
66
+
67
+ private
68
+
69
+ def request(method, path, options = {})
70
+ response = connection.send(method) do |request|
71
+ case method
72
+ when :get, :delete
73
+ request.url(URI.encode(path), options)
74
+ when :post, :put
75
+ request.path = URI.encode(path)
76
+ request.body = envelope_encrypt(options)
77
+ end
78
+ end
79
+
80
+ response
81
+ end
82
+
83
+ def connection
84
+ options = {
85
+ headers: {
86
+ 'Accept' => "application/#{format}; charset=utf-8",
87
+ 'User-Agent' => user_agent,
88
+ 'Content-Type' => "application/#{format}"
89
+ },
90
+ proxy: proxy,
91
+ url: endpoint,
92
+ }.merge(connection_options)
93
+
94
+ connection = Faraday::Connection.new(options) do |conn|
95
+ conn.use Faraday::Request::Authorization, :Bearer, access_token
96
+ conn.request :retry, max: 2
97
+ conn.request :json
98
+
99
+ conn.use PaylocityWebService::Middleware::Response::RaiseError
100
+ conn.use FaradayMiddleware::ParseJson, :content_type => /\bjson$/
101
+ end
102
+
103
+ connection
104
+ end
105
+ end
106
+ end
@@ -0,0 +1,32 @@
1
+ require 'openssl'
2
+
3
+ module PaylocityWebService
4
+ module Encryption
5
+ def envelope_encrypt(data)
6
+ # 1 generate data encryption key and IV.
7
+ aes = OpenSSL::Cipher::AES.new(256, :CBC)
8
+ aes.encrypt
9
+
10
+ key = aes.random_key
11
+ iv = aes.random_iv
12
+
13
+ # 2. encrypt data use your data encryption key
14
+ encrypted_data = aes.update(data.to_json) + aes.final
15
+ encoded_data64 = Base64.strict_encode64(encrypted_data)
16
+
17
+ # 3. encrypt your key through the public key issued by paylocity
18
+ pub_key = OpenSSL::PKey::RSA.new(self.public_key)
19
+ encrypted_key64 = Base64.strict_encode64(pub_key.public_encrypt(key))
20
+ encoded_iv64 = Base64.strict_encode64(iv)
21
+
22
+ # 4. put together.
23
+ {
24
+ secureContent: {
25
+ key: encrypted_key64,
26
+ iv: encoded_iv64,
27
+ content: encoded_data64
28
+ }
29
+ }
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,122 @@
1
+ module PaylocityWebService
2
+ class Error < StandardError
3
+ # Returns the appropriate PaylocityWebService::Error subclass based
4
+ # on status and response message
5
+ #
6
+ # @param [Hash] response HTTP response
7
+ # @return [PaylocityWebService::Error]
8
+ def self.from_response(response)
9
+ case response[:status]
10
+ when 400
11
+ raise PaylocityWebService::BadRequest, response_values(response)
12
+ when 401
13
+ raise PaylocityWebService::ClientError, response_values(response)
14
+ when 403
15
+ raise PaylocityWebService::Forbidden, response_values(response)
16
+ when 404
17
+ raise PaylocityWebService::NotFound, response_values(response)
18
+ when 405
19
+ raise PaylocityWebService::MethodNotAllowed, response_values(response)
20
+ when 406
21
+ raise PaylocityWebService::NotAcceptable, response_values(response)
22
+ when 409
23
+ raise PaylocityWebService::Conflict, response_values(response)
24
+ when 415
25
+ raise PaylocityWebService::UnsupportedMediaType, response_values(response)
26
+ when 422
27
+ raise PaylocityWebService::UnprocessableEntity, response_values(response)
28
+ when 451
29
+ raise PaylocityWebService::UnavailableForLegalReasons, response_values(response)
30
+ when 400..499
31
+ raise PaylocityWebService::ClientError, response_values(response)
32
+ when 500
33
+ raise PaylocityWebService::InternalServerError, response_values(response)
34
+ when 501
35
+ raise PaylocityWebService::NotImplemented, response_values(response)
36
+ when 502
37
+ raise PaylocityWebService::BadGateway, response_values(response)
38
+ when 503
39
+ raise PaylocityWebService::ServiceUnavailable, response_values(response)
40
+ when 500..599
41
+ raise PaylocityWebService::ServerError, response_values(response)
42
+ end
43
+ end
44
+
45
+ private
46
+
47
+ def self.response_values(response)
48
+ return nil if response.nil?
49
+
50
+ {
51
+ status: response.status,
52
+ headers: response.response_headers,
53
+ body: response.body,
54
+ request: {
55
+ method: response.method,
56
+ url_path: response.url.path,
57
+ params: response.params,
58
+ headers: response.request_headers,
59
+ body: response.request_body
60
+ }
61
+ }
62
+ end
63
+ end
64
+
65
+ # Raised on errors in the 400-499 range
66
+ class ClientError < Error; end
67
+
68
+ # Raised when returns a 400 HTTP status code
69
+ class BadRequest < ClientError; end
70
+
71
+ # Raised when returns a 401 HTTP status code
72
+ class Unauthorized < ClientError; end
73
+
74
+ # Raised when returns a 403 HTTP status code
75
+ class Forbidden < ClientError; end
76
+
77
+ # Raised when returns a 404 HTTP status code
78
+ class NotFound < ClientError; end
79
+
80
+ # Raised when returns a 405 HTTP status code
81
+ class MethodNotAllowed < ClientError; end
82
+
83
+ # Raised when returns a 406 HTTP status code
84
+ class NotAcceptable < ClientError; end
85
+
86
+ # Raised when returns a 409 HTTP status code
87
+ class Conflict < ClientError; end
88
+
89
+ # Raised when returns a 414 HTTP status code
90
+ class UnsupportedMediaType < ClientError; end
91
+
92
+ # Raised when returns a 422 HTTP status code
93
+ class UnprocessableEntity < ClientError; end
94
+
95
+ # Raised when returns a 451 HTTP status code
96
+ class UnavailableForLegalReasons < ClientError; end
97
+
98
+ # Raised on errors in the 500-599 range
99
+ class ServerError < Error; end
100
+
101
+ # Raised when returns a 500 HTTP status code
102
+ class InternalServerError < ServerError; end
103
+
104
+ # Raised when returns a 501 HTTP status code
105
+ class NotImplemented < ServerError; end
106
+
107
+ # Raised when returns a 502 HTTP status code
108
+ class BadGateway < ServerError; end
109
+
110
+ # Raised when returns a 503 HTTP status code
111
+ class ServiceUnavailable < ServerError; end
112
+
113
+ # Raised when client fails to provide valid Content-Type
114
+ class MissingContentType < ArgumentError; end
115
+
116
+ # Raised when client fails to provide a required key
117
+ class MissingKey < ArgumentError; end
118
+
119
+ # Raised when a method requires an application client_id
120
+ # and secret but none is provided
121
+ class ApplicationCredentialsRequired < StandardError; end
122
+ end
@@ -0,0 +1,26 @@
1
+ require 'paylocity_web_service/error'
2
+
3
+ module PaylocityWebService
4
+ module Middleware
5
+ # Faraday response middleware
6
+ module Response
7
+
8
+ # This class raises an exception based
9
+ # HTTP status codes returned by the API
10
+ class RaiseError < Faraday::Response::Middleware
11
+
12
+ def initialize(app)
13
+ super app
14
+ end
15
+
16
+ def call(env)
17
+ @app.call(env).on_complete do |response|
18
+ if error = PaylocityWebService::Error.from_response(response)
19
+ raise error
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module PaylocityWebService
4
+ VERSION = "0.1.0"
5
+ end
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/paylocity_web_service/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "paylocity_web_service"
7
+ spec.version = PaylocityWebService::VERSION
8
+ spec.authors = ["Ryan Lv"]
9
+ spec.email = ["tech@workstream.is"]
10
+
11
+ spec.summary = "Ruby wrapper for the API of Paylocity Web Service"
12
+ spec.description = spec.summary
13
+ spec.homepage = "https://github.com/helloworld1812/paylocity-ruby-gem"
14
+ spec.license = "MIT"
15
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.4.0")
16
+
17
+ spec.metadata["homepage_uri"] = spec.homepage
18
+ spec.metadata["source_code_uri"] = "https://github.com/helloworld1812/paylocity-ruby-gem"
19
+ spec.metadata["changelog_uri"] = "https://github.com/helloworld1812/paylocity-ruby-gem/blob/master/CHANGELOG.md"
20
+
21
+ # Specify which files should be added to the gem when it is released.
22
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
23
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
24
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{\A(?:test|spec|features)/}) }
25
+ end
26
+ spec.bindir = "exe"
27
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
28
+ spec.require_paths = ["lib"]
29
+
30
+ spec.add_dependency 'faraday'
31
+ spec.add_dependency 'faraday_middleware'
32
+ end
metadata ADDED
@@ -0,0 +1,101 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: paylocity_web_service
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Ryan Lv
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2021-06-21 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: faraday
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: faraday_middleware
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: Ruby wrapper for the API of Paylocity Web Service
42
+ email:
43
+ - tech@workstream.is
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - ".gitignore"
49
+ - ".rspec"
50
+ - ".rubocop.yml"
51
+ - ".travis.yml"
52
+ - CHANGELOG.md
53
+ - CODE_OF_CONDUCT.md
54
+ - Gemfile
55
+ - LICENSE.txt
56
+ - README.md
57
+ - Rakefile
58
+ - bin/console
59
+ - bin/setup
60
+ - lib/paylocity_web_service.rb
61
+ - lib/paylocity_web_service/authentication.rb
62
+ - lib/paylocity_web_service/cache.rb
63
+ - lib/paylocity_web_service/client.rb
64
+ - lib/paylocity_web_service/client/companies.rb
65
+ - lib/paylocity_web_service/client/credentials.rb
66
+ - lib/paylocity_web_service/client/employees.rb
67
+ - lib/paylocity_web_service/configuration.rb
68
+ - lib/paylocity_web_service/connection.rb
69
+ - lib/paylocity_web_service/encryption.rb
70
+ - lib/paylocity_web_service/error.rb
71
+ - lib/paylocity_web_service/middleware/request/.keep
72
+ - lib/paylocity_web_service/middleware/response/raise_error.rb
73
+ - lib/paylocity_web_service/version.rb
74
+ - paylocity_web_service.gemspec
75
+ homepage: https://github.com/helloworld1812/paylocity-ruby-gem
76
+ licenses:
77
+ - MIT
78
+ metadata:
79
+ homepage_uri: https://github.com/helloworld1812/paylocity-ruby-gem
80
+ source_code_uri: https://github.com/helloworld1812/paylocity-ruby-gem
81
+ changelog_uri: https://github.com/helloworld1812/paylocity-ruby-gem/blob/master/CHANGELOG.md
82
+ post_install_message:
83
+ rdoc_options: []
84
+ require_paths:
85
+ - lib
86
+ required_ruby_version: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ version: 2.4.0
91
+ required_rubygems_version: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - ">="
94
+ - !ruby/object:Gem::Version
95
+ version: '0'
96
+ requirements: []
97
+ rubygems_version: 3.0.8
98
+ signing_key:
99
+ specification_version: 4
100
+ summary: Ruby wrapper for the API of Paylocity Web Service
101
+ test_files: []