igist 0.0.1 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,6 @@
1
+ language: ruby
2
+ rvm:
3
+ - jruby-19mode
4
+ - rbx-19mode
5
+ - 1.9.3
6
+ - 2.0.0
data/README.md CHANGED
@@ -1,11 +1,18 @@
1
1
  # IGist
2
2
 
3
+ [![Gem Version](https://badge.fury.io/rb/igist.png)](http://badge.fury.io/rb/igist)
4
+ [![Build Status](https://api.travis-ci.org/wangyuhere/igist.png?branch=master)](https://api.travis-ci.org/wangyuhere/igist.png?branch=master)
5
+ [![Code Climate](https://codeclimate.com/github/wangyuhere/igist.png)](https://codeclimate.com/github/wangyuhere/igist.png)
6
+ [![Dependency Status](https://gemnasium.com/wangyuhere/igist.png)](https://gemnasium.com/wangyuhere/igist)
7
+
3
8
  IGist is a command line tool used to search your gists and starred gists by keyword of gist description. All your gists will be indexed using simple inversed index algorithm. And the index data is saved locally (~/.igist).
4
9
 
5
10
  ## Installation
6
11
 
7
12
  $ gem install igist
8
13
 
14
+ Note: ruby 1.9.3+ is required.
15
+
9
16
  ## Usage
10
17
 
11
18
  Before search your gists, authorize and index first:
@@ -17,6 +24,14 @@ Search by keywords in description
17
24
  $ igist -s ruby
18
25
  $ igist -s "ruby rails"
19
26
 
27
+ Print a single gist by id
28
+
29
+ $ igist -p 1
30
+
31
+ Open a single gist in the browser by gist id
32
+
33
+ $ igist -o 1
34
+
20
35
  For other options
21
36
 
22
37
  $ igist -h
@@ -12,32 +12,16 @@ module IGist
12
12
  end
13
13
 
14
14
  def create_authorization(username, password)
15
- request = Net::HTTP::Post.new(authorization_url)
16
- request.body = JSON.dump({
17
- scopes: [:gist],
18
- note: "The igist gem",
19
- note_url: ""
20
- })
21
- request.content_type = "application/json"
22
- request.basic_auth(username, password)
15
+ request = authorization_request(username, password)
23
16
  response = send_request(request)
17
+ response = send_otp(request) if response.is_a?(Net::HTTPUnauthorized) && response["X-GitHub-OTP"]
24
18
 
25
- if response.is_a?(Net::HTTPUnauthorized) && response["X-GitHub-OTP"]
26
- print "two factor authentication code: "
27
- otp = $stdin.gets.strip
28
- puts ""
29
- request["X-GitHub-OTP"] = otp
30
- response = send_request(request)
31
- end
19
+ raise "Can not authorize because: #{response.body}" unless response.is_a?(Net::HTTPCreated)
32
20
 
33
- if response.is_a?(Net::HTTPCreated)
34
- result = JSON.parse(response.body)
35
- @token = result["token"]
36
- @username = username
37
- yield result if block_given?
38
- else
39
- raise "Can not authorize because: #{response.body}"
40
- end
21
+ result = JSON.parse(response.body)
22
+ @token = result["token"]
23
+ @username = username
24
+ yield result if block_given?
41
25
  end
42
26
 
43
27
  def each_my_gist(&block)
@@ -48,8 +32,36 @@ module IGist
48
32
  each_gist(starred_gists_url, &block)
49
33
  end
50
34
 
35
+ def single_gist(id)
36
+ request = Net::HTTP::Get.new(single_gist_url(id))
37
+ response = send_request(request)
38
+
39
+ raise "Can not get single gist because: #{response.body}" unless response.is_a?(Net::HTTPOK)
40
+ JSON.parse(response.body)
41
+ end
42
+
51
43
  private
52
44
 
45
+ def authorization_request(username, password)
46
+ request = Net::HTTP::Post.new(authorization_url)
47
+ request.body = JSON.dump({
48
+ scopes: [:gist],
49
+ note: "The igist gem",
50
+ note_url: ""
51
+ })
52
+ request.content_type = "application/json"
53
+ request.basic_auth(username, password)
54
+ request
55
+ end
56
+
57
+ def send_otp(request)
58
+ print "two factor authentication code: "
59
+ otp = $stdin.gets.strip
60
+ puts ""
61
+ request["X-GitHub-OTP"] = otp
62
+ send_request(request)
63
+ end
64
+
53
65
  def each_gist(url, &block)
54
66
  request = Net::HTTP::Get.new(url)
55
67
  connection.start do |http|
@@ -91,15 +103,19 @@ module IGist
91
103
  end
92
104
 
93
105
  def gists_url
94
- append_token "#{api_url}/users/#{@username}/gists"
106
+ build_url "/users/#{@username}/gists"
95
107
  end
96
108
 
97
109
  def starred_gists_url
98
- append_token "#{api_url}/gists/starred"
110
+ build_url "/gists/starred"
111
+ end
112
+
113
+ def single_gist_url(id)
114
+ build_url "/gists/#{id}"
99
115
  end
100
116
 
101
- def append_token(url)
102
- "#{url}?access_token=#{@token}"
117
+ def build_url(uri)
118
+ "#{api_url}#{uri}?access_token=#{@token}"
103
119
  end
104
120
 
105
121
  end
@@ -1,4 +1,5 @@
1
1
  require "io/console"
2
+ require "open-uri"
2
3
  require "optparse"
3
4
  require "igist/version"
4
5
  require "igist/igist"
@@ -23,20 +24,24 @@ You can also "igist -i -s KEYWORD" to first index and then search.
23
24
 
24
25
  Usage: igist [-i|-u|-v]
25
26
  igist [-t|-a] -s KEYWORD
27
+ igist -p ID
26
28
 
27
29
  Options:
28
30
  EOS
29
31
 
30
32
  opt.on("-v", "--version", "Show version") do
31
33
  puts VERSION
34
+ exit
32
35
  end
33
36
 
34
37
  opt.on("-i", "--index", "Index your gists") do
35
38
  index
39
+ exit
36
40
  end
37
41
 
38
42
  opt.on("-u", "--auth", "Authorize igist") do
39
43
  authorize
44
+ exit
40
45
  end
41
46
 
42
47
  opt.on("-s", "--search KEYWORD", "Search your own gists by keyword in description") do |keyword|
@@ -51,8 +56,19 @@ Options:
51
56
  options[:all] = true
52
57
  end
53
58
 
59
+ opt.on("-p", "--print ID", "Print gist content by gist id") do |id|
60
+ print_gist(id)
61
+ exit
62
+ end
63
+
64
+ opt.on("-o", "--open ID", "Open gist in the browser") do |id|
65
+ open_gist(id)
66
+ exit
67
+ end
68
+
54
69
  opt.on("--clear", "Remove your gists index data locally") do
55
70
  clear
71
+ exit
56
72
  end
57
73
 
58
74
  end
@@ -96,6 +112,21 @@ Options:
96
112
  end
97
113
  end
98
114
 
115
+ def print_gist(id)
116
+ gist = @igist.single_gist(id)
117
+ gist['files'].each_pair { |file, data| print_gist_file(file, data) }
118
+ rescue => e
119
+ puts e.message
120
+ end
121
+
122
+ def open_gist(id)
123
+ gist = @igist.single_gist(id)
124
+ command = RUBY_PLATFORM =~ /darwin/ ? 'open' : 'firefox'
125
+ `#{command} #{gist["html_url"]}`
126
+ rescue => e
127
+ puts e.message
128
+ end
129
+
99
130
  def clear
100
131
  @igist.clear
101
132
  puts "Your local index files are deleted!"
@@ -103,6 +134,16 @@ Options:
103
134
 
104
135
  private
105
136
 
137
+ def print_gist_file(file, data)
138
+ len = file.size
139
+ puts
140
+ puts "*" * len
141
+ puts file
142
+ puts "*" * len
143
+ puts
144
+ puts open(data["raw_url"]).read
145
+ end
146
+
106
147
  def print_result(title, result)
107
148
  puts "#{title}: "
108
149
  if result.empty?
@@ -62,16 +62,8 @@ module IGist
62
62
  def index
63
63
  @my_gists = {}
64
64
  @starred_gists = {}
65
-
66
- api.each_my_gist { |g| @my_gists[g["id"]] = g["description"]}
67
- api.each_starred_gist { |g| @starred_gists[g["id"]] = g["description"]}
68
- write_json_file(my_gists, my_gists_file)
69
- write_json_file(starred_gists, starred_gists_file)
70
-
71
- @my_gists_index = build_index(my_gists)
72
- @starred_gists_index = build_index(starred_gists)
73
- write_json_file(my_gists_index, my_gists_index_file)
74
- write_json_file(starred_gists_index, starred_gists_index_file)
65
+ save_gists_data
66
+ save_gists_index
75
67
  end
76
68
 
77
69
  def search(keyword)
@@ -82,12 +74,30 @@ module IGist
82
74
  search_index(starred_gists_index, keyword).map { |id| {id: id, description: starred_gists[id]} }
83
75
  end
84
76
 
77
+ def single_gist(id)
78
+ api.single_gist(id)
79
+ end
80
+
85
81
  def clear
86
82
  File.delete(my_gists_file, my_gists_index_file, starred_gists_file, starred_gists_index_file)
87
83
  end
88
84
 
89
85
  private
90
86
 
87
+ def save_gists_data
88
+ api.each_my_gist { |g| @my_gists[g["id"]] = g["description"]}
89
+ api.each_starred_gist { |g| @starred_gists[g["id"]] = g["description"]}
90
+ write_json_file(my_gists, my_gists_file)
91
+ write_json_file(starred_gists, starred_gists_file)
92
+ end
93
+
94
+ def save_gists_index
95
+ @my_gists_index = build_index(my_gists)
96
+ @starred_gists_index = build_index(starred_gists)
97
+ write_json_file(my_gists_index, my_gists_index_file)
98
+ write_json_file(starred_gists_index, starred_gists_index_file)
99
+ end
100
+
91
101
  def read_json_file(file)
92
102
  if File.exists?(file)
93
103
  JSON.parse(File.read(file))
@@ -1,3 +1,3 @@
1
1
  module IGist
2
- VERSION = "0.0.1"
2
+ VERSION = "0.1.0"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: igist
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-09-15 00:00:00.000000000 Z
12
+ date: 2013-09-24 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
@@ -69,6 +69,7 @@ extensions: []
69
69
  extra_rdoc_files: []
70
70
  files:
71
71
  - .gitignore
72
+ - .travis.yml
72
73
  - Gemfile
73
74
  - LICENSE.txt
74
75
  - README.md
@@ -101,7 +102,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
101
102
  version: '0'
102
103
  segments:
103
104
  - 0
104
- hash: -1414565074206882299
105
+ hash: -134198980528100810
105
106
  required_rubygems_version: !ruby/object:Gem::Requirement
106
107
  none: false
107
108
  requirements:
@@ -110,7 +111,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
110
111
  version: '0'
111
112
  segments:
112
113
  - 0
113
- hash: -1414565074206882299
114
+ hash: -134198980528100810
114
115
  requirements: []
115
116
  rubyforge_project:
116
117
  rubygems_version: 1.8.25