git-sleep 0.2.1 → 0.3.0

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
2
  SHA1:
3
- metadata.gz: 703ea02488e136772457a35b046e5fefb597719b
4
- data.tar.gz: 2faeac69b7894c3f375cd9044812e80d7d760c3a
3
+ metadata.gz: 03761eab75a16919876defde696872ddbb25abed
4
+ data.tar.gz: 96806446a53e17a10e9623b9769131319c292072
5
5
  SHA512:
6
- metadata.gz: 2d402cec651060270e066ef680ae5a651e65db165bc5e76b9430141888636845694aaa0c03caad0ffa7aedb0c155b4d83975f2f1a6e503a0e485714b91fb06ac
7
- data.tar.gz: 77010c74581c7a6ba38ebc60af011f7dc7d25e6afe7e59ef4770e3d94c66f45f5995357f910cfdd51ec707edeb567de13e9278af0b207199874cbe6ea95fc963
6
+ metadata.gz: 51f064e90fc2b9d7df55e9d7d771e43260f40414dd725669670a2002026d9bb09677ea0336db83d5f27d53b0ca7787aabfb3110f44b9017153d947c95d97301c
7
+ data.tar.gz: 55e8a184cd8cacf9a6fc933bd207bf9214e8e341ad0056632a5cf069948443e0c661eca5dcfb2be6b36ab14d9d1a144b69be54967b39c3070e7d3a863f225edb
data/.gitignore CHANGED
@@ -1,3 +1,5 @@
1
1
  .DS_STORE
2
2
  *.gem
3
- /spec/spec_helper/support/.netrc
3
+ /spec/support/.netrc
4
+ Gemfile.lock
5
+ tags
data/README.md CHANGED
@@ -1,7 +1,14 @@
1
1
  # git sleep
2
2
 
3
- [![Build
4
- Status](https://travis-ci.org/maxjacobson/git-sleep-gem.svg?branch=add-travis)](https://travis-ci.org/maxjacobson/git-sleep-gem)
3
+ [![Build Status](https://travis-ci.org/maxjacobson/git-sleep-gem.svg?branch=master)](https://travis-ci.org/maxjacobson/git-sleep-gem)
4
+ [![Gem Version](https://badge.fury.io/rb/git-sleep.svg)](http://badge.fury.io/rb/git-sleep)
5
+ [![Code Climate](https://codeclimate.com/github/maxjacobson/git-sleep-gem/badges/gpa.svg)](https://codeclimate.com/github/maxjacobson/git-sleep-gem)
6
+ [![Test Coverage](https://codeclimate.com/github/maxjacobson/git-sleep-gem/badges/coverage.svg)](https://codeclimate.com/github/maxjacobson/git-sleep-gem)
7
+
8
+ (code climate score is higher than it should be, I think, because it's ignoring
9
+ the non `*.rb` files)
10
+
11
+ ## installation:
5
12
 
6
13
  `gem install git-sleep`
7
14
 
@@ -2,73 +2,5 @@
2
2
 
3
3
  require File.expand_path('../../lib/git-sleep', __FILE__)
4
4
 
5
- auth = GitSleep::Authorizer.instance
6
-
7
- help_text = "Available commands:
8
- git sleep authorize
9
- git sleep init"
10
-
11
- CURRENT_PATH = File.expand_path('.')
12
- PATH_TO_THIS_REPOS_HOOKS = "#{CURRENT_PATH}/.git/hooks"
13
-
14
- def install_hook(event)
15
- hook_code = File.read(File.expand_path("../../hooks/#{event}", __FILE__))
16
- path_to_new_hook = File.expand_path("#{PATH_TO_THIS_REPOS_HOOKS}/#{event}")
17
- if File.exist?(path_to_new_hook)
18
- puts "You already have a #{event} hook and we don't want to overwrite it..."
19
- print 'Is it okay? [y/n] '
20
- ok = $stdin.gets.chomp
21
- return unless ok == 'y'
22
- end
23
- File.open(path_to_new_hook, 'w') do |f|
24
- f.write hook_code
25
- end
26
- `chmod a+x #{path_to_new_hook}`
27
- puts 'Hook installed and made executable'
28
- end
29
-
30
- case ARGV.length
31
- when 0
32
- puts help_text
33
- when 1
34
- case ARGV.first.downcase.to_sym
35
- when :init
36
-
37
- unless Dir.entries(CURRENT_PATH).include?('.git')
38
- puts 'This is not a git repo. Would you like to initialize one?'
39
- print '[y/n] '
40
- ok = $stdin.gets.chomp
41
- if ok == 'y'
42
- `git init`
43
- else
44
- exit 0
45
- end
46
- end
47
-
48
- puts 'Install the pre-commit hook that prevents ' \
49
- "commits when you haven't sleep enough?"
50
- print 'Is it okay? [y/n] '
51
- ok = $stdin.gets.chomp
52
- install_hook('pre-commit') if ok == 'y'
53
-
54
- puts 'Install the post-commit hook that adds ' \
55
- 'git notes about your sleep data?'
56
- print 'Is it okay? [y/n] '
57
- ok = $stdin.gets.chomp
58
- install_hook('post-commit') if ok == 'y'
59
-
60
- when :authorize
61
- puts "Visit #{GitSleep::OUR_SITE} to get the necessary information"
62
- puts 'what is your xid?'
63
- print '> '
64
- xid = $stdin.gets.chomp
65
- auth.xid = xid
66
- when :'-v'
67
- puts "Git Sleep v#{GitSleep::VERSION}"
68
- else # unrecognized command
69
- puts help_text
70
- end
71
- else # other amounts of arguments
72
- puts help_text
73
- end
5
+ GitSleep::Cli.start
74
6
 
@@ -33,5 +33,6 @@ Gem::Specification.new do |git_sleep|
33
33
  git_sleep.add_development_dependency 'rubocop', '~> 0.27'
34
34
  git_sleep.add_development_dependency 'rspec', '~> 3.1'
35
35
  git_sleep.add_development_dependency 'pry', '~> 0.10'
36
+ git_sleep.add_development_dependency 'codeclimate-test-reporter', '~> 0.4'
36
37
  end
37
38
 
@@ -1,36 +1,21 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
3
  require 'git-sleep'
4
- require 'httparty'
5
4
 
6
- auth = GitSleep::Authorizer.instance
5
+ comm = GitSleep::Communicator.instance
7
6
 
8
- begin
9
- response = HTTParty.get(
10
- 'http://www.gitsleep.com/api/need_sleep',
11
- body: {
12
- xid: auth.xid
13
- }
14
- )
15
- rescue StandardError => _
7
+ unless comm.can_connect?
16
8
  puts "Can't connect to gitsleep.com"
17
9
  exit 0
18
10
  end
19
11
 
20
- if response.response.code.to_i == 200
21
- data = response.parsed_response
22
-
23
- `git notes --ref=gitsleep add -m "On #{data['sleep24']} hours sleep"`
12
+ if comm.happy_response?
13
+ `git notes --ref=gitsleep add -m "On #{comm.hours_of_sleep} hours sleep"`
24
14
  remotes = `git remote`.split("\n")
25
15
  remotes.each do |remote|
26
16
  `git push #{remote} refs/notes/* 2> /dev/null`
27
17
  end
28
-
29
- elsif response.response.code.to_i == 401
30
- puts "Must first authorize at #{GitSleep::OUR_SITE}"
31
- puts 'Then run `git sleep authorize`'
32
- else
33
- # could not communicate with our server
34
- # uh oh!
18
+ elsif comm.not_signed_up_response?
19
+ puts comm.not_signed_up_message
35
20
  end
36
21
 
@@ -1,40 +1,26 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
3
  require 'git-sleep'
4
- require 'httparty'
5
4
 
6
- auth = GitSleep::Authorizer.instance
5
+ comm = GitSleep::Communicator.instance
7
6
 
8
- begin
9
- response = HTTParty.get(
10
- 'http://www.gitsleep.com/api/need_sleep',
11
- body: {
12
- xid: auth.xid
13
- }
14
- )
15
- rescue StandardError => _
7
+ unless comm.can_connect?
16
8
  puts "Can't connect to gitsleep.com"
17
9
  exit 0
18
10
  end
19
11
 
20
- if response.response.code.to_i == 200
21
- data = response.parsed_response
22
- if data['can_commit']
23
- puts "Great job #{ENV['USER']}! " \
24
- "You slept #{data['sleep24']} hours in the last 24 hours"
12
+ if comm.happy_response?
13
+ if comm.can_commit?
14
+ puts "Great job #{comm.username}! " \
15
+ "You slept #{comm.hours_of_sleep} hours in the last 24 hours"
25
16
  else
26
- puts "You have only slept #{data['sleep24']} hours in the last 24 hours!!"
27
- puts "You'll have to git some sleep #{ENV['USER']} " \
28
- 'before you can commit again'
29
- puts "(if you're sure you want to commit right now, " \
17
+ puts "You have only slept #{comm.hours_of_sleep} hours in the " \
18
+ "last 24 hours!!\nYou'll have to git some sleep #{ENV['USER']} before" \
19
+ "you can commit again\n(if you're sure you want to commit right now, " \
30
20
  'run: git commit --no-verify)'
31
21
  exit(1)
32
22
  end
33
- elsif response.response.code.to_i == 401
34
- puts "Must first authorize at #{GitSleep::OUR_SITE}"
35
- puts 'Then run `git sleep authorize`'
36
- else
37
- # could not communicate with our server
38
- # uh oh!
23
+ elsif comm.not_signed_up_response?
24
+ puts comm.not_signed_up_message
39
25
  end
40
26
 
@@ -1,5 +1,8 @@
1
+ require_relative './git-sleep/metadata'
1
2
  require_relative './git-sleep/exceptions'
2
3
  require_relative './git-sleep/authorizer'
4
+ require_relative './git-sleep/cli'
5
+ require_relative './git-sleep/communicator'
3
6
 
4
7
  module GitSleep
5
8
  end
@@ -0,0 +1,69 @@
1
+ require_relative './hook_installer'
2
+
3
+ module GitSleep
4
+ class Cli
5
+ def self.start
6
+ if command = ARGV.first
7
+ command = command.to_sym
8
+ end
9
+ new.start(command)
10
+ end
11
+
12
+ def start(command)
13
+ case command
14
+ when :init then init!
15
+ when :authorize then authorize!
16
+ when :'-v' then version!
17
+ else
18
+ send_help!
19
+ end
20
+ end
21
+
22
+ private
23
+
24
+ def version!
25
+ puts "Git Sleep v#{GitSleep::VERSION}"
26
+ end
27
+
28
+ def authorize!
29
+ puts "Visit #{GitSleep::OUR_SITE} to get the necessary information"
30
+ puts 'what is your xid?'
31
+ print '> '
32
+ auth.xid = $stdin.gets.chomp
33
+ end
34
+
35
+ def init!
36
+ raise NotGitRepoError unless Dir.entries(current_path).include?('.git')
37
+
38
+ puts 'Install the pre-commit hook that prevents commits when you' \
39
+ " haven't slept enough?"
40
+ HookInstaller.install('pre-commit') if user_consents?
41
+
42
+ puts 'Install the post-commit hook that adds git notes about your sleep' \
43
+ ' data?'
44
+ HookInstaller.install('post-commit') if user_consents?
45
+ end
46
+
47
+ def send_help!
48
+ puts help_text
49
+ end
50
+
51
+ def auth
52
+ GitSleep::Authorizer.instance
53
+ end
54
+
55
+ def help_text
56
+ "Available commands:\n git sleep authorize\n git sleep init"
57
+ end
58
+
59
+ def current_path
60
+ File.expand_path('.')
61
+ end
62
+
63
+ def user_consents?
64
+ print 'Is it okay? [y/n] '
65
+ $stdin.gets.chomp == 'y'
66
+ end
67
+ end
68
+ end
69
+
@@ -0,0 +1,73 @@
1
+ require 'singleton'
2
+ require 'httparty'
3
+
4
+ module GitSleep
5
+ class Communicator
6
+ include Singleton
7
+
8
+ def can_connect?
9
+ reset_data!
10
+ true
11
+ # TODO: be more specific
12
+ # only rescue httparty error related to inability to connect
13
+ rescue StandardError
14
+ false
15
+ end
16
+
17
+ def happy_response?
18
+ response.code == 200
19
+ end
20
+
21
+ def not_signed_up_response?
22
+ response.code == 401
23
+ end
24
+
25
+ def not_signed_up_message
26
+ "Must first authorize at #{GitSleep::OUR_SITE}\n" \
27
+ 'Then run `git sleep authorize`'
28
+ end
29
+
30
+ def can_commit?
31
+ data['can_commit']
32
+ end
33
+
34
+ def hours_of_sleep
35
+ data['sleep24']
36
+ end
37
+
38
+ def username
39
+ ENV['USER']
40
+ end
41
+
42
+ private
43
+
44
+ def data
45
+ @data ||= response.parsed_response
46
+ end
47
+
48
+ def response
49
+ @response ||= HTTParty.get(
50
+ need_sleep_url,
51
+ timeout: 2,
52
+ body: {
53
+ xid: auth.xid
54
+ }
55
+ )
56
+ end
57
+
58
+ def reset_data!
59
+ @response = nil
60
+ @data = nil
61
+ data
62
+ end
63
+
64
+ def need_sleep_url
65
+ 'http://www.gitsleep.com/api/need_sleep'
66
+ end
67
+
68
+ def auth
69
+ Authorizer.instance
70
+ end
71
+ end
72
+ end
73
+
@@ -1,4 +1,5 @@
1
1
  module GitSleep
2
2
  class NotSetupError < StandardError; end
3
+ class NotGitRepoError < StandardError; end
3
4
  end
4
5
 
@@ -0,0 +1,60 @@
1
+ module GitSleep
2
+ class HookInstaller
3
+ def self.install(event)
4
+ new(event).install
5
+ end
6
+
7
+ attr_reader :event
8
+
9
+ def initialize(event)
10
+ @event = event
11
+ end
12
+
13
+ def install
14
+ (return unless ok_to_overwrite?) if File.exist?(new_hook_path)
15
+ move_hook_code_into_place
16
+ make_hook_executable
17
+ puts 'Hook installed and made executable'
18
+ end
19
+
20
+ private
21
+
22
+ def ok_to_overwrite?
23
+ puts already_exists_warning_text
24
+ print 'Is it okay? [y/n] '
25
+ $stdin.gets.chomp == 'y'
26
+ end
27
+
28
+ def move_hook_code_into_place
29
+ File.open(new_hook_path, 'w') do |f|
30
+ f.write hook_code
31
+ end
32
+ end
33
+
34
+ def make_hook_executable
35
+ `chmod a+x #{new_hook_path}`
36
+ end
37
+
38
+ def hook_code
39
+ File.read(hook_code_path)
40
+ end
41
+
42
+ def hook_code_path
43
+ File.expand_path("../../../hooks/#{event}", __FILE__)
44
+ end
45
+
46
+ def new_hook_path
47
+ current_path + '/.git/hooks/' + event
48
+ end
49
+
50
+ # TODO: make sure this will be the path the user is currently in
51
+ def current_path
52
+ File.expand_path('.')
53
+ end
54
+
55
+ def already_exists_warning_text
56
+ "you already have a #{event} hook and we don't want to overwrite it..."
57
+ end
58
+ end
59
+ end
60
+
@@ -1,5 +1,5 @@
1
1
  module GitSleep
2
- VERSION = '0.2.1'
3
- OUR_SITE = 'http://www.gitsleep.com'
2
+ VERSION ||= '0.3.0'
3
+ OUR_SITE ||= 'http://www.gitsleep.com'
4
4
  end
5
5
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: git-sleep
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Max Jacobson
@@ -10,78 +10,92 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2014-11-16 00:00:00.000000000 Z
13
+ date: 2014-12-21 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: httparty
17
17
  requirement: !ruby/object:Gem::Requirement
18
18
  requirements:
19
- - - "~>"
19
+ - - ~>
20
20
  - !ruby/object:Gem::Version
21
21
  version: '0.13'
22
22
  type: :runtime
23
23
  prerelease: false
24
24
  version_requirements: !ruby/object:Gem::Requirement
25
25
  requirements:
26
- - - "~>"
26
+ - - ~>
27
27
  - !ruby/object:Gem::Version
28
28
  version: '0.13'
29
29
  - !ruby/object:Gem::Dependency
30
30
  name: netrc
31
31
  requirement: !ruby/object:Gem::Requirement
32
32
  requirements:
33
- - - "~>"
33
+ - - ~>
34
34
  - !ruby/object:Gem::Version
35
35
  version: '0.8'
36
36
  type: :runtime
37
37
  prerelease: false
38
38
  version_requirements: !ruby/object:Gem::Requirement
39
39
  requirements:
40
- - - "~>"
40
+ - - ~>
41
41
  - !ruby/object:Gem::Version
42
42
  version: '0.8'
43
43
  - !ruby/object:Gem::Dependency
44
44
  name: rubocop
45
45
  requirement: !ruby/object:Gem::Requirement
46
46
  requirements:
47
- - - "~>"
47
+ - - ~>
48
48
  - !ruby/object:Gem::Version
49
49
  version: '0.27'
50
50
  type: :development
51
51
  prerelease: false
52
52
  version_requirements: !ruby/object:Gem::Requirement
53
53
  requirements:
54
- - - "~>"
54
+ - - ~>
55
55
  - !ruby/object:Gem::Version
56
56
  version: '0.27'
57
57
  - !ruby/object:Gem::Dependency
58
58
  name: rspec
59
59
  requirement: !ruby/object:Gem::Requirement
60
60
  requirements:
61
- - - "~>"
61
+ - - ~>
62
62
  - !ruby/object:Gem::Version
63
63
  version: '3.1'
64
64
  type: :development
65
65
  prerelease: false
66
66
  version_requirements: !ruby/object:Gem::Requirement
67
67
  requirements:
68
- - - "~>"
68
+ - - ~>
69
69
  - !ruby/object:Gem::Version
70
70
  version: '3.1'
71
71
  - !ruby/object:Gem::Dependency
72
72
  name: pry
73
73
  requirement: !ruby/object:Gem::Requirement
74
74
  requirements:
75
- - - "~>"
75
+ - - ~>
76
76
  - !ruby/object:Gem::Version
77
77
  version: '0.10'
78
78
  type: :development
79
79
  prerelease: false
80
80
  version_requirements: !ruby/object:Gem::Requirement
81
81
  requirements:
82
- - - "~>"
82
+ - - ~>
83
83
  - !ruby/object:Gem::Version
84
84
  version: '0.10'
85
+ - !ruby/object:Gem::Dependency
86
+ name: codeclimate-test-reporter
87
+ requirement: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - ~>
90
+ - !ruby/object:Gem::Version
91
+ version: '0.4'
92
+ type: :development
93
+ prerelease: false
94
+ version_requirements: !ruby/object:Gem::Requirement
95
+ requirements:
96
+ - - ~>
97
+ - !ruby/object:Gem::Version
98
+ version: '0.4'
85
99
  description: Uses Jawbone to figure out if you are awake enough to code
86
100
  email:
87
101
  - max@hardscrabble.net
@@ -92,17 +106,20 @@ executables:
92
106
  extensions: []
93
107
  extra_rdoc_files: []
94
108
  files:
95
- - "./.gitignore"
96
- - "./README.md"
97
- - "./bin/git-sleep"
98
- - "./git-sleep.gemspec"
99
- - "./hooks/post-commit"
100
- - "./hooks/pre-commit"
101
- - "./lib/git-sleep.rb"
102
- - "./lib/git-sleep/authorizer.rb"
103
- - "./lib/git-sleep/credentials_file.rb"
104
- - "./lib/git-sleep/exceptions.rb"
105
- - "./lib/git-sleep/metadata.rb"
109
+ - ./.gitignore
110
+ - ./README.md
111
+ - ./bin/git-sleep
112
+ - ./git-sleep.gemspec
113
+ - ./hooks/post-commit
114
+ - ./hooks/pre-commit
115
+ - ./lib/git-sleep.rb
116
+ - ./lib/git-sleep/authorizer.rb
117
+ - ./lib/git-sleep/cli.rb
118
+ - ./lib/git-sleep/communicator.rb
119
+ - ./lib/git-sleep/credentials_file.rb
120
+ - ./lib/git-sleep/exceptions.rb
121
+ - ./lib/git-sleep/hook_installer.rb
122
+ - ./lib/git-sleep/metadata.rb
106
123
  - bin/git-sleep
107
124
  homepage: http://www.gitsleep.com
108
125
  licenses:
@@ -114,19 +131,20 @@ require_paths:
114
131
  - lib
115
132
  required_ruby_version: !ruby/object:Gem::Requirement
116
133
  requirements:
117
- - - ">="
134
+ - - '>='
118
135
  - !ruby/object:Gem::Version
119
136
  version: 2.0.0
120
137
  required_rubygems_version: !ruby/object:Gem::Requirement
121
138
  requirements:
122
- - - ">="
139
+ - - '>='
123
140
  - !ruby/object:Gem::Version
124
141
  version: '0'
125
142
  requirements: []
126
143
  rubyforge_project:
127
- rubygems_version: 2.2.2
144
+ rubygems_version: 2.4.1
128
145
  signing_key:
129
146
  specification_version: 4
130
147
  summary: Uses Jawbone to figure out if you are awake enoughto code based on your sleep
131
148
  data
132
149
  test_files: []
150
+ has_rdoc: