cheatr 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,13 @@
1
+ # Cheatr
2
+
3
+ ## TEAM
4
+
5
+ Ernesto García (@gnapse) -- Creator
6
+
7
+ ## THANKS
8
+
9
+ Chris Wanstrath (@defunkt) for the original idea (https://github.com/defunkt/cheat)
10
+
11
+ ## TECHNOLOGY
12
+
13
+ Ruby, Sinatra, Git, Markdown
@@ -0,0 +1,3 @@
1
+ # robotstxt.org/
2
+
3
+ User-agent: *
@@ -0,0 +1,139 @@
1
+ require 'git'
2
+ require 'active_model'
3
+ require 'active_support/core_ext'
4
+ require 'cheatr/error'
5
+
6
+ module Cheatr::Server
7
+ class Sheet
8
+ include ActiveModel::Model
9
+
10
+ attr_accessor :name
11
+ validates_presence_of :name, :contents
12
+ validates_format_of :name, with: Cheatr::SHEET_NAME_REGEXP
13
+
14
+ def initialize(name)
15
+ super(name: name)
16
+ end
17
+
18
+ def human_name
19
+ @human_name ||= name.gsub('.', ' / ')
20
+ end
21
+
22
+ def contents
23
+ @contents ||= object.try(:contents)
24
+ end
25
+
26
+ def contents=(new_contents)
27
+ @contents = filter_contents(new_contents)
28
+ end
29
+
30
+ def save
31
+ return false unless valid?
32
+
33
+ git do
34
+ File.open(file_name, 'w') { |f| f.write(contents) }
35
+ git.add(file_name)
36
+ if git.status[file_name].type
37
+ git.commit "#{new? ? 'Create' : 'Update'} #{name}"
38
+ self.reload
39
+ end
40
+ end
41
+
42
+ return true
43
+ rescue => e
44
+ errors.add(:contents, "couldn't be saved (#{e.message})")
45
+ return false
46
+ end
47
+
48
+ def reload
49
+ # Forces reloading when getters are invoked
50
+ @object = @contents = nil
51
+ end
52
+
53
+ def file_name
54
+ @file_name ||= "#{name}.md"
55
+ end
56
+
57
+ def object
58
+ @object ||= git.object("master:#{file_name}")
59
+ rescue
60
+ nil
61
+ end
62
+
63
+ def new?
64
+ object.nil?
65
+ end
66
+
67
+ # Querying for sheets
68
+
69
+ def self.all(query = nil)
70
+ query = "*" if query.nil?
71
+ git.ls_files("#{query}.md").keys.map { |f| Sheet.new f.gsub(/\.md\z/, '') }
72
+ end
73
+
74
+ def self.find(name)
75
+ sheet = Sheet.new(name)
76
+ sheet.new? ? nil : sheet
77
+ end
78
+
79
+ def self.find!(name)
80
+ find(name) or raise Sinatra::NotFound
81
+ end
82
+
83
+ # Git connection
84
+
85
+ def self.repository=(path)
86
+ @@repository = path
87
+ git # check if the path is a valid repository
88
+ path
89
+ end
90
+
91
+ def self.repository
92
+ @@repository ||= Dir.pwd
93
+ end
94
+
95
+ def self.git
96
+ if block_given?
97
+ git.reset_hard
98
+ git.checkout('master')
99
+ git.chdir { yield }
100
+ return
101
+ end
102
+ @@git ||= Git.open(repository) rescue init_repository
103
+ rescue Errno::EEXIST
104
+ raise Cheatr::Error, "Folder #{repository} is not a valid cheatr repository."
105
+ end
106
+
107
+ def git(&block)
108
+ self.class.git(&block)
109
+ end
110
+
111
+ private
112
+
113
+ #
114
+ # Initializes a new cheatr repository at the specified location.
115
+ #
116
+ # The given folder must not exist.
117
+ #
118
+ def self.init_repository
119
+ Dir.mkdir(repository)
120
+ g = Git.init(repository)
121
+ g.chdir do
122
+ File.open('config.ru', 'w') do |f|
123
+ f.write("require 'cheatr'\nrun Cheatr::Server::App\n")
124
+ end
125
+ g.add('config.ru')
126
+ g.commit('Initial commit')
127
+ end
128
+ g
129
+ end
130
+
131
+ #
132
+ # Standardizes the contents, specially with regards to newline characters.
133
+ #
134
+ def filter_contents(contents)
135
+ contents.strip.gsub("\r\n", "\n").gsub("\r", "\n") + "\n"
136
+ end
137
+
138
+ end
139
+ end
@@ -0,0 +1,5 @@
1
+ # <%= @title %>
2
+
3
+ <% @sheets.each do |sheet| %>
4
+ * {{<%= sheet.name %>}}
5
+ <% end %>
@@ -0,0 +1,24 @@
1
+ <!DOCTYPE html>
2
+ <!--[if lt IE 7]> <html class="no-js lt-ie9 lt-ie8 lt-ie7"> <![endif]-->
3
+ <!--[if IE 7]> <html class="no-js lt-ie9 lt-ie8"> <![endif]-->
4
+ <!--[if IE 8]> <html class="no-js lt-ie9"> <![endif]-->
5
+ <!--[if gt IE 8]><!--> <html class="no-js"> <!--<![endif]-->
6
+ <head>
7
+ <meta charset="utf-8">
8
+ <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
9
+ <title><%= @title %> - Cheatr</title>
10
+ <meta name="description" content="Cheatr cheat sheets, cause cheating is fun!">
11
+ <meta name="viewport" content="width=device-width">
12
+ <link rel="stylesheet" href="css/normalize.css">
13
+ <link rel="stylesheet" href="css/main.css">
14
+ </head>
15
+ <body>
16
+ <!--[if lt IE 7]>
17
+ <p class="chromeframe">You are using an <strong>outdated</strong> browser. Please <a href="http://browsehappy.com/">upgrade your browser</a> or <a href="http://www.google.com/chromeframe/?redirect=true">activate Google Chrome Frame</a> to improve your experience.</p>
18
+ <![endif]-->
19
+ <div id="container">
20
+ <%= yield %>
21
+ </div>
22
+ </body>
23
+ </html>
24
+
@@ -0,0 +1 @@
1
+ <%= @sheet.contents %>
@@ -0,0 +1,3 @@
1
+ module Cheatr
2
+ VERSION = "0.0.1"
3
+ end
metadata ADDED
@@ -0,0 +1,239 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cheatr
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Ernesto García
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-07-22 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: slop
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: git
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: sinatra
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :runtime
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: activemodel
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :runtime
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ - !ruby/object:Gem::Dependency
79
+ name: redcarpet
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ type: :runtime
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ - !ruby/object:Gem::Dependency
95
+ name: rest-client
96
+ requirement: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ! '>='
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ type: :runtime
103
+ prerelease: false
104
+ version_requirements: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ! '>='
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ - !ruby/object:Gem::Dependency
111
+ name: pager
112
+ requirement: !ruby/object:Gem::Requirement
113
+ none: false
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
+ none: false
122
+ requirements:
123
+ - - ! '>='
124
+ - !ruby/object:Gem::Version
125
+ version: '0'
126
+ - !ruby/object:Gem::Dependency
127
+ name: bundler
128
+ requirement: !ruby/object:Gem::Requirement
129
+ none: false
130
+ requirements:
131
+ - - ~>
132
+ - !ruby/object:Gem::Version
133
+ version: '1.3'
134
+ type: :development
135
+ prerelease: false
136
+ version_requirements: !ruby/object:Gem::Requirement
137
+ none: false
138
+ requirements:
139
+ - - ~>
140
+ - !ruby/object:Gem::Version
141
+ version: '1.3'
142
+ - !ruby/object:Gem::Dependency
143
+ name: rake
144
+ requirement: !ruby/object:Gem::Requirement
145
+ none: false
146
+ requirements:
147
+ - - ! '>='
148
+ - !ruby/object:Gem::Version
149
+ version: '0'
150
+ type: :development
151
+ prerelease: false
152
+ version_requirements: !ruby/object:Gem::Requirement
153
+ none: false
154
+ requirements:
155
+ - - ! '>='
156
+ - !ruby/object:Gem::Version
157
+ version: '0'
158
+ - !ruby/object:Gem::Dependency
159
+ name: rspec
160
+ requirement: !ruby/object:Gem::Requirement
161
+ none: false
162
+ requirements:
163
+ - - ! '>='
164
+ - !ruby/object:Gem::Version
165
+ version: '0'
166
+ type: :development
167
+ prerelease: false
168
+ version_requirements: !ruby/object:Gem::Requirement
169
+ none: false
170
+ requirements:
171
+ - - ! '>='
172
+ - !ruby/object:Gem::Version
173
+ version: '0'
174
+ description: Display cheat sheets for a variety of programs, tools and libraries,
175
+ right on the command line.
176
+ email:
177
+ - gnapse@gmail.com
178
+ executables:
179
+ - cheatr
180
+ extensions: []
181
+ extra_rdoc_files: []
182
+ files:
183
+ - .gitignore
184
+ - Gemfile
185
+ - LICENSE.txt
186
+ - README.md
187
+ - Rakefile
188
+ - bin/cheatr
189
+ - cheatr.gemspec
190
+ - lib/cheatr.rb
191
+ - lib/cheatr/client.rb
192
+ - lib/cheatr/client/sheet.rb
193
+ - lib/cheatr/error.rb
194
+ - lib/cheatr/server.rb
195
+ - lib/cheatr/server/app.rb
196
+ - lib/cheatr/server/helpers.rb
197
+ - lib/cheatr/server/public/404.html
198
+ - lib/cheatr/server/public/css/main.css
199
+ - lib/cheatr/server/public/css/normalize.css
200
+ - lib/cheatr/server/public/favicon.ico
201
+ - lib/cheatr/server/public/humans.txt
202
+ - lib/cheatr/server/public/robots.txt
203
+ - lib/cheatr/server/sheet.rb
204
+ - lib/cheatr/server/views/index.md.erb
205
+ - lib/cheatr/server/views/layout.html.erb
206
+ - lib/cheatr/server/views/sheet.md.erb
207
+ - lib/cheatr/version.rb
208
+ homepage: ''
209
+ licenses:
210
+ - MIT
211
+ post_install_message:
212
+ rdoc_options: []
213
+ require_paths:
214
+ - lib
215
+ required_ruby_version: !ruby/object:Gem::Requirement
216
+ none: false
217
+ requirements:
218
+ - - ! '>='
219
+ - !ruby/object:Gem::Version
220
+ version: '0'
221
+ segments:
222
+ - 0
223
+ hash: 1230626457616116074
224
+ required_rubygems_version: !ruby/object:Gem::Requirement
225
+ none: false
226
+ requirements:
227
+ - - ! '>='
228
+ - !ruby/object:Gem::Version
229
+ version: '0'
230
+ segments:
231
+ - 0
232
+ hash: 1230626457616116074
233
+ requirements: []
234
+ rubyforge_project:
235
+ rubygems_version: 1.8.23
236
+ signing_key:
237
+ specification_version: 3
238
+ summary: Display cheat sheets right on the command line.
239
+ test_files: []