-
Notifications
You must be signed in to change notification settings - Fork 0
/
atom.xml
280 lines (211 loc) · 18.8 KB
/
atom.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
<title><![CDATA[Indyarocks]]></title>
<link href="http://indyarocks.github.io/atom.xml" rel="self"/>
<link href="http://indyarocks.github.io/"/>
<updated>2013-10-13T21:25:28+05:30</updated>
<id>http://indyarocks.github.io/</id>
<author>
<name><![CDATA[Chandan Kumar]]></name>
</author>
<generator uri="http://octopress.org/">Octopress</generator>
<entry>
<title type="html"><![CDATA[Rack Basics]]></title>
<link href="http://indyarocks.github.io/blog/2013/10/13/rack-basics/"/>
<updated>2013-10-13T20:04:00+05:30</updated>
<id>http://indyarocks.github.io/blog/2013/10/13/rack-basics</id>
<content type="html"><![CDATA[<p>Today I explored Rack, an awesome API connecting web servers and web frameworks by Christian Neukirchen. Here is an <a href="http://chneukirchen.org/blog/archive/2007/02/introducing-rack.html" target="_blank">introduction article</a> by Christian Neukirchen.</p>
<p><a href="http://rack.github.io/" target="_blank">Rack</a> was insipired from python’s <a href="http://wsgi.readthedocs.org/en/latest/">WSGI</a>.</p>
<h3>What is Rack?</h3>
<p>To put in the words of Christian,</br></p>
<p style="font-style:italic;margin:1.5em;">Rack provides a minimal, modular and adaptable interface for developing web applications in Ruby. By wrapping HTTP requests and responses in the simplest way possible, it unifies and distills the API for web servers, web frameworks, and software in between (the so-called middleware) into a single method call.</p>
<p>Instead of going deeper in documentation, I’ll walk through what exactly Rack is, and how it fits in different web-frameworks in Ruby. I’ve put the references at the end of the post, which helped me understand Rack.</p>
<!-- more -->
<p>Rack is very simple yet very powerful tool. Rack has <a href="http://rack.rubyforge.org/doc/SPEC.html" target="_blank">specifications</a> on how Rack application and web server should communicate. As long as you follow them, you don’t need ‘rack’ gem to develop you framework.</p>
<p style="font-style:italic;margin:1.5em;">A Rack application is a Ruby object(not a class) that responds to <span style="text-decoration:underline">call</span>. It takes exactly one argument, the environment and return an Array of exactly three values: The status, the headers, and the body.</p>
<h3>TODO</h3>
<div><script src='https://gist.github.com/indyarocks/6963372.js'></script>
<noscript><pre><code># config.ru
require 'rack/lobster'
require 'logger'
infinity = Rack::Builder.new do
use Rack::CommonLogger
Logger.new('rack.log')
map '/' do
run Proc.new {|env| [200, {"Content-Type" => "text/html"}, ["hello"]]}
end
map '/secret' do
use Rack::Auth::Basic, "Restricted Area" do |user, password|
user == 'super' && password == 'secretsauce'
end
map '/' do
run Proc.new{|env| [200, {"Content-Type" => "text/html"}, ["Secret page"]]}
end
end
map '/version' do
map '/' do
run Proc.new{|env| [200, {"Content-Type" => "text/html"}, ["Infinity 0.1"]]}
end
map '/last' do
run Proc.new{|env| [200, {"Content-Type" => "text/html"}, ["Infinity Beta"]]}
end
end
end
class ToUpper
# Initialize another Rack app
def initialize(app)
@app = app
end
def call(env)
# First call @app
status, headers, body = @app.call(env)
# Upcase body
upcased_body = ""
body.each {|chunk| upcased_body << chunk.upcase }
# Pass new body
[status, headers, [upcased_body]]
end
end
use Rack::Reloader
use ToUpper
run infinity
#run Rack::Lobster.new
</code></pre></noscript></div>
<h3><span style="text-decoration:underline">References</span>:-</h3>
<div style="margin-left:50px;">
<ol>
<li><a href="https://github.com/rack/rack/wiki/Tutorials" target="_blank">Rack Tutorials</a></li>
<li><a href="http://rubylearning.com/blog/a-quick-introduction-to-rack/" target="_blank">A Quick Introduction to Rack by Satish Talim</a></li>
<li><a href="http://m.onkey.org/ruby-on-rack-1-hello-rack" target="_blank">Ruby on Rack #1 by Pratik Naik</a></li>
<li><a href="http://m.onkey.org/ruby-on-rack-2-the-builder" target="_blank">Ruby on Rack #2 by Pratik Naik</a></li>
<li><a href="http://net.tutsplus.com/tutorials/exploring-rack/" target="_blank">Exploring Rack</a></li>
<li><a href="https://devcenter.heroku.com/articles/static-sites-ruby" target="_blank">Creating Static Sites in Ruby with Rack</a></li>
<li><a href="http://chneukirchen.org/blog/archive/2007/02/introducing-rack.html" target="_blank">introduction article by Christian Neukirchen</a></li>
<li><a href="http://library.edgecase.com/rails-routing-and-rack-endpoints" target="_blank">RAILS 3 ROUTING AND RACK ENDPOINTS by Matt Yoho</a></li>
</ol>
</div>
<ul>
<li style="font-style:italic;">PS: The whole credit of this article goes to the author of <em>References</em> listed above. I have just jotted down, what I learnt today mostly as note for my own reference.</li>
</ul>
]]></content>
</entry>
<entry>
<title type="html"><![CDATA[ActiveRecord logger in rails console]]></title>
<link href="http://indyarocks.github.io/blog/2013/10/12/activerecord-logger-in-rails-console/"/>
<updated>2013-10-12T14:51:00+05:30</updated>
<id>http://indyarocks.github.io/blog/2013/10/12/activerecord-logger-in-rails-console</id>
<content type="html"><![CDATA[<p>This came to my surprise, as I was going through ‘The Rails 4 Way’ by Obie Fernandez, that we can have our own logger instances in Rails.</p>
<p>Watch, what ActiveRecord is doing with your query in console itself. :)</p>
<div><script src='https://gist.github.com/indyarocks/6947792.js'></script>
<noscript><pre><code>> rails c
Loading development environment (Rails 4.0.0)
2.0.0p247 :001 > ActiveRecord::Base.logger = Logger.new(STDOUT)
=> #<Logger:0x007f816e1c35a8 @progname=nil, @level=0, @default_formatter=#<Logger::Formatter:0x007f816e1c3580 @datetime_format=nil>, @formatter=nil, @logdev=#<Logger::LogDevice:0x007f816e1c3530 @shift_size=nil, @shift_age=nil, @filename=nil, @dev=#<IO:<STDOUT>>, @mutex=#<Logger::LogDevice::LogDeviceMutex:0x007f816e1c3508 @mon_owner=nil, @mon_count=0, @mon_mutex=#<Mutex:0x007f816e1c34b8>>>>
2.0.0p247 :002 > user = User.first
D, [2013-10-12T14:46:23.337060 #25919] DEBUG -- : User Load (0.1ms) SELECT "users".* FROM "users" ORDER BY "users"."id" ASC LIMIT 1
=> #<User id: 1, name: "Chandan Kumar", email: "[email protected]", created_at: "2013-09-16 20:01:15", updated_at: "2013-09-16 20:34:20", password_digest: "$2a$10$qoNP6rM.89XSOJEXIsXig.9LKM2jcvY0Y4bFhCECCJ4X...", remember_token: "66302290bbc685a193b5d2ab5c8d3fc67e56318e", admin: true>
2.0.0p247 :003 > user.microposts
D, [2013-10-12T14:46:28.886696 #25919] DEBUG -- : Micropost Load (36.8ms) SELECT "microposts".* FROM "microposts" WHERE "microposts"."user_id" = ? ORDER BY created_at DESC [["user_id", 1]]
=> #<ActiveRecord::Associations::CollectionProxy [#<Micropost id: 301, content: "Cool\r\n", user_id: 1, created_at: "2013-10-06 15:27:10", updated_at: "2013-10-06 15:27:10">, #<Micropost id: 295, content: "Eligendi ducimus aut incidunt vel vitae accusamus.", user_id: 1, created_at: "2013-09-16 20:01:23", updated_at: "2013-09-16 20:01:23">, #<Micropost id: 289, content: "Dolores blanditiis aut omnis corrupti et unde.", user_id: 1, created_at: "2013-09-16 20:01:23", updated_at: "2013-09-16 20:01:23">, #<Micropost id: 283, content: "Quia iste et consequatur et id illo voluptas.", user_id: 1, created_at: "2013-09-16 20:01:23", updated_at: "2013-09-16 20:01:23">, #<Micropost id: 277, content: "Excepturi voluptatem adipisci nesciunt praesentium ...", user_id: 1, created_at: "2013-09-16 20:01:23", updated_at: "2013-09-16 20:01:23">, #<Micropost id: 271, content: "Deserunt et labore explicabo est id voluptatibus vo...", user_id: 1, created_at: "2013-09-16 20:01:23", updated_at: "2013-09-16 20:01:23">, #<Micropost id: 265, content: "Aut perspiciatis sapiente aspernatur maiores.", user_id: 1, created_at: "2013-09-16 20:01:23", updated_at: "2013-09-16 20:01:23">, #<Micropost id: 259, content: "Labore illo accusantium atque asperiores voluptas.", user_id: 1, created_at: "2013-09-16 20:01:23", updated_at: "2013-09-16 20:01:23">, #<Micropost id: 253, content: "Laborum tempora eum ipsa dolores.", user_id: 1, created_at: "2013-09-16 20:01:23", updated_at: "2013-09-16 20:01:23">, #<Micropost id: 247, content: "Excepturi commodi quibusdam velit deserunt nulla om...", user_id: 1, created_at: "2013-09-16 20:01:23", updated_at: "2013-09-16 20:01:23">, ...]></code></pre></noscript></div>
<p>I came across blog of Jamis Buck, and he had given on more interesting piece of hack to see the log in console. In your config/environment.rb define following method:</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="k">def</span> <span class="nf">log_to</span><span class="p">(</span><span class="n">stream</span><span class="p">)</span>
</span><span class='line'> <span class="ss">ActiveRecord</span><span class="p">:</span><span class="ss">:Base</span><span class="o">.</span><span class="n">logger</span> <span class="o">=</span> <span class="no">Logger</span><span class="o">.</span><span class="n">new</span><span class="p">(</span><span class="n">stream</span><span class="p">)</span>
</span><span class='line'> <span class="ss">ActiveRecrod</span><span class="p">:</span><span class="ss">:Base</span><span class="o">.</span><span class="n">clear_active_connections!</span>
</span><span class='line'><span class="k">end</span>
</span></code></pre></td></tr></table></div></figure>
<p>Now you can buffer your logs in a local variable.</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="o">></span> <span class="n">log_to</span> <span class="no">STDOUT</span>
</span><span class='line'><span class="o">=></span> <span class="o">.</span><span class="n">.</span>
</span><span class='line'><span class="o">></span> <span class="no">Post</span><span class="o">.</span><span class="n">find</span><span class="p">(</span><span class="ss">:first</span><span class="p">)</span>
</span><span class='line'> <span class="no">Post</span> <span class="no">Load</span> <span class="p">(</span><span class="mi">0</span><span class="o">.</span><span class="mo">00013</span><span class="mi">8</span><span class="p">)</span> <span class="no">SELECT</span> <span class="o">*</span> <span class="no">FROM</span> <span class="n">posts</span> <span class="no">LIMIT</span> <span class="mi">1</span>
</span><span class='line'><span class="o">=></span> <span class="c1">#<Post:0x1234 ...></span>
</span><span class='line'><span class="o">></span> <span class="n">buffer</span> <span class="o">=</span> <span class="no">StringIO</span><span class="o">.</span><span class="n">new</span>
</span><span class='line'><span class="o">=></span> <span class="o">.</span><span class="n">.</span>
</span><span class='line'><span class="o">></span> <span class="n">log_to</span> <span class="n">buffer</span>
</span><span class='line'><span class="o">=></span> <span class="o">.</span><span class="n">.</span>
</span><span class='line'><span class="no">User</span><span class="o">.</span><span class="n">first</span><span class="p">(</span><span class="ss">:first</span><span class="p">)</span>
</span><span class='line'><span class="o">=></span> <span class="c1">#<User:0x1234...></span>
</span><span class='line'><span class="o">></span> <span class="nb">p</span> <span class="n">buffer</span><span class="o">.</span><span class="n">string</span>
</span><span class='line'><span class="o">=></span> <span class="s2">"</span><span class="se">\e</span><span class="s2">...."</span> <span class="c1"># Logs here</span>
</span></code></pre></td></tr></table></div></figure>
<p>This can be found useful at many place you want to check the behavior of certain queries in console.</p>
<p>Thanks Jamis/Obie :)</p>
]]></content>
</entry>
<entry>
<title type="html"><![CDATA[Using Ruby gsub with a hash]]></title>
<link href="http://indyarocks.github.io/blog/2013/10/10/using-ruby-gsub-with-a-hash/"/>
<updated>2013-10-10T22:47:00+05:30</updated>
<id>http://indyarocks.github.io/blog/2013/10/10/using-ruby-gsub-with-a-hash</id>
<content type="html"><![CDATA[<p><strong> Gsub with Hash </strong>
We all have used gsub for global substitution of a pattern in string. The cool thing I came across today is, you can use hash to define the replacement.</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="k">def</span> <span class="nf">cool_gsub_with_hash</span><span class="p">(</span><span class="n">str</span><span class="p">)</span>
</span><span class='line'> <span class="n">str</span><span class="o">.</span><span class="n">gsub</span><span class="p">(</span><span class="sr">/[aeiou]/</span><span class="p">,</span> <span class="s1">'a'</span> <span class="o">=></span> <span class="s1">'1'</span><span class="p">,</span> <span class="s1">'e'</span> <span class="o">=></span> <span class="s1">'2'</span><span class="p">,</span> <span class="s1">'i'</span> <span class="o">=></span> <span class="s1">'3'</span><span class="p">,</span> <span class="s1">'o'</span> <span class="o">=></span> <span class="s1">'4'</span><span class="p">,</span> <span class="s1">'u'</span> <span class="o">=></span> <span class="s1">'5'</span><span class="p">)</span>
</span><span class='line'><span class="k">end</span>
</span><span class='line'>
</span><span class='line'><span class="o">></span> <span class="n">cool_gsub_with_hash</span><span class="p">(</span><span class="s2">"We all have used gsub for global substitution of a pattern in string. "</span><span class="p">)</span>
</span><span class='line'>
</span><span class='line'> <span class="o">=></span> <span class="s2">"W2 1ll h1v2 5s2d gs5b f4r gl4b1l s5bst3t5t34n 4f 1 p1tt2rn 3n str3ng. "</span>
</span><span class='line'>
</span><span class='line'><span class="o">></span> <span class="n">cool_gsub_with_hash</span><span class="p">(</span><span class="s2">"hello"</span><span class="p">)</span>
</span><span class='line'>
</span><span class='line'> <span class="o">=></span> <span class="s2">"h2ll4"</span>
</span></code></pre></td></tr></table></div></figure>
<p>Here we are not restricted with single character replacements:</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="k">def</span> <span class="nf">gsub_word_with_hash</span><span class="p">(</span><span class="n">str</span><span class="p">)</span>
</span><span class='line'> <span class="n">str</span><span class="o">.</span><span class="n">gsub</span><span class="p">(</span><span class="sr">/H(ello)?i?/</span><span class="p">,</span> <span class="s1">'Hi'</span> <span class="o">=></span> <span class="s2">"Namaste"</span><span class="p">,</span> <span class="s2">"Hello"</span> <span class="o">=></span> <span class="s2">"Wannakam"</span><span class="p">)</span>
</span><span class='line'><span class="k">end</span>
</span><span class='line'>
</span><span class='line'><span class="o">></span> <span class="n">gsub_word_with_hash</span><span class="p">(</span><span class="s2">"Hello Chandan"</span><span class="p">)</span>
</span><span class='line'> <span class="o">=></span> <span class="s2">"Wannakam Chandan"</span>
</span><span class='line'><span class="o">></span> <span class="n">gsub_word_with_hash</span><span class="p">(</span><span class="s2">"Hi Chandan"</span><span class="p">)</span>
</span><span class='line'> <span class="o">=></span> <span class="s2">"Namaste Chandan"</span>
</span></code></pre></td></tr></table></div></figure>
<p>I found it quite interesting. Courtesy: Bozahidar</p>
]]></content>
</entry>
<entry>
<title type="html"><![CDATA[Forked octopress]]></title>
<link href="http://indyarocks.github.io/blog/2013/06/11/forked-octopress/"/>
<updated>2013-06-11T23:23:00+05:30</updated>
<id>http://indyarocks.github.io/blog/2013/06/11/forked-octopress</id>
<content type="html"><![CDATA[
]]></content>
</entry>
</feed>