Pieces of Code

Just missing parts of code for your application

POODR - Chapter 1

| Comments

Hi,

I’m reading this really nice book (Practical Object-Oriented Design in Ruby) and I will use this blog to post the summary and notes about the book.

I don’t intend in any way to substitute the original book. I’m keeping this for future references and also to help myself studying it.

I’ll use slideshare as a format for it.

Here’s the chapter 1. Have fun :) :

Rails Console Not Opening in MacOS

| Comments

This week, I had a problem when I reinstalled my Ruby version.

When I went to terminal and type rails console c, it was not working presenting this error message:

1
.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/irb/completion.rb:9:in `require': cannot load such file -- readline (LoadError)

After some googling and asking it about in StackOverflow, a user give a hint: “You didn’t compile Ruby with readline support.”

Then, I found this link that teachs 3 ways to do and this worked for me:

  • Go to terminal
  • brew install readline
  • Open the file ~/.rvm/user/db and add the line: ruby_configure_flags=--with-readline-dir=/usr/local/opt/readline
  • rvm reinstall 2.0.0-p247

Now, when I go to terminal and type rails console c, it’s working again :)

Hope it helps someone :)

Representable Gem to Parse Nested Results

| Comments

Recently, I had to integrate with a SOAP Webservice from Ruby.

Thanks to Savon gem this was pretty easy.

To invoke the webservice, I made a simple method:

(savon_request.rb) download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
def request(method, message)
  client = Savon::Client.new(basic_auth: auth, wsdl: wsdl)
  response = client.call(method, message: message)
end

private

def auth
  [options[:ws_user], options[:ws_password] ]
end

def wsdl
  options[:wsdl_endpoint]
end

...

Savon already gives us a hash response, making easier to deal it.

Usually soap responses are pretty nested, so I use representable with goal of only map it.

I added the representable gem and create a (in my scenario) a client response:

(client_representer.rb) download
1
2
3
4
5
6
7
8
9
10
module ClientRepresenter
  include Representable::JSON

  nested :client_get_response do
    nested :client_get_result do
      property :email
      property :name
    end
  end
end

To map for a real class that my application understands, i’ve created a Virtus Model and extend the representable.

Virtus model:

(client_virtus.rb) download
1
2
3
4
5
6
class Client
  include Virtus.model

  attribute :email, String
  attribute :name, String
end

And to map it:

(service_representable_sample.rb) download
1
2
3
4
5
6
response = soap.request(:client_get, message)

client = Client.new
client.extend(Representers::ClientRepresenter)
client.from_hash(response.body.try(:with_indifferent_access))

That’s is:

  • I made a request for a SOAP webservice using Savon.
  • Create a representable to parse the results.
  • Use a Virtus model that my application can handle.

Of course it be made better, so feel free to comment it :)

Using VCR With Timecop

| Comments

If you already used VCR to record requests to unit tests your application, maybe all your tests passed in the day you developed. Then you go to sleep, thinking in another task.

You wake up another day, run all the tests again and boom! All related to VCR are failing and you don’t know why.

I run into this problem when I was using VCR and Timecop gems.

I was using Timecop to freeze my requests and the other day, all my related tests was failing.

To fix this, I’ve looked and at this issue and saw it was solved by exposing the VCR::Cassette#originally_recorded_at.

So, to make my tests passing again:

(timecop_snippet.rb) download
1
2
3
4
5
6
7
before do
  Timecop.freeze(VCR.current_cassette.originally_recorded_at || Time.now)
end

after do
  Timecop.return
end

In my case, I had trouble because my requests had time dependencies, so, I needed to use the time when the original cassete was recorded. Then I started using Timecop and the tests started to fail the other day.

But quick fixed!

Feel free to give feedback for better solutions :)

Happy coding!

How I Built My Blog Using Jekyll?

| Comments

Hi!

Thanks for getting in here!

For the initial post, I’m gonna write all the things I’m using to build this blog. As many things I intend to write, nothing magical.

Just to start, in my about page I explain the reasons why I’m writing this blog.

Being a blog focused in software development, I thought it will be cool to use something new for me to learn and post about it. So, I decided to use Jekyll.

Googling a little more, I found this really cool platform built on top of Jekyll: Octopress, a blogging framework for hackers. So far, everything that was needed was simple just by following its documentation. As a developer, I’m feeling some freedom to customize and use it. Fast and clean!

To do my about page, I’ve followed these simple steps.

As a theme for my blog, I’m using this really nice one. If you to know more, see more themes here.

I bought this domain at Godaddy and configure it using nice post.

For analytics, I’ve created an account at Google Analytics, but to add it, i’m using Segment.IO. This way, I can add more analytics without increasing my page size.

All of this writing is being made in Sublime Text using the Markdown plugin.

As I said, I don’t want to show complete solutions, but if you use just one of this links, it’ll be worth of doing it :)

Here’s a print screen of my editor writing this post:

To summarize this post, I’ve talked about:

  • A little about me
  • Jekyll
  • Octopress
  • How to create a page in Jekyll
  • Themes for Octopress
  • Domain registration
  • Analytics
  • Text and editor and plugin to help you

If you have any doubts, feel free to contact in the comments below :)