jbcn 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
+ SHA1:
3
+ metadata.gz: 951525ccc503519e49cb98ada2ac871fdae43d82
4
+ data.tar.gz: 620abb4e2d0230396e5648ba024c7bc1e0366333
5
+ SHA512:
6
+ metadata.gz: aa9357f5df69aa3d078a536ae6e95ffee28a2e98d4ad3ccd446d4aaf208374cfa35dfd1d4076667736b37e3ebac4b3e5d10479ee914eebd646e5540f78808461
7
+ data.tar.gz: b595c710692edc242bd000b54afe975b1363170bb0bcb4fe32bb4115a3b7d9239b83f978eb8e5e5076f8e8f16382098c519b78d895c6b3a34bc3ff94a4c3df75
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in jbcn.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Tomoki Aonuma
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,51 @@
1
+ # Jbcn
2
+
3
+ Jbcn is a client for [Jobcan](http://jobcan.ne.jp).
4
+
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ ```ruby
11
+ gem 'jbcn'
12
+ ```
13
+
14
+ And then execute:
15
+
16
+ $ bundle
17
+
18
+ Or install it yourself as:
19
+
20
+ $ gem install jbcn
21
+
22
+ ## Usage
23
+
24
+ ```
25
+ require "jbcn"
26
+
27
+ client = Jbcn::Client.new
28
+ client.authenticate(code: "your code goes here")
29
+
30
+ # Clock in/out
31
+ client.clock(:in, group_id: "1")
32
+ p client.clock(:out, group_id: "1")
33
+ # => {"result"=>1, "state"=>"4", "current_status"=>"returned_home"}
34
+
35
+ # Clock in with extra info
36
+ client.clock(:in, group_id: "1", note: "my first night shift", night_shift: true)
37
+ ```
38
+
39
+ ## Development
40
+
41
+ After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
42
+
43
+ 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).
44
+
45
+ ## Contributing
46
+
47
+ Bug reports and pull requests are welcome on GitHub at https://github.com/uasi/jbcn.
48
+
49
+ ## License
50
+
51
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "jbcn"
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
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/jbcn.gemspec ADDED
@@ -0,0 +1,30 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'jbcn/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "jbcn"
8
+ spec.version = Jbcn::VERSION
9
+ spec.authors = ["Tomoki Aonuma"]
10
+ spec.email = ["uasi@uasi.jp"]
11
+
12
+ spec.summary = %q{Client for Jobcan}
13
+ spec.description = %q{Client for Jobcan}
14
+ spec.homepage = "https://github.com/uasi/jbcn"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
18
+ f.match(%r{^(test|spec|features)/})
19
+ end
20
+ spec.bindir = "exe"
21
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
+ spec.require_paths = ["lib"]
23
+
24
+ spec.add_development_dependency "bundler", "~> 1.13"
25
+ spec.add_development_dependency "rake", "~> 10.0"
26
+
27
+ spec.add_dependency "faraday", "~> 0.10"
28
+ spec.add_dependency "faraday_middleware", "~> 0.10"
29
+ spec.add_dependency "faraday-cookie_jar", "~> 0.0.6"
30
+ end
@@ -0,0 +1,66 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Jbcn
4
+ class Client
5
+ CLOCK_ENDPOINT = "https://ssl.jobcan.jp/employee/index/adit"
6
+
7
+ def authenticate(credentials)
8
+ if credentials.is_a?(Hash) && (code = credentials[:code])
9
+ credentials = CodeCredentials.new(code)
10
+ end
11
+ @token = credentials.authenticate(faraday)
12
+ end
13
+
14
+ def clock(in_out, group_id:, note: "", night_shift: false)
15
+ unless @token
16
+ fail(RuntimeError, "not authenticated")
17
+ end
18
+ unless [:in, :out].include?(in_out)
19
+ fail(ArgumentError, "expected :in or :out")
20
+ end
21
+
22
+ params = build_params(in_out, group_id.to_s, note.to_s, !!night_shift)
23
+ response = faraday.post(CLOCK_ENDPOINT, params) rescue fail(ClockError)
24
+
25
+ # Jobcan would normally return responses with status code 200
26
+ # regardless of whether the request was success or not,
27
+ # so status != 200 is really an unexpected condition.
28
+ unless response.status == 200
29
+ fail(ClockError.new(response: response))
30
+ end
31
+
32
+ result = JSON.parse(response.body) rescue
33
+ fail(ClockResponseParseError.new(response: response))
34
+
35
+ if result["errors"]
36
+ if result["errors"]["aditCount"] == "duplicate"
37
+ fail(ClockRequestDuplicateError.new(response: response, result: result))
38
+ end
39
+ fail(ClockError.new(response: response, result: result))
40
+ end
41
+
42
+ result
43
+ end
44
+
45
+ private
46
+
47
+ def build_params(in_out, group_id, note, night_shift)
48
+ {
49
+ adit_item: in_out == :in ? "work_start" : "work_end",
50
+ adit_group_id: group_id,
51
+ notice: note,
52
+ is_yakin: night_shift ? "1" : "0",
53
+ token: @token,
54
+ }
55
+ end
56
+
57
+ def faraday
58
+ @faraday ||= Faraday.new do |builder|
59
+ builder.request :url_encoded
60
+ builder.use FaradayMiddleware::FollowRedirects
61
+ builder.use :cookie_jar
62
+ builder.adapter Faraday.default_adapter
63
+ end
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,40 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Jbcn
4
+ class Credentials
5
+ attr_reader :token
6
+
7
+ def authenticate(_faraday)
8
+ fail(NotImplementedError)
9
+ end
10
+ end
11
+
12
+ class CodeCredentials < Credentials
13
+ AUTH_ENDPOINT = "https://ssl.jobcan.jp/employee?code="
14
+
15
+ attr_reader :code
16
+
17
+ def initialize(code)
18
+ @code = code
19
+ end
20
+
21
+ def authenticate(faraday)
22
+ response = faraday.get(AUTH_ENDPOINT + @code) rescue fail(AuthError)
23
+
24
+ unless response.status == 200
25
+ fail(AuthError, response: response)
26
+ end
27
+
28
+ token = response.body[/<input type="hidden" class="token" name="token" value="([^"]+)">/, 1]
29
+ unless token
30
+ fail(AuthTokenNotFoundError, response: response)
31
+ end
32
+
33
+ token
34
+ end
35
+ end
36
+
37
+ # TODO
38
+ #class UserCredentials < Credentials
39
+ #end
40
+ end
data/lib/jbcn/error.rb ADDED
@@ -0,0 +1,61 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Jbcn
4
+ class Error < StandardError
5
+ end
6
+
7
+ class AuthError < Error
8
+ attr_reader :response
9
+
10
+ def initialize(message = nil, response: nil)
11
+ super(message || default_message)
12
+ @response = response
13
+ end
14
+
15
+ private
16
+
17
+ def default_message
18
+ "auth failed"
19
+ end
20
+ end
21
+
22
+ class AuthTokenNotFoundError < AuthError
23
+ private
24
+
25
+ def default_message
26
+ "token not found (maybe jobcan's html structure has changed)"
27
+ end
28
+ end
29
+
30
+ class ClockError < Error
31
+ attr_reader :response, :result
32
+
33
+ def initialize(message = nil, response: nil, result: nil)
34
+ super(message || default_message)
35
+ @response = response
36
+ @result = result
37
+ end
38
+
39
+ private
40
+
41
+ def default_message
42
+ "request failed"
43
+ end
44
+ end
45
+
46
+ class ClockResponseParseError < ClockError
47
+ private
48
+
49
+ def default_message
50
+ "could not parse response"
51
+ end
52
+ end
53
+
54
+ class ClockRequestDuplicateError < ClockError
55
+ private
56
+
57
+ def default_message
58
+ "duplicate request"
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Jbcn
4
+ VERSION = "0.1.0"
5
+ end
data/lib/jbcn.rb ADDED
@@ -0,0 +1,9 @@
1
+ require "faraday"
2
+ require "faraday_middleware"
3
+ require "faraday-cookie_jar"
4
+ require "json"
5
+
6
+ require "jbcn/version"
7
+ require "jbcn/error"
8
+ require "jbcn/credentials"
9
+ require "jbcn/client"
metadata ADDED
@@ -0,0 +1,127 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jbcn
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Tomoki Aonuma
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-12-26 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.13'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.13'
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: faraday
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '0.10'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '0.10'
55
+ - !ruby/object:Gem::Dependency
56
+ name: faraday_middleware
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '0.10'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '0.10'
69
+ - !ruby/object:Gem::Dependency
70
+ name: faraday-cookie_jar
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: 0.0.6
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: 0.0.6
83
+ description: Client for Jobcan
84
+ email:
85
+ - uasi@uasi.jp
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - Gemfile
92
+ - LICENSE.txt
93
+ - README.md
94
+ - Rakefile
95
+ - bin/console
96
+ - bin/setup
97
+ - jbcn.gemspec
98
+ - lib/jbcn.rb
99
+ - lib/jbcn/client.rb
100
+ - lib/jbcn/credentials.rb
101
+ - lib/jbcn/error.rb
102
+ - lib/jbcn/version.rb
103
+ homepage: https://github.com/uasi/jbcn
104
+ licenses:
105
+ - MIT
106
+ metadata: {}
107
+ post_install_message:
108
+ rdoc_options: []
109
+ require_paths:
110
+ - lib
111
+ required_ruby_version: !ruby/object:Gem::Requirement
112
+ requirements:
113
+ - - ">="
114
+ - !ruby/object:Gem::Version
115
+ version: '0'
116
+ required_rubygems_version: !ruby/object:Gem::Requirement
117
+ requirements:
118
+ - - ">="
119
+ - !ruby/object:Gem::Version
120
+ version: '0'
121
+ requirements: []
122
+ rubyforge_project:
123
+ rubygems_version: 2.6.8
124
+ signing_key:
125
+ specification_version: 4
126
+ summary: Client for Jobcan
127
+ test_files: []