<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>NetManiac</title>
	<atom:link href="https://nhw.pl/feed" rel="self" type="application/rss+xml" />
	<link>https://nhw.pl/wp</link>
	<description>Witold Rugowski on web20 wave with Ruby on Rails</description>
	<lastBuildDate>Thu, 11 Jul 2024 09:25:32 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	
	<item>
		<title>Psych::SyntaxError: did not find expected key while parsing a block mapping at</title>
		<link>https://nhw.pl/wp/2024/07/11/psychsyntaxerror-did-not-find-expected-key-while-parsing-a-block-mapping-at</link>
					<comments>https://nhw.pl/wp/2024/07/11/psychsyntaxerror-did-not-find-expected-key-while-parsing-a-block-mapping-at#respond</comments>
		
		<dc:creator><![CDATA[Witold Rugowski]]></dc:creator>
		<pubDate>Thu, 11 Jul 2024 09:25:32 +0000</pubDate>
				<category><![CDATA[RubyOnRails]]></category>
		<category><![CDATA[ruby on rails]]></category>
		<guid isPermaLink="false">https://nhw.pl/wp/?p=623</guid>

					<description><![CDATA[This post is kind of note to self. This error will show in Rails when you have syntax error within your YAML translation files. But. Message shows line and column number but not exactly where error is but the parent entry location, so error is hiding somewhere else. In my case that was string closed [&#8230;]]]></description>
										<content:encoded><![CDATA[
<p class="wp-block-paragraph"><br>This post is kind of <em>note to self</em>. This error will show in Rails when you have syntax error within your YAML translation files. But. Message shows line and column number but not exactly where error is but the parent entry location, so error is hiding somewhere else.<br><br>In my case that was string closed into quotes and dot after quote and it was located several lines beneath line shown in error message.</p>



<pre class="wp-block-code"><code>lev1:
  lev2:
   s1: Text
   s2: Text
   s3: "Text %{title}".</code></pre>



<p class="wp-block-paragraph">Error message would show <code>lev2</code> as place of problem&#8230;</p>
]]></content:encoded>
					
					<wfw:commentRss>https://nhw.pl/wp/2024/07/11/psychsyntaxerror-did-not-find-expected-key-while-parsing-a-block-mapping-at/feed</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Rails 5.2, Ruby 2.5.9, serialization and unknown keywords: permitted_classes, aliases</title>
		<link>https://nhw.pl/wp/2023/08/18/rails-5-2-ruby-2-5-9-serialization-and-unknown-keywords-permitted_classes-aliases</link>
					<comments>https://nhw.pl/wp/2023/08/18/rails-5-2-ruby-2-5-9-serialization-and-unknown-keywords-permitted_classes-aliases#respond</comments>
		
		<dc:creator><![CDATA[Witold Rugowski]]></dc:creator>
		<pubDate>Fri, 18 Aug 2023 14:38:50 +0000</pubDate>
				<category><![CDATA[RubyOnRails]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[Ruby]]></category>
		<guid isPermaLink="false">https://nhw.pl/wp/?p=599</guid>

					<description><![CDATA[I wrote here few times here about quirks working with older Rails application. For example, upgrading old app to some more up-to-date toolset can be challenging. In my opinion it is best done step by step, so having 4.0 Rails app it is much more challenging to upgrade right to 6.1 will be very hard. [&#8230;]]]></description>
										<content:encoded><![CDATA[
<p class="wp-block-paragraph">I wrote here few times here about quirks working with older Rails application. For example, upgrading old app to some more up-to-date toolset can be challenging. In my opinion it is best done step by step, so having 4.0 Rails app it is much more challenging to upgrade right to 6.1 will be very hard. So I suggest going step by step. Read release notes for Rails versions and do try to guess what steps are safe for Your application.</p>



<p class="wp-block-paragraph">Recently I did  4.0 -&gt; 4.2 -&gt; 5.0 -&gt; 5.2 and probably will be 6.1 next in order, but&#8230; We are now on 5.2. With Ruby upgraded to 2.5.9 (BTW good <a href="https://www.fastruby.io/blog/ruby/rails/versions/compatibility-table.html">table with info which Ruby and Rails are compatible</a>) and Rails in 5.2 I have experienced errors <code>unknown keywords: permitted_classes, aliases</code>. It is related to serialization (some data was stored in ActiveRecord that way using Psych gem. But You can not find Psych on gems list. It is bundled with Ruby and used if not provided with alternative.</p>



<p class="wp-block-paragraph">So, just add Psych to Your gem file with correct version. Bundled version with 2.5.9 is Psych 3.0.6, using Psych 5.x or 4.x resulted in other problems/errors so I have finally settled with <code>gem 'psych', '~> 3.3'</code>which ended with 3.3.4 installed. Now everything works, I can move with updating app further.</p>



<p class="wp-block-paragraph">In case you are storing hash with symbols as key, then doing deserialization you may encounter <code>Tried to load unspecified class: Symbol</code>. In that case add to initializers: <code>config.active_record.yaml_column_permitted_classes = [Symbol]</code></p>
]]></content:encoded>
					
					<wfw:commentRss>https://nhw.pl/wp/2023/08/18/rails-5-2-ruby-2-5-9-serialization-and-unknown-keywords-permitted_classes-aliases/feed</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Working with old Rails 2.4 on Ubuntu 20.04</title>
		<link>https://nhw.pl/wp/2021/10/29/working-with-old-rails-2-4-on-ubuntu-20-04</link>
					<comments>https://nhw.pl/wp/2021/10/29/working-with-old-rails-2-4-on-ubuntu-20-04#respond</comments>
		
		<dc:creator><![CDATA[Witold Rugowski]]></dc:creator>
		<pubDate>Fri, 29 Oct 2021 13:00:49 +0000</pubDate>
				<category><![CDATA[RubyOnRails]]></category>
		<guid isPermaLink="false">https://nhw.pl/wp/?p=589</guid>

					<description><![CDATA[Installing old Rails 2.4 (well, somebody need to maintain that legacy code&#8230;) on Ubuntu with Ruby 2.4.10 You can get failure during Nokogirii installation. Log ends with: compiling xslt_stylesheet.c xslt_stylesheet.c:112:13: warning: ‘swallow_superfluous_xml_errors’ defined but not used [-Wunused-function] 112 &#124; static void swallow_superfluous_xml_errors(void * userdata, xmlErrorPtr error, …) &#124; ^~~~~~~~~~ cc1: warning: unrecognized command line option [&#8230;]]]></description>
										<content:encoded><![CDATA[
<p class="wp-block-paragraph">Installing old Rails 2.4 (well, somebody need to maintain that legacy code&#8230;) on Ubuntu with Ruby 2.4.10 You can get failure during Nokogirii installation. Log ends with:</p>



<pre class="wp-block-preformatted">compiling xslt_stylesheet.c
 xslt_stylesheet.c:112:13: warning: ‘swallow_superfluous_xml_errors’ defined but not used [-Wunused-function]
   112 | static void swallow_superfluous_xml_errors(void * userdata, xmlErrorPtr error, …)
       |             ^<del>~~~~~</del>~~~~
 cc1: warning: unrecognized command line option ‘-Wno-self-assign’
 cc1: warning: unrecognized command line option ‘-Wno-constant-logical-operand’
 cc1: warning: unrecognized command line option ‘-Wno-parentheses-equality’
 linking shared-object nokogiri/nokogiri.so
 Cleaning files only used during build.
 rm -rf /home/viciu/.rvm/gems/ruby-2.4.10@rails42/gems/nokogiri-1.10.10/ext/nokogiri/tmp/x86_64-pc-linux-gnu/ports
 rm -rf /home/viciu/.rvm/gems/ruby-2.4.10@rails42/gems/nokogiri-1.10.10/ports
 current directory: /home/viciu/.rvm/gems/ruby-2.4.10@rails42/gems/nokogiri-1.10.10/ext/nokogiri
 make "DESTDIR=" install
 make: /usr/bin/mkdir: Command not found
 make: *** [Makefile:200: .sitearchdir.-.nokogiri.time] Error 127
 make install failed, exit code 2</pre>



<p class="wp-block-paragraph">Simplest fix is to link <code>mkdir</code>  in place where make expects to find it (on Ubuntu make is in <code>/bin</code> not <code>/usr/bin</code>):</p>



<p class="wp-block-paragraph"><code>sudo ln -s /bin/mkdir /usr/bin/mkdir</code></p>



<p class="wp-block-paragraph">After that You will could install gem and <code>bundle install</code> will work.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://nhw.pl/wp/2021/10/29/working-with-old-rails-2-4-on-ubuntu-20-04/feed</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Rails &#8211; skip validation in inherited classes.</title>
		<link>https://nhw.pl/wp/2020/09/19/rails-skip-validation-in-inherited-classes</link>
					<comments>https://nhw.pl/wp/2020/09/19/rails-skip-validation-in-inherited-classes#respond</comments>
		
		<dc:creator><![CDATA[Witold Rugowski]]></dc:creator>
		<pubDate>Sat, 19 Sep 2020 10:16:28 +0000</pubDate>
				<category><![CDATA[RubyOnRails]]></category>
		<category><![CDATA[rails]]></category>
		<guid isPermaLink="false">https://nhw.pl/wp/?p=584</guid>

					<description><![CDATA[Doing refactor in one of my applications I had to change behavior of single model. Since that was not total rewrite but just workflow change and I wanted to do A/B tests I needed both versions working. Instead of writing completely new model I opted for using class inherited from old version. New class should [&#8230;]]]></description>
										<content:encoded><![CDATA[
<p class="wp-block-paragraph">Doing refactor in one of my applications I had to change behavior of single model. Since that was not total rewrite but just workflow change and I wanted to do A/B tests I needed both versions working. </p>



<p class="wp-block-paragraph">Instead of writing completely new model I opted for using class inherited from old version. New class should have some additional behavior compared to previous version, so that approach made sense for me.</p>



<p class="wp-block-paragraph">I ran on problem with validations. On old model validation was done in single step &#8211; object was in state &#8216;not ready&#8217; and until it was completed part of validations was not run. When it was in &#8216;ready state&#8217; then validations were enabled. Problem was that in new behavior, object building should be done in two steps. Normally that is not problem, since part of validations can be run from one stage  and part can be run when other condition is present.</p>



<p class="wp-block-paragraph">Since I have chosen inheritance, old, one-step validation was present in child model. I had to remove this one-step validation and add  new. So there is how it can be done (Rails 4.2):</p>



<p class="wp-block-paragraph">First, You need to know what kind of validator it is. In my case it was attribute presence validator for <code>:address</code>. Such validator is <code>ActiveRecord::Validations::PresenceValidator</code>. In case You don&#8217;t know exactly where to find it, You can always check all validators in given object. Just create object instance in console, and pretty print <code>object._validate_callbacks</code>. Then find &#8220;Your&#8221; validator and it&#8217;s attributes. Having that info (class and attributes) add to Your model:</p>



<pre class="wp-block-code"><code>old_validator = _validate_callbacks.find do |c|
    c.filter.is_a?(ActiveRecord::Validations::PresenceValidator) &amp;&amp;
        c.filter.instance_variable_get(:@attributes) == [:address]
  end.filter
  skip_callback :validate, old_validator</code></pre>



<p class="wp-block-paragraph">Replace validator class and attribute name in check to matching Your case.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://nhw.pl/wp/2020/09/19/rails-skip-validation-in-inherited-classes/feed</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Sentry Ruby client and silence&#8230;</title>
		<link>https://nhw.pl/wp/2019/02/12/sentry-ruby-client-and-silence</link>
					<comments>https://nhw.pl/wp/2019/02/12/sentry-ruby-client-and-silence#respond</comments>
		
		<dc:creator><![CDATA[Witold Rugowski]]></dc:creator>
		<pubDate>Tue, 12 Feb 2019 21:51:07 +0000</pubDate>
				<category><![CDATA[Ruby]]></category>
		<category><![CDATA[ruby on rails]]></category>
		<guid isPermaLink="false">http://nhw.pl/wp/?p=573</guid>

					<description><![CDATA[Sentry is great tool for monitoring Rails applications, we use it on Nettigo. But… when Raven, Sentry Ruby client starts it writes something like Raven ready to catch errors. It is a bit annoying. Every log file is full of this crap. There is no documentation of it, but if You look inside code, You [&#8230;]]]></description>
										<content:encoded><![CDATA[
<p class="wp-block-paragraph">Sentry is great tool for monitoring Rails applications, we use it on Nettigo. But… when <a href="https://github.com/getsentry/raven-ruby">Raven,</a> Sentry Ruby client starts it writes something like <em>Raven ready to catch errors</em>. It is a bit annoying. Every log file is full of this crap. There is no documentation of it, but if You look inside code, You will find <code>report_status</code> function, responsible for this mess. First line is:</p>



<p class="wp-block-paragraph"><code>return if configuration.silence_ready</code></p>



<p class="wp-block-paragraph">Hallelujah! Just add to Your <code>Raven.configure do |config|</code> block this line:</p>



<pre class="wp-block-code"><code>  config.silence_ready = true</code></pre>



<p class="wp-block-paragraph">To silence that message.</p>



<p class="wp-block-paragraph">BTW &#8211; long time no word from me. Maybe, just maybe it will be more posts in future. </p>
]]></content:encoded>
					
					<wfw:commentRss>https://nhw.pl/wp/2019/02/12/sentry-ruby-client-and-silence/feed</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Shell rules</title>
		<link>https://nhw.pl/wp/2016/08/30/shell-rules</link>
					<comments>https://nhw.pl/wp/2016/08/30/shell-rules#respond</comments>
		
		<dc:creator><![CDATA[Witold Rugowski]]></dc:creator>
		<pubDate>Tue, 30 Aug 2016 10:58:23 +0000</pubDate>
				<category><![CDATA[Other]]></category>
		<category><![CDATA[shell]]></category>
		<guid isPermaLink="false">http://nhw.pl/wp/?p=564</guid>

					<description><![CDATA[Recently in Nettigo I&#8217;m busy with learning Chef. Oh, we are small company (in terms of number of servers), so, when finally I had some practice witch Chef I stated I don&#8217;t need it :) At least, as full-blown tool, with knife, Chef server (both self hosted and as service). What I find useful is [&#8230;]]]></description>
										<content:encoded><![CDATA[<p>Recently in Nettigo I&#8217;m busy with learning Chef. Oh, we are small company (in terms of number of servers), so, when finally I had some practice witch Chef I stated I don&#8217;t need it :) At least, as full-blown tool, with knife, Chef server (both self hosted and as service). </p>
<p>What I find useful is Chef in local mode, Capistrano and Kitchen. At least this is what I found interesting for me. My goal is not to get fully automated server provisioning, deployment and configuration. I need reliable way to do deployments in repeatable way. I&#8217;m moving my application to new server and planning for some microservices inside. This is meant for me to be more reliable deployment way than old &#8211; write down all steps when I did created production environment. </p>
<p>Ok, this only a background for this one liner in shell:</p>
<p><code>tm=30;a=`du -sk|cut -f 1`;sleep $tm;b=`du -sk|cut -f 1`;let c=\(b-a\)/tm;echo $c</code></p>
<p>I&#8217;m deploying my web application once to VirtualBox and sometimes to real server. When creating my Rails application, I need to restore all downaloadable content like images, datasheets, etc. If I do deploy to VirutalBox instance fastest way is to copy it from backup on my laptop. When I do deploy to host in Internet to use data from backup stored on other server. To speed up downloading I have tested different network options (doesn&#8217;t matter what one). I needed way to compare how fast are data downloaded. Data downloaded are different files copied from different locations in background. So I did simple &#8216;oneliner&#8217; shown above to calculate how much data was downloaded and saved to disk in given time and calculate speed in kB/s. Looks ugly, but anybody who remember Perl can not say that ;)</p>
]]></content:encoded>
					
					<wfw:commentRss>https://nhw.pl/wp/2016/08/30/shell-rules/feed</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>When will_paginate renders strange URLs after Rails upgrade to 3.2</title>
		<link>https://nhw.pl/wp/2014/04/24/when-will_paginate-renders-strange-urls-after-rails-upgrade-to-3-2</link>
					<comments>https://nhw.pl/wp/2014/04/24/when-will_paginate-renders-strange-urls-after-rails-upgrade-to-3-2#respond</comments>
		
		<dc:creator><![CDATA[Witold Rugowski]]></dc:creator>
		<pubDate>Thu, 24 Apr 2014 08:58:34 +0000</pubDate>
				<category><![CDATA[RubyOnRails]]></category>
		<guid isPermaLink="false">http://nhw.pl/wp/?p=550</guid>

					<description><![CDATA[To be honest I didn&#8217;t expect I will write again here at this blog. But good news I did manage to find some time to write! Since 2011 I&#8217;m not freelancing anymore, but all the time I use Rails in Nettigo. Codebase used there has it&#8217;s roots in Rails 1.2 or even earlier release &#8211; [&#8230;]]]></description>
										<content:encoded><![CDATA[<p>To be honest I didn&#8217;t expect I will write again here at this blog. But good news I did manage to find some time to write! </p>
<p>Since 2011 I&#8217;m not freelancing anymore, but all the time I use Rails in <a href="http://nettigo.eu">Nettigo</a>. Codebase used there has it&#8217;s roots in Rails 1.2 or even earlier release &#8211; first migrations have not timestamps at beginning of filename but numbers, this change was introduced at Rails 2.1. In other words there was a few upgrades in live of this application, and there will be some in future ;) Some upgrades were easy but sometimes You can encounter problems which looks strange and after few hours debugging You have still no idea what is going on :)</p>
<p>Right now I&#8217;m in upgrade process from Rails 3.1 to 3.2. It went quite smooth (or at least I think so, since I have not deployed to production environment :) it yet) with one exception &#8211; <code>will_paginate</code> have started generating wrong URLs. For example on main page instead of just adding <code>page</code> parameter it have changed path to <code>/about?page=2&controller=Public/products</code>. It looked strange since root was defined, links with <code>link_to</code> were generated for it, page was displayed, but when <code>will_paginate</code> called <code>url_for</code>, it returned garbage &#8211; redirecting to <em>About</em> page instead of keeping original URL.</p>
<p>To keep long story short (as usual best solution is to leave code, get some sleep and see at problem again with fresh mind), this diff illustrates what was a cause:</p>
<p><script src="https://gist.github.com/netmaniac/f3f99a38f60378297849.js"></script></p>
<p>Root was served from controller in namespace (module Public), and with change in routing engine (Rails 3.2 introduced new one) capital letter did the difference.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://nhw.pl/wp/2014/04/24/when-will_paginate-renders-strange-urls-after-rails-upgrade-to-3-2/feed</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Passing current user id to Rails models</title>
		<link>https://nhw.pl/wp/2011/11/30/passing-current-user-id-to-rails-models</link>
					<comments>https://nhw.pl/wp/2011/11/30/passing-current-user-id-to-rails-models#comments</comments>
		
		<dc:creator><![CDATA[Witold Rugowski]]></dc:creator>
		<pubDate>Tue, 29 Nov 2011 23:31:02 +0000</pubDate>
				<category><![CDATA[RubyOnRails]]></category>
		<category><![CDATA[mvc violation]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[real world]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[ruby on rails]]></category>
		<category><![CDATA[tips]]></category>
		<guid isPermaLink="false">http://nhw.pl/wp/?p=533</guid>

					<description><![CDATA[Long time since last post, isn&#8217;t it? Well, I&#8217;m completely busy with my work in Nettigo, selling Arduinos and other nice gears :) I do not do gigs anymore, so there is a lot less reasons to write about Rails. But I still use this framework, since my backend software is written in Rails, so [&#8230;]]]></description>
										<content:encoded><![CDATA[<p>Long time since last post, isn&#8217;t it? Well, I&#8217;m completely busy with my work in Nettigo, selling Arduinos and other nice gears :) I do not do gigs anymore, so there is a lot less reasons to write about Rails.</p>
<p>But I still use this framework, since my backend software is written in Rails, so here is one thing which I think can be useful for You.</p>
<p>Often we want to pass ID of current user to Rails model internals. Most answers are <em>don&#8217;t do that</em>, this is controller work to do authorization stuff, MVC is to prevent such thins, etc. </p>
<p><small>here was not related picture (as usual on this blog). Some readers were complaining it was not only unrelated but even offending. Well, maybe they were right, so, I have removed that picture.</small></p>
<p>Yeah, right, but what if we want to keep some kind of audit trail what is happening with given model? This is not authorization, just who done what.</p>
<p>Since in my application case audit records are created in observers simple passing user id as some additional parameter is no go for me &#8211; I don&#8217;t want to change interface just to pass user info, that breaks too much things.</p>
<p>So, I have chosen this approach:</p>
<pre class='ruby' name='code'>
def with_user user, &block
  if user.blank?
    yield
  else
    Thread.current[:user] = user.id
    yield
    Thread.current[:user] = nil
  end
end
</pre>
<p>And each operation I want to track I invoke:</p>
<pre class='ruby' name='code'>
with_user(current_user) do
 model.do_some_stuff
end
</pre>
<p><code>Thread.current</code> acts as a hash accessible in current thread and allow us to pass some info skipping MVC isolation (You wanted that, right? :) )</p>
<p>In observers I can access user ID and store it with record. If it is not present, audit trails is being marked as created by <em>System</em>. That means probably cron job, or console action (well on console I can run code inside <code>with_user</code> block when needed).</p>
<p>This is safe as long each request is being processed in single thread and we make sure that user ID will be cleared after our operation. When ID will stay in Thread.current, next request being processed by this Rails app instance will have access to that value and wrong audit records will be created.</p>
<p>Code has to be updated, since any exception will occur inside block, code after <code>yield</code> will be skipped and user ID won&#8217;t be erased from <code>Thread.current</code>. Here it is, final version:</p>
<pre class='ruby' name='code'>
def with_user user, &block
  if user.blank?
    yield
  else
    Thread.current[:user] = user.id

    begin
      yield
    ensure
      Thread.current[:user] = nil
    end
  end
end
</pre>
<p>Code inside <code>ensure</code> will be executed each time &#8211; both when exception was raised and when execution was clean.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://nhw.pl/wp/2011/11/30/passing-current-user-id-to-rails-models/feed</wfw:commentRss>
			<slash:comments>8</slash:comments>
		
		
			</item>
		<item>
		<title>wsdl2ruby and failing SSL certificate validation</title>
		<link>https://nhw.pl/wp/2011/01/21/wsdl2ruby-and-failing-ssl-certificate-validation</link>
					<comments>https://nhw.pl/wp/2011/01/21/wsdl2ruby-and-failing-ssl-certificate-validation#comments</comments>
		
		<dc:creator><![CDATA[Witold Rugowski]]></dc:creator>
		<pubDate>Fri, 21 Jan 2011 00:39:29 +0000</pubDate>
				<category><![CDATA[Uncategorized]]></category>
		<guid isPermaLink="false">http://nhw.pl/wp/?p=521</guid>

					<description><![CDATA[When You are about to use Ruby to connect to some SOAP-like API You will probably use SOAP4r gem. First step would be to generate client code from WSDL provided by API. If it is served via HTTPS and server has SSL certificate not signed by some common CA (like self signed certs) You will [&#8230;]]]></description>
										<content:encoded><![CDATA[<p>When You are about to use Ruby to connect to some SOAP-like API You will probably use SOAP4r gem. First step would be to generate client code from WSDL provided by API. If it is served via HTTPS and server has SSL certificate not signed by some common CA (like self signed certs) You will experience following error  running <code>wsdl2ruby.rb</code>:</p>
<pre>
at depth 0 - 20: unable to get local issuer certificate
F, [2011-01-18T10:13:40.816069 #4035] 
FATAL -- app: Detected an exception. Stopping ... 
SSL_connect returned=1 errno=0 state=SSLv3 read server certificate B: 
certificate verify failed (OpenSSL::SSL::SSLError)
</pre>
<p>HTTPClient (this is gem used by SOAP4r to do all HTTP communication) tries to validate SSL certificates by default. Good, this is what SSL was thought for :) HTTPClient allows to use own CA (which is better solution than to lower security, especially on production :) ), however I don&#8217;t know how to pass this via SOPA4r abstraction (<code>ssl-config.set-trust-ca('path')</code> in HTTPClient (<strong>UPDATE</strong> &#8211; OK I think here You can find info <a href="http://www.fngtps.com/2007/03/openssl-ssl-sslerror-with-soap4r-and-the-rubyforge-gem">how to specify CA, client key and cert in SOAP client</a>). We can tell HTTPClient not to check SSL certs via options of SOAP::RPC::Driver when using client code:</p>
<pre class='ruby' name='code'>
d = SomeSOAP::ClientClass.new
d.options['protocol.http.ssl_config.verify_mode'] = OpenSSL::SSL::VERIFY_NONE
</pre>
<p>This may be not very wise, so use that if You know what You are doing.</p>
<h2>And what about wsdl2ruby.rb</h2>
<p>Above example relates to scenario when You have generated client code, which should connect to HTTPS. But how to recover from <code>certificate verify failed</code> when running wsdl2ruby.rb (in other words &#8211; when generating client code and WSDL is on HTTPS server with self signed cert)?</p>
<p>First solution is to download WSDL and generate client code from local file. Will do the trick unless WSDL imports some additional XSDs via HTTPS URL&#8230; </p>
<p><a href="https://gist.github.com/736721">This gist</a> gave me idea how to solve that (BTW &#8211; very useful trick to debug HTTP traffic if You can not use proxy like <a href="http://www.charlesproxy.com/">Charles Proxy</a>). I did checked out SOAP4r gem code and updated <code>wsdl2ruby.rb</code> adding at begin:</p>
<pre class='ruby' name='code'>
BEGIN {
  require 'rubygems'
  require 'httpclient'
  HTTPClient.class_eval do
    alias_method '__initialize__', 'initialize'
    	    
    def initialize(*args,&block)
      __initialize__(*args, &block)
    ensure
      self.ssl_config.verify_mode=OpenSSL::SSL::VERIFY_NONE
    end
  end
}
</pre>
<p>That&#8217;s it. Well, almost. I was unlucky &#8211; due to some configuration twists WSDL and other referenced resources were hosted in some dev environment with wrong network configuration. Fixing that was totally out of my reach. </p>
<p>What was wrong? WSDL was accessible via HTTPS. All resources imported inside were referenced via HTTPS, too. But. When accessing these HTTPS URLs (other than WSDL) from outside network (where I was) plain HTTP error message was displayed, so SOAP code could not be generated. When URL was changed to HTTP &#8211; proper XML file was available. </p>
<p>So, another hack to <code>wsdl2ruby.rb</code>:</p>
<pre class='ruby' name='code'>
class  HTTPClient
    alias_method '__get_content__', 'get_content'
    def get_content(uri, query = nil, extheader = {}, &block)
     uri = URI.parse(uri.to_s.gsub(/^https:/,'http:'))
     if block_given?
       __get_content__(uri, query, extheader) {block}
     else
       __get_content__(uri, query, extheader)
     end
    end
end
</pre>
<p>Now all requests to HTTPS are converted to plain HTTP. Again &#8211; do it when You understand what You are doing :) </p>
<h2>Where Ruby brought me</h2>
<p>When I started this blog (Apr 2006) I&#8217;ve just have discovered Ruby on Rails. During this almost five years many things have changed. And finally Ruby brought me to this place:</p>
<p><figure id="attachment_524" aria-describedby="caption-attachment-524" style="width: 500px" class="wp-caption aligncenter"><img fetchpriority="high" decoding="async" src="http://nhw.pl/wp/wp-content/uploads/massimo_and_arduino_logo.jpg" alt="Massimo and Arduino Logo at Maker Faire 2010 in NYC" title="Massimo showing Arduino Logo" width="500" height="333" class="size-full wp-image-524" srcset="https://nhw.pl/wp/wp-content/uploads/massimo_and_arduino_logo.jpg 500w, https://nhw.pl/wp/wp-content/uploads/massimo_and_arduino_logo-300x199.jpg 300w" sizes="(max-width: 500px) 100vw, 500px" /><figcaption id="caption-attachment-524" class="wp-caption-text">Massimo and Arduino Logo at Maker Faire 2010 in NYC<br />CC by http://www.flickr.com/photos/mattrichardson/</figcaption></figure></p>
<p>And now small announcement. As You can see I was publishing on this blog very seldom in recent months. And reason is very simple. Almost two years ago I have started side business (do freelancer have side businesses? :)) ) <a href="http://nettigo.pl/">selling Arduinos in Poland (and whole Europe).</a> After slow start, it have took off, and between my work as freelancer and running shop there were no time to write on this blog.</p>
<p>Recently I have made decision to stop freelancing and devote all my time to Arduino (and electronics) related topics. Will I stop writing about Ruby? I guess not. Shop is on some hosted platform, but I do run some custom RoR software as backend. I plan to migrate at some point to own e-commerce solution, so I&#8217;m not leaving Ruby world.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://nhw.pl/wp/2011/01/21/wsdl2ruby-and-failing-ssl-certificate-validation/feed</wfw:commentRss>
			<slash:comments>1</slash:comments>
		
		
			</item>
		<item>
		<title>ActiveMerchant: dumping traffic between Your app and payment gateway</title>
		<link>https://nhw.pl/wp/2010/10/04/activemerchant-dumping-traffic-between-your-app-and-payment-gateway</link>
					<comments>https://nhw.pl/wp/2010/10/04/activemerchant-dumping-traffic-between-your-app-and-payment-gateway#comments</comments>
		
		<dc:creator><![CDATA[Witold Rugowski]]></dc:creator>
		<pubDate>Mon, 04 Oct 2010 15:58:09 +0000</pubDate>
				<category><![CDATA[RubyOnRails]]></category>
		<category><![CDATA[activemerchant]]></category>
		<guid isPermaLink="false">http://nhw.pl/wp/?p=515</guid>

					<description><![CDATA[Just a quick note &#8211; if You need to dump all traffic between payment gateway and Your application using ActiveMerchant just add to Your development.rb: ActiveMerchant::Billing::QuantumGateway.wiredump_device = File.open("/tmp/q.log", "a+") ActiveMerchant::Billing::QuantumGateway.wiredump_device.sync = true Just remember &#8211; it will work ONLY You have one application instance, since it uses simple file handle to write all traffic. And [&#8230;]]]></description>
										<content:encoded><![CDATA[<p>Just a quick note &#8211; if You need to dump all traffic between payment gateway and Your application using ActiveMerchant just add to Your development.rb:</p>
<pre name='code' class='Ruby'>
ActiveMerchant::Billing::QuantumGateway.wiredump_device = File.open("/tmp/q.log", "a+")
ActiveMerchant::Billing::QuantumGateway.wiredump_device.sync = true
</pre>
<p>Just remember &#8211; it will work <b>ONLY</b> You have one application instance, since it uses simple file handle to write all traffic. And replace QunatumGateway with gateway used by You.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://nhw.pl/wp/2010/10/04/activemerchant-dumping-traffic-between-your-app-and-payment-gateway/feed</wfw:commentRss>
			<slash:comments>2</slash:comments>
		
		
			</item>
	</channel>
</rss>
