May 5, 2014

Failed plenv install-cpanm and ended up installing curl with --with-openssl option

Today I tried `plenv install-cpanm` and it failed. I do remember it worked fine a couple of month ago and the only thing I can think of was that I did `yum update openssl` last month. I'm pretty sure I didn't do anything else nasty.

I searched and found someone with same problem. I followed what he did as shown below: Then I succeeded to run plenv install-cpanm
% plenv install-cpanm      
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   303    0   303    0     0    509      0 --:--:-- --:--:-- --:--:--  7973
  0     0    0     0    0     0      0      0 --:--:--  0:00:01 --:--:--     0
100  262k  100  262k    0     0   113k      0  0:00:02  0:00:02 --:--:-- 3012k
--> Working on App::cpanminus
Fetching http://www.cpan.org/authors/id/M/MI/MIYAGAWA/App-cpanminus-1.7004.tar.gz ... OK
Configuring App-cpanminus-1.7004 ... OK
Building and testing App-cpanminus-1.7004 ... OK
Successfully installed App-cpanminus-1.7004
1 distribution installed

May 3, 2014

Facebook::OpenGraph Ver.1.21 supports API versioning

On 2014-04-30 Facebook announced API versioning to improve its stability. Each version is guaranteed to work 2 years so we do not have to be afraid of 90 day breaking change policy anymore. The version we were using prior to this introduction is called version 1.0 while the new version introduced along with API versioning policy is called version 2.0.

Versioned Request

Making versioned request is pretty easy. Just like other major platform APIs, you prepend version number to the end point like below:
/v2.0/me/friends
Unversioned request is also supported. Just don't prepend version number and it's considered "unversioned." The document says "An unversioned call will default to the oldest available version of the API," but here is one tricky part:
For apps that existed before April 30th 2014, making an API call without specifying a version number ('unversioned') is equivalent to making a call to the v1.0 of the API.
For apps created on or after April 30th 2014, making an API call without a specifing a version number is equivalent to making a call to v2.0 of the API.
So when you are making unversioned request, just be careful when the request app was created.

Facebook::OpenGraph 1.21

I implemented versioning support to Facebook::OpenGraph and launched it as Ver. 1.21. This change is backward compatible. If you update module and don't make any change to your code it makes unversioned request, which is exactly the same as previous version of this module.

API Versioning

With this version, you can specify default version on initialization as follows:
my $fb = Facebook::OpenGraph->new(+{
    app_id  => 12345,
    secret  => 'qwerty',
    version => 'v2.0',
});
If you don't specify any version,  version is left undef and each request becomes unversioned request.
You can still set version on each request by specifying versioned path such as /v1.0/me/friends. This will override the version configuration given on initialization.
my $fb = Facebook::OpenGraph->new(+{version => 'v2.0'});
$fb->get('/zuck');      # Use version 2.0 by accessing /v2.0/zuck
$fb->get('/v1.0/zuck'); # Ignore the default version and use version 1.0
 
my $fb = Facebook::OpenGraph->new();
$fb->get('/zuck'); # Unversioned API access since version is not specified
                   # on initialisation or each reqeust.

Dialog Versioning

Facebook Platform also supports dialog versioning. If you specify version -- let's say v2.0 -- on initialization, $fb->auth_uri() will return versioned Login dialog URL like https://www.facebook.com/v2.0/dialog/oauth.

If you have any enhancement idea, bug fix or question. Feel free to add an issue.

Apr 27, 2014

Facebook::OpenGraph 1.20 w/ JS SDK's cookie support is now available

Facebook::OpenGraph 1.20 is now available.

Work with JS SDK

Among 2 newly added features, one deals with cookie that is set by JS SDK. Detailed implementation process can be seen at github issue. $fb->js_cookie_name will give you the name of the cookie and $fb->get_user_token_by_cookie( $cookie_value ) will hit auth endpoint and exchange cookie value to user access token. Sample usage is shown as below:

Access Response Header

The other feature deals with response header. $res->header( $header_field_name ) provides access to any given header field. As a shorthand $res->etag is provided since $fb->fetch_with_etag supports access with etag value. Other than ETag header field there are some fields that you should know: X-FB-Rev and X-FB-Debug. As described in official blog entry, X-FB-Rev indicate Facebook Platform's internal revision number. Official document is not provided for X-FB-Debug, but it must be something that help resolving their issue. So when you get error response even though your request seems legitimate, you might want to check $res->header(''x-fb-rev) and $res->header('x-fb-debug') and report them on bug report.

Apr 13, 2014

Released Facebook::OpenGraph Ver. 1.13

Today I released Facebook::OpenGraph Ver. 1.13, which contains the addition of exchange_token() to support Generating Long-Lived User Tokens from Server-Side. That's pretty much everything that concerns interface change, but once you see the diffs you'll find many additions and updates of documentation links, POD and comments.

Back in 2009-2010, those were totally outdated and we had to exchange information on official wiki. Even in 2013 they did not provide documentation about appsecret_proof thing so we had to look into official PHP SDK code to find out its implementation and translate it into other language of your choice. It is really frustrating when you have to implement something yourself, but you cannot tell if your implementation satisfy its specification.

Now, however, official documents are getting enhanced day by day. They state more detailed specs and provide more step-by-step sample codes. So what I did is adding more links to corresponding part of documents and more quotes in a form of comments and POD. Quoted comments are directly copied from official documentation and are appended to each corresponding logical part. This way you or future maintainer can see the spec and implementation at once. When this module does not work as expected, compare these comments and implementation to the linked document. If there is any difference the document and spec is updated, but this module is not. The document does not provide updated date anymore so I think this is the easiest way to catch up the spec change.

I hope it helps you better integrate with Facebook Graph API.

Mar 21, 2014

Ideas on utilizing Teng#new_row_from_hash

This article is basically a follow-up of my previous article: Teng::Row and data2row.
As of Teng Ver. 0.21, data2row is implemented as Teng#new_row_from_hash. I already introduced a way how I used this method on my previous article, so I am going to introduce 2 different usages.

Examples are as shown on bottom.

The first example describes how to cache column values and then creates table row object with cached values. With Devel::Size I compared the size of row object and column values then I realized that the size of row objects become much bigger. So my idea is to cache only the column values to minimize cache size and then create table row object with the cached values.

The Second example shows how I utilize the table row object's method by creating temporary object. In this case I'm trying to upload image to a certain path and the path is generated based on the filename and campaign_type. I didn't want to generate path in 2 places, but instead I wanted to generate it at one place so I'm not going to mess up by modifying one and forgetting modifying the other.

I like the way Teng enables me to do this kind of things easier.


Feb 16, 2014

Benchmarked Sort::Maker's each sort style for my specific purpose

I use various internal and external APIs to build my services. An external API returns content something like below:
{
    "status": 1,
    "error_msg": '',
    "result": {
        "foo1": { .... },
        "foo2": { .... },
        "foo3": { .... },
        "foo4": { .... }
    }
}
After retrieving this, I have to sort the result in result.foo1, result.foo2, ..., result.foo100 order. I'm wondering why they don't return this in array, but I must face it as long as the provider is returning values in this way. Although Perl Best Practices insists on using Sort::Maker, I implemented this in my way because this sort wasn't that complicated.
my %tmp_cache; # for orcish maneuver
my @sorted_keys = sort {
    ( $tmp_cache{$a} //= $a =~ s/\A foo(\d+) \z/$1/xr ) <=>
    ( $tmp_cache{$b} //= $b =~ s/\A foo(\d+) \z/$1/xr )
} keys %{ $content->{result} };

# then sorted results are stored in stash to be displayed
$c->stash->{results} = [ map { $content->{result}->{$_} } @sorted_keys ];
Today, I benchmarked each sort type provided by Sort::Maker and found that using Sort::Maker was much faster. The code is as below. It was a bit surprising that my original method with orcish maneuver is13% slower than Sort::Maker's one with orcish maneuver. Needless to say, other sort types are much faster. So my conclusion is that even relatively simple sort should be implemented with Sort::Maker to increase readability, maintenancibility and performance.

Feb 9, 2014

Released Lingua::PigLatin::Bidirectional Ver.0.01

The other day I talked about Lingua::PigLatin and how it works. This module really helped me understand how Pig Latin works, but this module only handled English-to-PigLatin translation. So I created Lingua::PigLatin::Bidirectional. As the name implies, this translates English sentences to Pig Latin, and vice versa.
use Lingua::PigLatin::Bidirectional;
 
warn to_piglatin('hello');     # ellohay
warn from_piglatin('ellohay'); # hello
My IRC bot uses this module so when I want to pig-latinize some sentences or want to see if my pig-latinization is correct, I talk to this bot and it returns translated sentences.