<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
  <title>JetThoughts' articles</title>
  <id>http://blog.jetthoughts.com</id>
  <updated>2009-10-03T00:00:00Z</updated>
  <author>
    <name>Michael Nikitochkin</name>
  </author>
  <entry>
    <title>Change Factory Girl directory path for Rails 3 generator</title>
    <link href="http://blog.jetthoughts.com/2011/09/24/change-factory-girl-directory-path-for-rails-3-generator/" rel="alternate"/>
    <id>http://blog.jetthoughts.com/2011/09/24/change-factory-girl-directory-path-for-rails-3-generator/</id>
    <published>2011-09-24T00:00:00Z</published>
    <updated>2011-09-24T00:00:00Z</updated>
    <author>
      <name>Michael Nikitochkin</name>
    </author>
    <summary type="html">&lt;p&gt;After setup rails 3.1 + Rspec + FactoryGirl, all factories are generated to &lt;strong&gt;test/factories&lt;/strong&gt; instead &lt;strong&gt;spec/factories&lt;/strong&gt;.
Solution is to add to your &lt;strong&gt;application.rb&lt;/strong&gt;:&lt;/p&gt;

</summary>
    <content type="html">&lt;p&gt;After setup rails 3.1 + Rspec + FactoryGirl, all factories are generated to &lt;strong&gt;test/factories&lt;/strong&gt; instead &lt;strong&gt;spec/factories&lt;/strong&gt;.
Solution is to add to your &lt;strong&gt;application.rb&lt;/strong&gt;:&lt;/p&gt;




&lt;pre&gt;&lt;code&gt;g.fixture_replacement :factory_girl, :dir =&amp;gt; 'spec/factories'
&lt;/code&gt;&lt;/pre&gt;




&lt;p&gt;So we have:&lt;/p&gt;




&lt;pre&gt;&lt;code&gt;:::ruby
config.generators do |g|
  g.template_engine :haml
  g.test_framework :rspec, :fixture_replacement =&amp;gt; :factory_girl, :views =&amp;gt; false, :helper =&amp;gt; false
  g.view_specs false
  g.helper_specs false
  g.fixture_replacement :factory_girl, :dir =&amp;gt; 'spec/factories'
end
&lt;/code&gt;&lt;/pre&gt;

</content>
  </entry>
  <entry>
    <title>Add current date to text document</title>
    <link href="http://blog.jetthoughts.com/2011/04/04/add-current-date-to-text-document/" rel="alternate"/>
    <id>http://blog.jetthoughts.com/2011/04/04/add-current-date-to-text-document/</id>
    <published>2011-04-04T00:00:00Z</published>
    <updated>2011-04-04T00:00:00Z</updated>
    <author>
      <name>Michael Nikitochkin</name>
    </author>
    <summary type="html">&lt;p&gt;Often we need to add current date to text document.  For Mac users we have awesome tools as Automator and  AppleScript.
We need to do five simple steps:&lt;/p&gt;

</summary>
    <content type="html">&lt;p&gt;Often we need to add current date to text document.  For Mac users we have awesome tools as Automator and  AppleScript.
We need to do five simple steps:&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;1)&lt;/strong&gt; Create a new workflow with template &amp;ldquo;Service&amp;rdquo; in Automator&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;2)&lt;/strong&gt; Choose &amp;lsquo;Service receives&amp;rsquo; to &amp;ldquo;no input&amp;rdquo; in &amp;ldquo;any application&amp;rdquo;&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;3)&lt;/strong&gt; Add action &amp;ldquo;Run AppleScript&amp;rdquo; with content:&lt;/p&gt;




&lt;pre&gt;&lt;code&gt;:::shell
on run {input, parameters}
  tell application "System Events"
    keystroke (short date string of (current date))
  end tell
  return input
end run
&lt;/code&gt;&lt;/pre&gt;




&lt;p&gt;&lt;strong&gt;4)&lt;/strong&gt; Save the service&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;5)&lt;/strong&gt; Go To &amp;ldquo;System Preferences &amp;ndash;&gt; Keyboard &amp;ndash;&gt; Keyboard Shortcuts &amp;ndash;&gt; Services&amp;rdquo; and setup a shortcut for the service and enable it.&lt;/p&gt;

</content>
  </entry>
  <entry>
    <title>Allow multiple access control requests for Rails</title>
    <link href="http://blog.jetthoughts.com/2010/12/22/allow-multiple-access-control-requests-for-rails/" rel="alternate"/>
    <id>http://blog.jetthoughts.com/2010/12/22/allow-multiple-access-control-requests-for-rails/</id>
    <published>2010-12-22T00:00:00Z</published>
    <updated>2010-12-22T00:00:00Z</updated>
    <author>
      <name>Michael Nikitochkin</name>
    </author>
    <summary type="html">&lt;p&gt;If you have problem with sending XHR requests from different domains, you find trouble to get content, because&lt;/p&gt;




&lt;pre&gt;&lt;code&gt;XMLHttpRequest cannot load http://different.domain.local:3000/visits. Origin http://localhost:3000 is not allowed by Access-Control-Allow-Origin&amp;amp;hellip;
&lt;/code&gt;&lt;/pre&gt;

</summary>
    <content type="html">&lt;p&gt;If you have problem with sending XHR requests from different domains, you find trouble to get content, because&lt;/p&gt;




&lt;pre&gt;&lt;code&gt;XMLHttpRequest cannot load http://different.domain.local:3000/visits. Origin http://localhost:3000 is not allowed by Access-Control-Allow-Origin.
&lt;/code&gt;&lt;/pre&gt;




&lt;p&gt;The solution is to setup response headers: &lt;em&gt;Access-Control-Request-Method&lt;/em&gt;, &lt;em&gt;Access-Control-Allow-Origin&lt;/em&gt;.&lt;/p&gt;




&lt;pre&gt;&lt;code&gt;:::ruby
headers['Access-Control-Allow-Origin'] = '*'
headers['Access-Control-Request-Method'] = '*'
&lt;/code&gt;&lt;/pre&gt;




&lt;p&gt;Example for rails:&lt;/p&gt;




&lt;pre&gt;&lt;code&gt;:::ruby
#app/application_controller.rb
after_filter :set_access_control_headers

def set_access_control_headers
  headers['Access-Control-Allow-Origin'] = '*'
  headers['Access-Control-Request-Method'] = '*'
end
&lt;/code&gt;&lt;/pre&gt;

</content>
  </entry>
  <entry>
    <title>Redis Store to store session</title>
    <link href="http://blog.jetthoughts.com/2010/09/28/redis-store-to-store-session/" rel="alternate"/>
    <id>http://blog.jetthoughts.com/2010/09/28/redis-store-to-store-session/</id>
    <published>2010-09-28T00:00:00Z</published>
    <updated>2010-09-28T00:00:00Z</updated>
    <author>
      <name>Michael Nikitochkin</name>
    </author>
    <summary type="html">&lt;p&gt;I use Redis to store session and cache. And When I try cache some session values, I get exception &amp;ldquo;TypeError (can&amp;rsquo;t dump TCPSocket)&amp;rdquo;. I have researched this trouble. It was because session.slice(&amp;lsquo;keys&amp;rsquo;) return not simple Hash instance, but SessionHash. So instance method &lt;em&gt;to_hash&lt;/em&gt; fix all troubles&amp;hellip;&lt;/p&gt;

</summary>
    <content type="html">&lt;p&gt;I use Redis to store session and cache. And When I try cache some session values, I get exception &amp;ldquo;TypeError (can&amp;rsquo;t dump TCPSocket)&amp;rdquo;. I have researched this trouble. It was because session.slice(&amp;lsquo;keys&amp;rsquo;) return not simple Hash instance, but SessionHash. So instance method &lt;em&gt;to_hash&lt;/em&gt; fix all troubles.
Example:&lt;/p&gt;




&lt;pre&gt;&lt;code&gt;:::ruby
Rails.cache.write("key", session.except("flash", :session_id, :_csrf_token))
&lt;/code&gt;&lt;/pre&gt;




&lt;p&gt;Solution:&lt;/p&gt;




&lt;pre&gt;&lt;code&gt;:::ruby
Rails.cache.write("key", session.except("flash", :session_id, :_csrf_token).to_hash)
&lt;/code&gt;&lt;/pre&gt;

</content>
  </entry>
  <entry>
    <title>Show not valid CSV lines with sed</title>
    <link href="http://blog.jetthoughts.com/2010/09/21/show-not-valid-csv-lines-with-sed/" rel="alternate"/>
    <id>http://blog.jetthoughts.com/2010/09/21/show-not-valid-csv-lines-with-sed/</id>
    <published>2010-09-21T00:00:00Z</published>
    <updated>2010-09-21T00:00:00Z</updated>
    <author>
      <name>Michael Nikitochkin</name>
    </author>
    <summary type="html">&lt;p&gt;I have trouble with invalid formatted CSV file. First step show lines with invalid lines.&lt;/p&gt;




&lt;pre&gt;&lt;code&gt;:::sh
sed -n '/"[^",]*"[^",]*"[^",]*",/,1p' &amp;lt;fileName&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

</summary>
    <content type="html">&lt;p&gt;I have trouble with invalid formatted CSV file. First step show lines with invalid lines.&lt;/p&gt;




&lt;pre&gt;&lt;code&gt;:::sh
sed -n '/"[^",]*"[^",]*"[^",]*",/,1p' &amp;lt;fileName&amp;gt;
&lt;/code&gt;&lt;/pre&gt;




&lt;p&gt;Then find in the google way to replace symbol inside quotes. And read next manual http://sed.sourceforge.net/sed1line.txt. So create a sed script with next content, call it as &lt;strong&gt;script.sed&lt;/strong&gt;:&lt;/p&gt;




&lt;pre&gt;&lt;code&gt;:::sh
s/\",\"/\$XXXX\$/g;
:a
s/\([^,]\)"\([^,]\)/\1'\2/g
ta
s/\$XXXX\$/\",\"/g;
&lt;/code&gt;&lt;/pre&gt;




&lt;p&gt;Next we just do:&lt;/p&gt;




&lt;pre&gt;&lt;code&gt;:::sh
sed -f script.sed &amp;lt;fileName&amp;gt;
&lt;/code&gt;&lt;/pre&gt;




&lt;p&gt;And we get in output a normal csv format file. Next we just add the argument to apply this in this file.&lt;/p&gt;




&lt;pre&gt;&lt;code&gt;:::sh
sed -i .bak -f script.sed &amp;lt;fileName&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

</content>
  </entry>
  <entry>
    <title>Different timestamp for Rails and Postgres</title>
    <link href="http://blog.jetthoughts.com/2010/09/16/different-timestamp-for-rails-and-postgres/" rel="alternate"/>
    <id>http://blog.jetthoughts.com/2010/09/16/different-timestamp-for-rails-and-postgres/</id>
    <published>2010-09-16T00:00:00Z</published>
    <updated>2010-09-16T00:00:00Z</updated>
    <author>
      <name>Michael Nikitochkin</name>
    </author>
    <summary type="html">&lt;p&gt;Today I found a discrepancy in Rails timestamp and Postgres timestamp for my specs:&lt;/p&gt;




&lt;pre&gt;&lt;code&gt;:::ruby
puts Time.now.to_i
puts ActiveRecord::Base.connection.execute("SELECT LOCALTIMESTAMP as c1")[0]["c1"].to_time(:local).to_i
&lt;/code&gt;&lt;/pre&gt;

</summary>
    <content type="html">&lt;p&gt;Today I found a discrepancy in Rails timestamp and Postgres timestamp for my specs:&lt;/p&gt;




&lt;pre&gt;&lt;code&gt;:::ruby
puts Time.now.to_i
puts ActiveRecord::Base.connection.execute("SELECT LOCALTIMESTAMP as c1")[0]["c1"].to_time(:local).to_i
&lt;/code&gt;&lt;/pre&gt;




&lt;p&gt;But for console timestamps are the same. Why it is not working for specs, I dont know.&lt;/p&gt;

</content>
  </entry>
  <entry>
    <title>Remove first lines from stream</title>
    <link href="http://blog.jetthoughts.com/2010/09/04/remove-first-lines-from-stream/" rel="alternate"/>
    <id>http://blog.jetthoughts.com/2010/09/04/remove-first-lines-from-stream/</id>
    <published>2010-09-04T00:00:00Z</published>
    <updated>2010-09-04T00:00:00Z</updated>
    <author>
      <name>Michael Nikitochkin</name>
    </author>
    <summary type="html">&lt;p&gt;I am not a linux hacker, so I wasted a lot of time to find solution to strip first lines from output stream. First my solution was:&lt;/p&gt;




&lt;pre&gt;&lt;code&gt;:::sh
tail -f some_file | ruby -e 'a =0; while t=gets; a+=1; puts t if a &amp;gt; 1; end'
&lt;/code&gt;&lt;/pre&gt;

</summary>
    <content type="html">&lt;p&gt;I am not a linux hacker, so I wasted a lot of time to find solution to strip first lines from output stream. First my solution was:&lt;/p&gt;




&lt;pre&gt;&lt;code&gt;:::sh
tail -f some_file | ruby -e 'a =0; while t=gets; a+=1; puts t if a &amp;gt; 1; end'
&lt;/code&gt;&lt;/pre&gt;




&lt;p&gt;It is looks very long, and I thought that this problem is very popular, and at least one tool already exist in the world.&lt;/p&gt;




&lt;p&gt;I knew a tool &lt;strong&gt;sed&lt;/strong&gt; and used it before. So read the manual and wuala:&lt;/p&gt;




&lt;pre&gt;&lt;code&gt;:::sh
tail -f some_file | sed "1d"
&lt;/code&gt;&lt;/pre&gt;




&lt;p&gt;Remove first 10 lines:&lt;/p&gt;




&lt;pre&gt;&lt;code&gt;:::sh
tail -f some_file | sed '1,10d'
&lt;/code&gt;&lt;/pre&gt;




&lt;p&gt;It do the same thing as first solution, but more clear and simple. &lt;strong&gt;sed&lt;/strong&gt; is a great tool.&lt;/p&gt;

</content>
  </entry>
  <entry>
    <title>Enable line numbers in Toto syntaxhighlight</title>
    <link href="http://blog.jetthoughts.com/2010/09/04/enable-line-numbers-in-toto-syntaxhighlight/" rel="alternate"/>
    <id>http://blog.jetthoughts.com/2010/09/04/enable-line-numbers-in-toto-syntaxhighlight/</id>
    <published>2010-09-04T00:00:00Z</published>
    <updated>2010-09-04T00:00:00Z</updated>
    <author>
      <name>Michael Nikitochkin</name>
    </author>
    <summary type="html">&lt;p&gt;Syntax Highlighting by &lt;a href="http://github.com/wbzyl/rack-codehighlighter"&gt;Rack::CodeHighlighter&lt;/a&gt; gem has a bug. May be I did wrong steps. But by default &lt;strong&gt;CodeRay&lt;/strong&gt; disable to show line numbers for code. I did not found any options to enable it by &lt;strong&gt;CodeHighlighter&lt;/strong&gt;. So input next lines in &lt;strong&gt;config.ru&lt;/strong&gt; will show line numbers&amp;hellip;&lt;/p&gt;

</summary>
    <content type="html">&lt;p&gt;Syntax Highlighting by &lt;a href="http://github.com/wbzyl/rack-codehighlighter"&gt;Rack::CodeHighlighter&lt;/a&gt; gem has a bug. May be I did wrong steps. But by default &lt;strong&gt;CodeRay&lt;/strong&gt; disable to show line numbers for code. I did not found any options to enable it by &lt;strong&gt;CodeHighlighter&lt;/strong&gt;. So input next lines in &lt;strong&gt;config.ru&lt;/strong&gt; will show line numbers.&lt;/p&gt;




&lt;pre&gt;&lt;code&gt;:::ruby
CodeRay::Encoders["html"]::DEFAULT_OPTIONS[:line_numbers]=:inline
&lt;/code&gt;&lt;/pre&gt;




&lt;p&gt;  But I have a problem with &lt;strong&gt;css&lt;/strong&gt;. I found that &lt;strong&gt;CodeHighlighter&lt;/strong&gt; generate not correct html for &lt;strong&gt;CodeRay&lt;/strong&gt; and default theme&amp;rsquo;s css for Toto broke this highlighting too. You could use my fixed &lt;a href="/css/coderay.css"&gt;coderay.css&lt;/a&gt; for Toto.&lt;/p&gt;

</content>
  </entry>
  <entry>
    <title>Geoip_city on Mac OS</title>
    <link href="http://blog.jetthoughts.com/2010/07/11/geoipcity-on-mac-os/" rel="alternate"/>
    <id>http://blog.jetthoughts.com/2010/07/11/geoipcity-on-mac-os/</id>
    <published>2010-07-11T00:00:00Z</published>
    <updated>2010-07-11T00:00:00Z</updated>
    <author>
      <name>Michael Nikitochkin</name>
    </author>
    <summary type="html">&lt;p&gt;Today, I spent a time to install gem &lt;em&gt;geoip_city&lt;/em&gt;. So what did I do:&lt;/p&gt;




&lt;ul&gt;
&lt;li&gt;Get latest version of GeoIP C Api from &lt;a href="http://geolite.maxmind.com/download/geoip/api/c/"&gt;http://geolite.maxmind.com/download/geoip/api/c/&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</summary>
    <content type="html">&lt;p&gt;Today, I spent a time to install gem &lt;em&gt;geoip_city&lt;/em&gt;. So what did I do:&lt;/p&gt;




&lt;ul&gt;
&lt;li&gt;Get latest version of GeoIP C Api from &lt;a href="http://geolite.maxmind.com/download/geoip/api/c/"&gt;http://geolite.maxmind.com/download/geoip/api/c/&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;




&lt;pre&gt;
    wget http://geolite.maxmind.com/download/geoip/api/c/GeoIP-1.4.6.tar.gz
    tar zxf GeoIP-1.4.6.tar.gz
    cd GeoIP-1.4.6
&lt;/pre&gt;




&lt;ul&gt;
&lt;li&gt;Read file &lt;em&gt;README.OSX&lt;/em&gt;. Found simple instructions to compile this lib.&lt;/li&gt;
&lt;/ul&gt;




&lt;pre&gt;
    export GEOIP_ARCH='-arch i386 -arch x86_64 -arch ppc -arch ppc64'
    export MACOSX_DEPLOYMENT_TARGET=10.4
    export LDFLAGS=$GEOIP_ARCH
    export CFLAGS="-mmacosx-version-min=10.4 -isysroot /Developer/SDKs/MacOSX10.4u.sdk $GEOIP_ARCH"
    ./configure --disable-dependency-tracking
    perl -i.bak -pe'/^archive_cmds=/ and !/\bGEOIP_ARCH\b/ and s/-dynamiclib\b/-dynamiclib \\\$(GEOIP_ARCH)/' ./libtool
    make
&lt;/pre&gt;




&lt;ul&gt;
&lt;li&gt;&lt;p&gt;When run all this stuff, I did not get a success result, I still have a error when install gem.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;show available SDKs in you host
&lt;pre&gt;ls /Developer/SDKs &lt;/pre&gt;
I got two: MacOSX10.5.sdk     MacOSX10.6.sdk&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;So change line in readme file
before&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;




&lt;pre&gt;export CFLAGS="-mmacosx-version-min=10.4 -isysroot /Developer/SDKs/MacOSX10.4u.sdk $GEOIP_ARCH"&lt;/pre&gt;




&lt;p&gt;to&lt;/p&gt;




&lt;pre&gt;export CFLAGS="-mmacosx-version-min=10.4 -isysroot /Developer/SDKs/MacOSX10.6.sdk $GEOIP_ARCH"&lt;/pre&gt;




&lt;ul&gt;
&lt;li&gt;&lt;p&gt;and execute all steps from README file.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;but found a problem in step configure:&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;




&lt;pre&gt;
% ./configure --disable-dependency-tracking
checking for gcc... gcc
checking for C compiler default output file name...
configure: error: C compiler cannot create executables
See `config.log' for more details.
&lt;/pre&gt;




&lt;ul&gt;
&lt;li&gt;&lt;p&gt;I choose another SDK and set to &amp;ldquo;MacOSX10.5.sdk&amp;rdquo;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;run again all steps&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;then install gem and all works fine&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;if you have troubles to install gem and still see next message:&lt;/p&gt;




&lt;pre&gt;
checking for GeoIP_record_by_ipnum() in -lGeoIP... no
you must have geoip c library installed!
*** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of
necessary libraries and/or headers.  Check the mkmf.log file for more
details.  You may need configuration options.

Provided configuration options:
     --with-opt-dir
     --without-opt-dir
     --with-opt-include
     --without-opt-include=${opt-dir}/include
     --with-opt-lib
     --without-opt-lib=${opt-dir}/lib
     --with-make-prog
     --without-make-prog
     --srcdir=.
     --curdir
     --ruby=/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/bin/ruby
     --with-geoip-dir
     --without-geoip-dir
     --with-geoip-include
     --without-geoip-include=${geoip-dir}/include
     --with-geoip-lib
     --without-geoip-lib=${geoip-dir}/lib
     --with-GeoIPlib
     --without-GeoIPlib
&lt;/pre&gt;




&lt;p&gt;So you should add include /usr/local/lib to DYNLD_LIBRARY_PATH. Or do next:
in step of configuration do  ./configure &amp;mdash;disable-dependency-tracking  &amp;mdash;prefix=/opt/GeoIP
and then next steps from README. I suggest do &lt;em&gt;make clean&lt;/em&gt; before each compiles.
And then &lt;em&gt;sudo gem install geoip_city &amp;mdash; &amp;mdash;with-geoip-dir=/opt/GeoIP&lt;/em&gt; to install gem.&lt;/p&gt;

</content>
  </entry>
  <entry>
    <title>Install Nokogiri on Mac OS</title>
    <link href="http://blog.jetthoughts.com/2010/07/01/install-nokogiri-on-mac-os/" rel="alternate"/>
    <id>http://blog.jetthoughts.com/2010/07/01/install-nokogiri-on-mac-os/</id>
    <published>2010-07-01T00:00:00Z</published>
    <updated>2010-07-01T00:00:00Z</updated>
    <author>
      <name>Michael Nikitochkin</name>
    </author>
    <summary type="html">&lt;p&gt;Run in the terminal:&lt;/p&gt;




&lt;pre&gt;&lt;code&gt;:::sh
sudo gem install nokogiri -- --with-xml2-include=/usr/include/libxml2 --with-xml2-lib=/usr/lib --with-xslt-dir=/usr
&lt;/code&gt;&lt;/pre&gt;

</summary>
    <content type="html">&lt;p&gt;Run in the terminal:&lt;/p&gt;




&lt;pre&gt;&lt;code&gt;:::sh
sudo gem install nokogiri -- --with-xml2-include=/usr/include/libxml2 --with-xml2-lib=/usr/lib --with-xslt-dir=/usr
&lt;/code&gt;&lt;/pre&gt;

</content>
  </entry>
</feed>

