freee-pigmon 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 557cf5532801e3c096ac85c34350e5e3a72f1959dde58620ea5bcd78d08c90be
4
+ data.tar.gz: '047482c2efccf70329ede69136b6a5b64bdcdde71147702f1173663916c7348d'
5
+ SHA512:
6
+ metadata.gz: 5cc1ca7b2e91d4ef876a30e84a6bf15701ebdb402f1dd0440f17a1cb237c7324d4881e2a38f166896db321cddb965d796836088b7c34e042b45757fa1f591952
7
+ data.tar.gz: 79967ff25492b32f660a2098fbea513e0aecb18f42cc9321f80827889c3c855756b09c277987ac10c3d294c0e485b3bb40aa881d7c7d566bb1dfd11e6328e11d
File without changes
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
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at iori.osada@gmail.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in freee-pigmon.gemspec
6
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,65 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ freee-pigmon (0.2.0)
5
+ faraday (~> 0.12.2)
6
+ faraday_middleware (~> 0.12.2)
7
+ oauth2 (~> 1.4.0)
8
+ thor
9
+
10
+ GEM
11
+ remote: https://rubygems.org/
12
+ specs:
13
+ byebug (10.0.2)
14
+ coderay (1.1.2)
15
+ diff-lcs (1.3)
16
+ faraday (0.12.2)
17
+ multipart-post (>= 1.2, < 3)
18
+ faraday_middleware (0.12.2)
19
+ faraday (>= 0.7.4, < 1.0)
20
+ jwt (2.2.2)
21
+ method_source (0.9.2)
22
+ multi_json (1.15.0)
23
+ multi_xml (0.6.0)
24
+ multipart-post (2.1.1)
25
+ oauth2 (1.4.4)
26
+ faraday (>= 0.8, < 2.0)
27
+ jwt (>= 1.0, < 3.0)
28
+ multi_json (~> 1.3)
29
+ multi_xml (~> 0.5)
30
+ rack (>= 1.2, < 3)
31
+ pry (0.12.2)
32
+ coderay (~> 1.1.0)
33
+ method_source (~> 0.9.0)
34
+ pry-byebug (3.6.0)
35
+ byebug (~> 10.0)
36
+ pry (~> 0.10)
37
+ rack (2.2.3)
38
+ rake (10.5.0)
39
+ rspec (3.8.0)
40
+ rspec-core (~> 3.8.0)
41
+ rspec-expectations (~> 3.8.0)
42
+ rspec-mocks (~> 3.8.0)
43
+ rspec-core (3.8.2)
44
+ rspec-support (~> 3.8.0)
45
+ rspec-expectations (3.8.4)
46
+ diff-lcs (>= 1.2.0, < 2.0)
47
+ rspec-support (~> 3.8.0)
48
+ rspec-mocks (3.8.1)
49
+ diff-lcs (>= 1.2.0, < 2.0)
50
+ rspec-support (~> 3.8.0)
51
+ rspec-support (3.8.2)
52
+ thor (1.1.0)
53
+
54
+ PLATFORMS
55
+ ruby
56
+
57
+ DEPENDENCIES
58
+ bundler (~> 1.17)
59
+ freee-pigmon!
60
+ pry-byebug (~> 3.6.0)
61
+ rake (~> 10.0)
62
+ rspec (~> 3.0)
63
+
64
+ BUNDLED WITH
65
+ 1.17.2
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2019 Iori OSADA
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,75 @@
1
+ NOTE: Be careful, this is the beta-version. Specifically, this doesn't have any test codes. 😭
2
+
3
+ # Freee::Pigmon
4
+
5
+ Request Freee time clocking with CLI.
6
+
7
+ ![image](https://vignette.wikia.nocookie.net/ultra/images/b/b9/Max_pigmon.png/revision/latest?cb=20111211083202)
8
+
9
+ ## Installation
10
+
11
+ Clone this repo.
12
+
13
+ $ git clone git@github.com:xxxx/freee-pigmon.git
14
+
15
+ And then execute:
16
+
17
+ $ gem build freee-pigmon.gemspec
18
+
19
+ Finally install it.
20
+
21
+ $ gem install freee-pigmon
22
+
23
+ ## Usage
24
+
25
+ Note: You can see simple descriptions about commands with below command:
26
+
27
+ $ freee-pigmon
28
+
29
+ ### Register Code
30
+
31
+ First, you have to get authorize key from [Here](https://accounts.secure.freee.co.jp/public_api/authorize?client_id=a213888077faae573fdba8e5d7787448e84efbf5e9ca6584035deac9f74508a4&redirect_uri=urn:ietf:wg:oauth:2.0:oob&response_type=code).
32
+
33
+ Then, register access tokens using the key.
34
+
35
+ $ freee-pigmon set_code {code}
36
+
37
+ This is just once task of OAuth2.
38
+
39
+ ### clock in
40
+
41
+ Note: Be careful, you can do this just once in a day.
42
+
43
+ $ freee-pigmon clock_in
44
+
45
+ ### clock out
46
+
47
+ Note: Be careful, you can do this just once in a day and only after clock-in.
48
+
49
+ $ freee-pigmon clock_out
50
+
51
+ ### break
52
+
53
+ You can request as below.
54
+
55
+ $ freee-pigmon break_begin
56
+
57
+ $ freee-pigmon break_end
58
+
59
+ ## Development
60
+
61
+ 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.
62
+
63
+ 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 tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
64
+
65
+ ## Contributing
66
+
67
+ Bug reports and pull requests are welcome on GitHub at https://github.com/degica/freee-pigmon. 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.
68
+
69
+ ## License
70
+
71
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
72
+
73
+ ## Code of Conduct
74
+
75
+ Everyone interacting in the Freee::Pigmon project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/freee-pigmon/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "freee/pigmon"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
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
data/exe/freee-pigmon ADDED
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'freee/pigmon'
4
+
5
+ Freee::Pigmon::CLI.start
6
+
@@ -0,0 +1,44 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "freee/pigmon/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "freee-pigmon"
8
+ spec.version = Freee::Pigmon::VERSION
9
+ spec.authors = ["Iori OSADA"]
10
+ spec.email = ["iosada@degica.com"]
11
+
12
+ spec.summary = %q{freee checking-in and out system.}
13
+ spec.description = %q{This is to help you checking-in and out on freee.}
14
+ spec.homepage = "https://degica.com"
15
+ spec.license = "MIT"
16
+
17
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
18
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
19
+ # if spec.respond_to?(:metadata)
20
+ # spec.metadata['allowed_push_host'] = 'https://rubygems.pkg.github.com/degica/freee-pigmon'
21
+ # else
22
+ # raise "RubyGems 2.0 or newer is required to protect against " \
23
+ # "public gem pushes."
24
+ # end
25
+
26
+ # Specify which files should be added to the gem when it is released.
27
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
28
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
29
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
30
+ end
31
+ spec.bindir = "exe"
32
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
33
+ spec.require_paths = ["lib"]
34
+
35
+ spec.add_development_dependency "bundler", "~> 1.17"
36
+ spec.add_development_dependency "rake", "~> 10.0"
37
+ spec.add_development_dependency "rspec", "~> 3.0"
38
+ spec.add_development_dependency 'pry-byebug', '~> 3.6.0'
39
+
40
+ spec.add_dependency 'oauth2', '~> 1.4.0'
41
+ spec.add_dependency 'faraday', '~> 0.12.2'
42
+ spec.add_dependency 'faraday_middleware', '~> 0.12.2'
43
+ spec.add_dependency 'thor'
44
+ end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'oauth2'
4
+ require 'faraday'
5
+ require 'faraday_middleware'
6
+ require 'fileutils'
7
+ require 'yaml'
8
+ require 'psych'
9
+
10
+ require 'freee/pigmon/version'
11
+ require 'freee/pigmon/cli'
12
+ require 'freee/pigmon/client'
13
+ require 'freee/pigmon/api_client'
14
+ require 'freee/pigmon/helper/config_helper'
15
+ require 'freee/pigmon/helper/oauth2_helper'
16
+
17
+ module Freee
18
+ module Pigmon
19
+ class Error < StandardError; end
20
+ end
21
+ end
@@ -0,0 +1,77 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Freee
4
+ module Pigmon
5
+ class APIClient
6
+ HR_API_URL = 'https://api.freee.co.jp'
7
+
8
+ attr_accessor :access_token
9
+
10
+ def initialize(access_token)
11
+ self.access_token = access_token
12
+ end
13
+
14
+ def fetch_user_info_hash
15
+ response = request_api('/hr/api/v1/users/me', :get, nil)
16
+ company_info_hash = response.body['companies'].first
17
+ company_id = company_info_hash['id']
18
+ employee_id = company_info_hash['employee_id']
19
+
20
+ {
21
+ company_id: company_id,
22
+ employee_id: employee_id
23
+ }
24
+ end
25
+
26
+ def check_available_time_clock_types(employee_id, company_id)
27
+ path = "/hr/api/v1/employees/#{employee_id}/time_clocks/available_types"
28
+ params = { company_id: company_id }
29
+ response = request_api(path, :get, params)
30
+ response.body['available_types']
31
+ end
32
+
33
+ def post_time_clock(employee_id, company_id, type)
34
+ path = "/hr/api/v1/employees/#{employee_id}/time_clocks"
35
+ params = { company_id: company_id, type: type }
36
+ response = request_api(path, :post, params)
37
+ response
38
+ end
39
+
40
+ def get_time_clocks(employee_id, company_id, from_date: nil, to_date: nil, per: nil, page: nil)
41
+ path = "/hr/api/v1/employees/#{employee_id}/time_clocks"
42
+ params = {
43
+ company_id: company_id,
44
+ from_date: from_date,
45
+ to_date: to_date,
46
+ per: per,
47
+ page: page,
48
+ }
49
+ response = request_api(path, :get, params)
50
+ response
51
+ end
52
+
53
+ def request_api(path, method, params)
54
+ client = api_client
55
+ response = client.send(method) do |req|
56
+ req.url path
57
+ req.body = params.to_json unless params.nil?
58
+ end
59
+ case response.status
60
+ when 401
61
+ raise 'Unauthorized'
62
+ end
63
+ response
64
+ end
65
+
66
+ def api_client
67
+ client = Faraday.new(url: HR_API_URL) do |faraday|
68
+ faraday.request :json
69
+ faraday.response :json, content_type: /\bjson$/
70
+ faraday.adapter Faraday.default_adapter
71
+ end
72
+ client.authorization :Bearer, access_token
73
+ client
74
+ end
75
+ end
76
+ end
77
+ end
@@ -0,0 +1,128 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'thor'
4
+
5
+ module Freee
6
+ module Pigmon
7
+ class CLI < Thor
8
+
9
+ desc 'set_client {id} {secret}', 'set client_id and client_secret'
10
+ def set_client(client_id, client_secret)
11
+ Freee::Pigmon.configure(
12
+ freee_client_id: client_id,
13
+ freee_client_secret: client_secret
14
+ )
15
+ end
16
+
17
+ desc 'authorize {authorize_code}', 'set authorized code and set up.'
18
+ def authorize(authorize_code)
19
+ token_hash = pigmon.fetch_token_hash(authorize_code)
20
+ Freee::Pigmon.configure(
21
+ access_token: token_hash[:access_token],
22
+ refresh_token: token_hash[:refresh_token],
23
+ expires_at: token_hash[:expires_at]
24
+ )
25
+ user_info_hash = pigmon.fetch_freee_user_info_hash(access_token)
26
+ Freee::Pigmon.configure(
27
+ employee_id: user_info_hash[:employee_id],
28
+ company_id: user_info_hash[:company_id]
29
+ )
30
+ end
31
+
32
+ desc 'generate_code', 'display generate_code url'
33
+ def generate_code
34
+ puts Freee::Pigmon.url_to_generate_code(client_id)
35
+ end
36
+
37
+ desc 'clock_in', 'send clock_in request to freee.'
38
+ def clock_in
39
+ token_refresh if token_expired?
40
+ puts pigmon.clock_in(access_token, employee_id, company_id).body
41
+ end
42
+
43
+ desc 'break_begin', 'send break_begin request to freee.'
44
+ def break_begin
45
+ token_refresh if token_expired?
46
+ puts pigmon.break_begin(access_token, employee_id, company_id).body
47
+ end
48
+
49
+ desc 'break_end', 'send break_end request to freee.'
50
+ def break_end
51
+ token_refresh if token_expired?
52
+ puts pigmon.break_end(access_token, employee_id, company_id).body
53
+ end
54
+
55
+ desc 'clock_out', 'send clock_out request to freee.'
56
+ def clock_out
57
+ token_refresh if token_expired?
58
+ puts pigmon.clock_out(access_token, employee_id, company_id).body
59
+ end
60
+
61
+ private
62
+
63
+ def pigmon
64
+ Freee::Pigmon::Client.new(client_id, client_secret)
65
+ end
66
+
67
+ def client_id
68
+ Freee::Pigmon.config[:freee_client_id]
69
+ end
70
+
71
+ def client_secret
72
+ Freee::Pigmon.config[:freee_client_secret]
73
+ end
74
+
75
+ def access_token
76
+ Freee::Pigmon.config[:access_token]
77
+ end
78
+
79
+ def refresh_token
80
+ Freee::Pigmon.config[:refresh_token]
81
+ end
82
+
83
+ def expires_at
84
+ Freee::Pigmon.config[:expires_at]
85
+ end
86
+
87
+ def employee_id
88
+ Freee::Pigmon.config[:employee_id]
89
+ end
90
+
91
+ def company_id
92
+ Freee::Pigmon.config[:company_id]
93
+ end
94
+
95
+ def token_expired?
96
+ return false if expires_at.nil?
97
+
98
+ Time.parse(expires_at) <= Time.now
99
+ end
100
+
101
+ def token_refresh
102
+ token_hash = Freee::Pigmon.token_refresh(
103
+ client_id,
104
+ client_secret,
105
+ access_token,
106
+ refresh_token,
107
+ expires_at
108
+ )
109
+
110
+ Freee::Pigmon.configure(
111
+ access_token: token_hash[:access_token],
112
+ refresh_token: token_hash[:refresh_token],
113
+ expires_at: token_hash[:expires_at]
114
+ )
115
+ end
116
+
117
+ def warn_to_get_code
118
+ puts "Get code from here: #{url_to_generate_code}\n"
119
+ true
120
+ end
121
+
122
+ def token_is_ok_message
123
+ puts 'You already prepared token. No problem.'
124
+ true
125
+ end
126
+ end
127
+ end
128
+ end
@@ -0,0 +1,97 @@
1
+ # frozen_string_literal: true
2
+ require 'freee/pigmon/api_client'
3
+
4
+ module Freee
5
+ module Pigmon
6
+ class Client
7
+ attr_accessor :client_id, :client_secret
8
+
9
+ def initialize(client_id, client_secret)
10
+ self.client_id = client_id
11
+ self.client_secret = client_secret
12
+ end
13
+
14
+ def url_to_generate_code
15
+ Freee::Pigmon.url_to_generate_code(self.client_id)
16
+ end
17
+
18
+ def fetch_token_hash(authorize_code)
19
+ token_info_hash = Freee::Pigmon.fetch_token_from_code(client_id, client_secret, authorize_code)
20
+ token_info_hash
21
+ end
22
+
23
+ def refresh_access_token(access_token, refresh_token, expires_at)
24
+ token_info_hash = Freee::Pigmon.token_refresh(
25
+ access_token,
26
+ refresh_token,
27
+ expires_at
28
+ )
29
+ token_info_hash
30
+ end
31
+
32
+ def fetch_freee_user_info_hash(access_token)
33
+ api_client(access_token).fetch_user_info_hash
34
+ end
35
+
36
+ # =========================
37
+
38
+ def clock_in(access_token, employee_id, company_id)
39
+ api_client(access_token).post_time_clock(
40
+ employee_id,
41
+ company_id,
42
+ 'clock_in'
43
+ )
44
+ end
45
+
46
+ def clocked_in?(access_token, employee_id, company_id)
47
+ available_types = api_client(access_token)
48
+ .check_available_time_clock_types(
49
+ employee_id,
50
+ company_id
51
+ )
52
+ available_types.include? 'clock_out'
53
+ end
54
+
55
+ def break_begin(access_token, employee_id, company_id)
56
+ api_client(access_token).post_time_clock(
57
+ employee_id,
58
+ company_id,
59
+ 'break_begin'
60
+ )
61
+ end
62
+
63
+ def break_end(access_token, employee_id, company_id)
64
+ api_client(access_token).post_time_clock(
65
+ employee_id,
66
+ company_id,
67
+ 'break_end'
68
+ )
69
+ end
70
+
71
+ def clock_out(access_token, employee_id, company_id)
72
+ api_client(access_token).post_time_clock(
73
+ employee_id,
74
+ company_id,
75
+ 'clock_out'
76
+ )
77
+ end
78
+
79
+ def get_clocks(access_token, employee_id, company_id, from_date: nil, to_date: nil, per: nil, page: nil)
80
+ api_client(access_token).get_time_clocks(
81
+ employee_id,
82
+ company_id,
83
+ from_date: from_date,
84
+ to_date: to_date,
85
+ per: per,
86
+ page: page
87
+ )
88
+ end
89
+
90
+ private
91
+
92
+ def api_client(access_token)
93
+ Freee::Pigmon::APIClient.new(access_token)
94
+ end
95
+ end
96
+ end
97
+ end
@@ -0,0 +1,65 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Freee
4
+ module Pigmon
5
+
6
+ @config = {
7
+ freee_client_id: nil,
8
+ freee_client_secret: nil,
9
+ access_token: nil,
10
+ refresh_token: nil,
11
+ expires_at: nil,
12
+ authorize_code: nil,
13
+ company_id: nil,
14
+ employee_id: nil
15
+ }
16
+
17
+ @valid_config_keys = @config.keys
18
+
19
+ def self.configure(opts = {})
20
+ config
21
+ opts.each do |k, v|
22
+ @config[k.to_sym] = v if @valid_config_keys.include? k.to_sym
23
+ end
24
+ save_config
25
+ end
26
+
27
+ def self.get_config_path
28
+ generate_config
29
+ config_path
30
+ end
31
+
32
+ def self.generate_config
33
+ Dir.mkdir(config_dir_path) unless Dir.exist? config_dir_path
34
+ FileUtils.touch(config_path) unless File.exist? config_path
35
+ end
36
+
37
+ def self.config_dir_path
38
+ Dir.home + '/.freee-pigmon'
39
+ end
40
+
41
+ def self.config_path
42
+ config_dir_path + '/settings.yml'
43
+ end
44
+
45
+ def self.config
46
+ yml_path = get_config_path
47
+ yml_file = YAML.load_file(yml_path)
48
+ if yml_file
49
+ @config = yml_file
50
+ else
51
+ File.open(yml_path, 'w') { |f| YAML.dump(@config, f) }
52
+ end
53
+ end
54
+
55
+ # 現在の@configをyamlに保存
56
+ def self.save_config
57
+ yml_path = get_config_path
58
+ if File.exist?(yml_path)
59
+ File.open(yml_path, 'w') { |f| YAML.dump(@config, f) }
60
+ else
61
+ raise "Can't find #{yml_path}. please set configure."
62
+ end
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,57 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Freee
4
+ module Pigmon
5
+ class << self
6
+ CALLBACK_URL = 'urn:ietf:wg:oauth:2.0:oob'
7
+ OAUTH2_SITE = 'https://accounts.secure.freee.co.jp'
8
+ OAUTH2_URL = '/public_api/token'
9
+
10
+ def url_to_generate_code(client_id)
11
+ "https://accounts.secure.freee.co.jp/public_api/authorize?client_id=#{client_id}&redirect_uri=#{CALLBACK_URL}&response_type=code"
12
+ end
13
+
14
+ def fetch_token_from_code(client_id, client_secret, authrorize_code)
15
+ client = oauth2_client(client_id, client_secret)
16
+ response = client.auth_code.get_token(authrorize_code,
17
+ redirect_uri: CALLBACK_URL)
18
+ token_info_hash(response)
19
+ end
20
+
21
+ def token_refresh(
22
+ client_id,
23
+ client_secret,
24
+ access_token,
25
+ refresh_token,
26
+ expires_at
27
+ )
28
+ return false if refresh_token.nil?
29
+
30
+ client = oauth2_client(client_id, client_secret)
31
+ oauth2_access_token = OAuth2::AccessToken.new(
32
+ client,
33
+ access_token,
34
+ refresh_token: refresh_token,
35
+ expires_at: expires_at.to_i
36
+ )
37
+ response = oauth2_access_token.refresh!
38
+ token_info_hash(response)
39
+ end
40
+
41
+ def oauth2_client(client_id, client_secret)
42
+ OAuth2::Client.new(client_id,
43
+ client_secret,
44
+ site: OAUTH2_SITE,
45
+ token_url: OAUTH2_URL)
46
+ end
47
+
48
+ def token_info_hash(response)
49
+ {
50
+ access_token: response.token,
51
+ refresh_token: response.refresh_token,
52
+ expires_at: Time.at(response.expires_at).strftime('%Y-%m-%d %H:%M:%S')
53
+ }
54
+ end
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,5 @@
1
+ module Freee
2
+ module Pigmon
3
+ VERSION = '0.2.0'
4
+ end
5
+ end
metadata ADDED
@@ -0,0 +1,176 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: freee-pigmon
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
5
+ platform: ruby
6
+ authors:
7
+ - Iori OSADA
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2021-02-16 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.17'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.17'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: pry-byebug
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 3.6.0
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 3.6.0
69
+ - !ruby/object:Gem::Dependency
70
+ name: oauth2
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: 1.4.0
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: 1.4.0
83
+ - !ruby/object:Gem::Dependency
84
+ name: faraday
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: 0.12.2
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: 0.12.2
97
+ - !ruby/object:Gem::Dependency
98
+ name: faraday_middleware
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: 0.12.2
104
+ type: :runtime
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: 0.12.2
111
+ - !ruby/object:Gem::Dependency
112
+ name: thor
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :runtime
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ description: This is to help you checking-in and out on freee.
126
+ email:
127
+ - iosada@degica.com
128
+ executables:
129
+ - freee-pigmon
130
+ extensions: []
131
+ extra_rdoc_files: []
132
+ files:
133
+ - ".freee-pigmon/config.yml"
134
+ - ".gitignore"
135
+ - ".rspec"
136
+ - CODE_OF_CONDUCT.md
137
+ - Gemfile
138
+ - Gemfile.lock
139
+ - LICENSE.txt
140
+ - README.md
141
+ - Rakefile
142
+ - bin/console
143
+ - bin/setup
144
+ - exe/freee-pigmon
145
+ - freee-pigmon.gemspec
146
+ - lib/freee/pigmon.rb
147
+ - lib/freee/pigmon/api_client.rb
148
+ - lib/freee/pigmon/cli.rb
149
+ - lib/freee/pigmon/client.rb
150
+ - lib/freee/pigmon/helper/config_helper.rb
151
+ - lib/freee/pigmon/helper/oauth2_helper.rb
152
+ - lib/freee/pigmon/version.rb
153
+ homepage: https://degica.com
154
+ licenses:
155
+ - MIT
156
+ metadata: {}
157
+ post_install_message:
158
+ rdoc_options: []
159
+ require_paths:
160
+ - lib
161
+ required_ruby_version: !ruby/object:Gem::Requirement
162
+ requirements:
163
+ - - ">="
164
+ - !ruby/object:Gem::Version
165
+ version: '0'
166
+ required_rubygems_version: !ruby/object:Gem::Requirement
167
+ requirements:
168
+ - - ">="
169
+ - !ruby/object:Gem::Version
170
+ version: '0'
171
+ requirements: []
172
+ rubygems_version: 3.0.3
173
+ signing_key:
174
+ specification_version: 4
175
+ summary: freee checking-in and out system.
176
+ test_files: []