Skip to content

Instantly share code, notes, and snippets.

@aisuii
Created June 1, 2011 02:23
Show Gist options
  • Save aisuii/1001684 to your computer and use it in GitHub Desktop.
Save aisuii/1001684 to your computer and use it in GitHub Desktop.
# -*- coding: utf-8 -*-
require 'open-uri'
require 'rubygems'
require 'active_support/core_ext'
class GetQrcode
API_URL = "http://chart.apis.google.com/chart"
API_TYPE = "qr"
IMAGE_SIZE = "100x100"
DEFAULT_OPTIONS = {
:cht => API_TYPE,
:chs => IMAGE_SIZE
}
def initialize(site_url, local_path, options = {})
@site_url = site_url
@local_path = local_path
@options = DEFAULT_OPTIONS.merge(options)
end
def get
api_path = API_URL + '?' + params
open(api_path) do |qr|
open(@local_path, 'wb+'){|file| file.write(qr.read) }
end
end
def params
# sample_url : http://chart.apis.google.com/chart?chs=150x150&cht=qr&chl=http%3a//netafull.jp/hoge?foo=q
@options.merge(:chl => @site_url).to_param
end
end
if $0 == __FILE__
# sample
get_qrcode = GetQrcode.new('http://google.co.jp', 'test.png')
get_qrcode.get
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment