gisterday 1.0.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.
Files changed (4) hide show
  1. checksums.yaml +7 -0
  2. data/bin/gist +64 -0
  3. data/lib/gist.rb +117 -0
  4. metadata +101 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 029def42789fbb2a07ff4a658bfab79821a3d59b
4
+ data.tar.gz: 1bcdae20876b6f8406c5bbc4aa446cd3693bae27
5
+ SHA512:
6
+ metadata.gz: b51dd9cce41f92a425b9dd962f56c61d226884a16a4167273ed90eee9ae130d02ee250c212fac2c55d7cc0d66d5407ae24c3273a2d2a311f357f4e08861bb428
7
+ data.tar.gz: 1350a3dd1273c6c29970e0a2108012a6d1ffc90c83c8e2e64ace5e3f87498137560b8a49a2260c23dfed3122c5e4b0e6e3e227c9ee3a63ee9ebf88b91a964f2c
@@ -0,0 +1,64 @@
1
+ #!/usr/bin/env ruby
2
+ require_relative '../lib/gist.rb'
3
+
4
+ options = {}
5
+
6
+ opt_parser = OptionParser.new do |opt|
7
+ opt.banner = "Usage: gist [COMMAND] [OPTIONS]"
8
+ opt.separator ""
9
+ opt.separator "Commands"
10
+ opt.separator " gist <blank>: create a new gist in text editor"
11
+ opt.separator " gist <file>: create a new gist using an existing file"
12
+ opt.separator " gist login: log in to your github account. Saves token to ~/.gisterday"
13
+ opt.separator " gist help: show the help options"
14
+ opt.separator ""
15
+ opt.separator "Options"
16
+
17
+ opt.on("-a","create the gist anonymously so that it isn't associated with your GitHub account") do
18
+ options[:anonymous] = true
19
+ end
20
+
21
+ opt.on("-d","--description DESCRIPTION","set the gist description") do |description|
22
+ options[:description] = description
23
+ end
24
+
25
+ opt.on("-v","--verbose","view full response of the http request") do
26
+ options[:verbose] = true
27
+ end
28
+ end
29
+
30
+ opt_parser.parse!
31
+
32
+ if Dir.entries(".").include? ARGV[0]
33
+ file = ARGV[0]
34
+ gist = Gisterday::Gist.new(options)
35
+ gist.add_file(file)
36
+ gist.push_gist
37
+ else
38
+ case ARGV[0]
39
+ when "login"
40
+ puts "Logging in..."
41
+ puts "What is your username?"
42
+ username = STDIN.gets.chomp
43
+ `stty -echo`
44
+ puts "What is your password?"
45
+ password = STDIN.gets.chomp
46
+ `stty echo`
47
+ authentication = Gisterday::GitHubAuth.new(username,password)
48
+ authentication.begin_authentication
49
+ when "help"
50
+ puts opt_parser
51
+ else
52
+ file = ARGV[0] || "gisterday_file.rb"
53
+ # if `file` gets saved, it will be in the current directory
54
+ system %Q(vim +"put ='# When you save this file, a gist will be created.'" #{file})
55
+
56
+ # check if user saved and create gist if so
57
+ if Dir.entries(".").include? file
58
+ gist = Gisterday::Gist.new(options)
59
+ gist.add_file(file)
60
+ gist.push_gist
61
+ end
62
+ end
63
+ end
64
+
@@ -0,0 +1,117 @@
1
+ require "optparse"
2
+ require "json"
3
+ require "httparty"
4
+
5
+ module Gisterday
6
+ AUTH_URL = "https://api.github.com/authorizations"
7
+ GIST_URL = "https://api.github.com/gists"
8
+ TOKEN_LOCATION = ENV['HOME'] + "/.gisterday"
9
+
10
+ class GitHubAuth
11
+ attr_accessor :username, :password, :two_factor, :base_request_options
12
+
13
+ def initialize(username,password)
14
+ @username = username
15
+ @password = password
16
+ @base_request_options = {
17
+ :headers => {
18
+ 'User-Agent' => 'gisterday'
19
+ },
20
+ :basic_auth => {
21
+ :username => @username,
22
+ :password => @password
23
+ },
24
+ :body => {
25
+ :note => 'Gisterday CLI for creating gists',
26
+ :scopes => ["gist"]
27
+ }.to_json
28
+ }
29
+ end
30
+
31
+ def auth_request(options)
32
+ HTTParty.post(AUTH_URL,options)
33
+ end
34
+
35
+ def begin_authentication
36
+ response = auth_request(@base_request_options)
37
+
38
+ if response.code == 401 && response.headers["X-GitHub-OTP"].match(/required/)
39
+ puts "Enter Two-Factor Auth Code:"
40
+ @two_factor = STDIN.gets.chomp
41
+ finish_authentication
42
+ elsif response.code == 201 && response['token']
43
+ write_token_file(TOKEN_LOCATION,response['token'])
44
+ else
45
+ puts "Request returned with the code: #{response.code}"
46
+ end
47
+ end
48
+
49
+ def finish_authentication
50
+ @base_request_options[:headers]['X-GitHub-OTP'] = @two_factor
51
+ response = auth_request(@base_request_options)
52
+
53
+ if response.code == 201
54
+ write_token_file(TOKEN_LOCATION,response['token'])
55
+ else
56
+ puts "Request returned with the code: #{response.code}"
57
+ puts response
58
+ end
59
+ end
60
+
61
+ def write_token_file(location,token)
62
+ # For persistence, write the token to a file '.gisterday' in the user's home directory
63
+ File.open(location, "w") { |f| f.write(token) }
64
+ puts "Logged in as #{@username}"
65
+ end
66
+ end
67
+
68
+ class Gist
69
+ attr_accessor :options, :headers, :body
70
+
71
+ def initialize(options)
72
+ @options = options
73
+ @headers = {'User-Agent' => 'gisterday'}
74
+ @body = {
75
+ 'description' => @options[:description] || "Gist created using Gisterday",
76
+ 'public' => true,
77
+ 'files' => {}
78
+ }
79
+ end
80
+
81
+ def read_file(file)
82
+ File.read(file)
83
+ end
84
+
85
+ def get_token
86
+ begin
87
+ read_file(TOKEN_LOCATION)
88
+ rescue
89
+ false
90
+ end
91
+ end
92
+
93
+ def add_file(file)
94
+ @body['files'][file] = {"content" => read_file(file)}
95
+ end
96
+
97
+ def push_gist
98
+ if get_token && @options[:anonymous] == nil
99
+ @headers['Authorization'] = "token #{get_token}"
100
+ end
101
+
102
+ response = HTTParty.post(GIST_URL,:body => @body.to_json, :headers => @headers)
103
+ format_response(response)
104
+ end
105
+
106
+ def format_response(response)
107
+ STDOUT.puts %Q{
108
+
109
+ #{JSON.parse(response.body) if @options[:verbose]}
110
+
111
+
112
+ New Gist created at:
113
+ #{JSON.parse(response.body)['html_url']}
114
+ }
115
+ end
116
+ end
117
+ end
metadata ADDED
@@ -0,0 +1,101 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: gisterday
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Michael Niday
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-09-25 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: httparty
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
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: json
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '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: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: webmock/rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: A command line tool for creating Gists
70
+ email: michael.niday@gmail.com
71
+ executables:
72
+ - gist
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - bin/gist
77
+ - lib/gist.rb
78
+ homepage: https://github.com/mjniday/gisterday
79
+ licenses: []
80
+ metadata: {}
81
+ post_install_message:
82
+ rdoc_options: []
83
+ require_paths:
84
+ - lib
85
+ required_ruby_version: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ required_rubygems_version: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ version: '0'
95
+ requirements: []
96
+ rubyforge_project:
97
+ rubygems_version: 2.5.1
98
+ signing_key:
99
+ specification_version: 4
100
+ summary: A command line tool for creating Gists
101
+ test_files: []