Arquivo

Arquivo da Categoria ‘API’

Generate PDF with prawn and Google charts

29, dezembro, 2011 Sem comentários

Hey guys,

It’s a quickly post about how to generate pdf with prawn and google charts. Prawn is a awesome lib in ruby for generating PDF documents and google charts is a API to generate charts.

I like to create a class responsible to generate my pdf documents. Here is my implementation:

# encoding: UTF-8
require 'open-uri'
class GenderReportPdf < Prawn::Document

  def initialize(mens, womans)
    super(top_margin: 70)
    add_title
    @mens = mens
    @womans = womans
    add_count
    add_image
  end

  def add_title
    text "Report by Gender", size: 28, style: :bold, position: :center
  end

  def add_count
    move_down 20
    text "Mens #{@h_count}"
    text "Womans #{@m_count}"
  end

  def add_image
    move_down 20
    image get_external_image, :position => :center
  end

  def get_external_image
    img = URI.parse(URI.encode("https://chart.googleapis.com/chart?cht=p3&chd=t:#{@mens},#{@womans}&chs=250x100&chl=Mens|Womans")).to_s
    img = open(img)
  end

end

It’s a simple ruby code, very easy to understand (I guess). In the next post I will show you how to send this report to client using Rails.

Categories: API, Gems, Ruby Tags: , , ,

OmniAuth strategy for authenticating to Podio

22, dezembro, 2011 Sem comentários

Hello fellows,

Now that I finished my bachelor’s degree, I have free time to devote to open source projects that I like and a project that I really like is OmniAuth.

OmniAuth is a libary that standardizes multi-provider authentication for web applications. It’s very flexible and nice!

In my current job, we are using a lot a web application called Podio. It’s a very cool app.
So yesterday I started to develop a OmniAuth strategy for authenticating to Podio and today I finished it.

You can see the code here

Fell free to contribute and use it.

Thanks,

Categories: API, Gems, Rails, Ruby Tags:

Short tips to create a great API

21, outubro, 2011 1 comentário

The last projects that I have worked had a API for external client access, I learned a lot with those projects and here I gonna share with you what I learned.

  • HTTP You must to understand how HTTP protocol works. This is the first and more important thing.
  • Use HTTP verbs – Your api must be able to understand what we expect that it does if for example a request with put verb is received. It’s terrifying to use just get and post to everything or to use get to modify data.
  • Everything needs a ID - Every resource needs to have a identification. That way you can provide a way for your users can access that resource.
  • Hypermedia Support – Link resources together. This is hard to explain, lets see a example:
<order self='http://example.com/customers/4332' >

<amount>53</amount>

<product ref='http://example.com/products/45654' />

<customer ref='http://example.com/customers/2332' />

</order>

In this example, we have a resource representation named ‘order’. That resource has a attribute named amount and a link to two another resource. That way, my client doesn’t need to know how to access resources connected with that resource, It just follow the links. If any time I change the url of products resources, the clients will not break.

  • Use multiple representations - Not just XML and JSON, you can create your own representation format to give more power to your API.
  • Etag and Cache-Control – Your client must know what to do when it found headers like “If-Modified-Since”, “If-None-Match”, “Last Modified” . Your Api must send back informations like  etag and cache-control and your client needs to respect this.

Take time to know restfulie. It’s a great lib that can help you with this things.

Categories: API, Dicas, Ferramentas, Ruby Tags: , ,