Friday, July 20, 2012

3 Character to 2 Character Country Codes

For anyone else who needs to map 3 character to 2 character country codes in ruby, I have provided the following class based on the ISO_3166-1 country code list found at http://en.wikipedia.org/wiki/ISO_3166-1#Current_codes
module CountryCodes
class << self
def two_char_code_for(three_char_country_code)
if country = CODES[three_char_country_code.to_sym]
country[:two_char_code]
end
end
def three_char_code_for(two_char_country_code)
CODES.select {|k,v| v[:two_char_code].to_sym == two_char_country_code.upcase.to_sym}.keys.first
end
def country_name_for(three_char_country_code)
if country = CODES[three_char_country_code.upcase.to_sym]
country[:country_name]
end
end
end
CODES = {
:AFG => {:two_char_code =>'AF', :country_name => 'AFGHANISTAN'},
:ALB => {:two_char_code =>'AL', :country_name => 'ALBANIA'},
:DZA => {:two_char_code =>'DZ', :country_name => 'ALGERIA'},
:ASM => {:two_char_code =>'AS', :country_name => 'AMERICAN SAMOA'},
:AND => {:two_char_code =>'AD', :country_name => 'ANDORRA'},
:AGO => {:two_char_code =>'AO', :country_name => 'ANGOLA'},
:AIA => {:two_char_code =>'AI', :country_name => 'ANGUILLA'},
:ATA => {:two_char_code =>'AQ', :country_name => 'ANTARCTICA'},
:ATG => {:two_char_code =>'AG', :country_name => 'ANTIGUA AND BARBUDA'},
:ARG => {:two_char_code =>'AR', :country_name => 'ARGENTINA'},
:ARM => {:two_char_code =>'AM', :country_name => 'ARMENIA'},
:ABW => {:two_char_code =>'AW', :country_name => 'ARUBA'},
:AUS => {:two_char_code =>'AU', :country_name => 'AUSTRALIA'},
:AUT => {:two_char_code =>'AT', :country_name => 'AUSTRIA'},
:AZE => {:two_char_code =>'AZ', :country_name => 'AZERBAIJAN'},
:BHS => {:two_char_code =>'BS', :country_name => 'BAHAMAS'},
:BHR => {:two_char_code =>'BH', :country_name => 'BAHRAIN'},
:BGD => {:two_char_code =>'BD', :country_name => 'BANGLADESH'},
:BRB => {:two_char_code =>'BB', :country_name => 'BARBADOS'},
:BLR => {:two_char_code =>'BY', :country_name => 'BELARUS'},
:BEL => {:two_char_code =>'BE', :country_name => 'BELGIUM'},
:BLZ => {:two_char_code =>'BZ', :country_name => 'BELIZE'},
:BEN => {:two_char_code =>'BJ', :country_name => 'BENIN'},
:BMU => {:two_char_code =>'BM', :country_name => 'BERMUDA'},
:BTN => {:two_char_code =>'BT', :country_name => 'BHUTAN'},
:BOL => {:two_char_code =>'BO', :country_name => 'BOLIVIA'},
:BIH => {:two_char_code =>'BA', :country_name => 'BOSNIA AND HERZEGOWINA'},
:BWA => {:two_char_code =>'BW', :country_name => 'BOTSWANA'},
:BVT => {:two_char_code =>'BV', :country_name => 'BOUVET ISLAND'},
:BRA => {:two_char_code =>'BR', :country_name => 'BRAZIL'},
:IOT => {:two_char_code =>'IO', :country_name => 'BRITISH INDIAN OCEAN TERRITORY'},
:BRN => {:two_char_code =>'BN', :country_name => 'BRUNEI DARUSSALAM'},
:BGR => {:two_char_code =>'BG', :country_name => 'BULGARIA'},
:BFA => {:two_char_code =>'BF', :country_name => 'BURKINA FASO'},
:BDI => {:two_char_code =>'BI', :country_name => 'BURUNDI'},
:KHM => {:two_char_code =>'KH', :country_name => 'CAMBODIA'},
:CMR => {:two_char_code =>'CM', :country_name => 'CAMEROON'},
:CAN => {:two_char_code =>'CA', :country_name => 'CANADA'},
:CPV => {:two_char_code =>'CV', :country_name => 'CAPE VERDE'},
:CYM => {:two_char_code =>'KY', :country_name => 'CAYMAN ISLANDS'},
:CAF => {:two_char_code =>'CF', :country_name => 'CENTRAL AFRICAN REPUBLIC'},
:TCD => {:two_char_code =>'TD', :country_name => 'CHAD'},
:CHL => {:two_char_code =>'CL', :country_name => 'CHILE'},
:CHN => {:two_char_code =>'CN', :country_name => 'CHINA'},
:CXR => {:two_char_code =>'CX', :country_name => 'CHRISTMAS ISLAND'},
:CCK => {:two_char_code =>'CC', :country_name => 'COCOS (KEELING) ISLANDS'},
:COL => {:two_char_code =>'CO', :country_name => 'COLOMBIA'},
:COM => {:two_char_code =>'KM', :country_name => 'COMOROS'},
:COG => {:two_char_code =>'CG', :country_name => 'CONGO'},
:COK => {:two_char_code =>'CK', :country_name => 'COOK ISLANDS'},
:CRI => {:two_char_code =>'CR', :country_name => 'COSTA RICA'},
:CIV => {:two_char_code =>'CI', :country_name => 'COTE D\'IVOIRE'},
:HRV => {:two_char_code =>'HR', :country_name => 'CROATIA (local name: Hrvatska)'},
:CUB => {:two_char_code =>'CU', :country_name => 'CUBA'},
:CYP => {:two_char_code =>'CY', :country_name => 'CYPRUS'},
:CZE => {:two_char_code =>'CZ', :country_name => 'CZECH REPUBLIC'},
:DNK => {:two_char_code =>'DK', :country_name => 'DENMARK'},
:DJI => {:two_char_code =>'DJ', :country_name => 'DJIBOUTI'},
:DMA => {:two_char_code =>'DM', :country_name => 'DOMINICA'},
:DOM => {:two_char_code =>'DO', :country_name => 'DOMINICAN REPUBLIC'},
:TMP => {:two_char_code =>'TP', :country_name => 'EAST TIMOR'},
:ECU => {:two_char_code =>'EC', :country_name => 'ECUADOR'},
:EGY => {:two_char_code =>'EG', :country_name => 'EGYPT'},
:SLV => {:two_char_code =>'SV', :country_name => 'EL SALVADOR'},
:GNQ => {:two_char_code =>'GQ', :country_name => 'EQUATORIAL GUINEA'},
:ERI => {:two_char_code =>'ER', :country_name => 'ERITREA'},
:EST => {:two_char_code =>'EE', :country_name => 'ESTONIA'},
:ETH => {:two_char_code =>'ET', :country_name => 'ETHIOPIA'},
:FLK => {:two_char_code =>'FK', :country_name => 'FALKLAND ISLANDS (MALVINAS)'},
:FRO => {:two_char_code =>'FO', :country_name => 'FAROE ISLANDS'},
:FJI => {:two_char_code =>'FJ', :country_name => 'FIJI'},
:FIN => {:two_char_code =>'FI', :country_name => 'FINLAND'},
:FRA => {:two_char_code =>'FR', :country_name => 'FRANCE'},
:FXX => {:two_char_code =>'FX', :country_name => 'FRANCE, METROPOLITAN'},
:GUF => {:two_char_code =>'GF', :country_name => 'FRENCH GUIANA'},
:PYF => {:two_char_code =>'PF', :country_name => 'FRENCH POLYNESIA'},
:ATF => {:two_char_code =>'TF', :country_name => 'FRENCH SOUTHERN TERRITORIES'},
:GAB => {:two_char_code =>'GA', :country_name => 'GABON'},
:GMB => {:two_char_code =>'GM', :country_name => 'GAMBIA'},
:GEO => {:two_char_code =>'GE', :country_name => 'GEORGIA'},
:DEU => {:two_char_code =>'DE', :country_name => 'GERMANY'},
:GHA => {:two_char_code =>'GH', :country_name => 'GHANA'},
:GIB => {:two_char_code =>'GI', :country_name => 'GIBRALTAR'},
:GRC => {:two_char_code =>'GR', :country_name => 'GREECE'},
:GRL => {:two_char_code =>'GL', :country_name => 'GREENLAND'},
:GRD => {:two_char_code =>'GD', :country_name => 'GRENADA'},
:GLP => {:two_char_code =>'GP', :country_name => 'GUADELOUPE'},
:GUM => {:two_char_code =>'GU', :country_name => 'GUAM'},
:GTM => {:two_char_code =>'GT', :country_name => 'GUATEMALA'},
:GIN => {:two_char_code =>'GN', :country_name => 'GUINEA'},
:GNB => {:two_char_code =>'GW', :country_name => 'GUINEA-BISSAU'},
:GUY => {:two_char_code =>'GY', :country_name => 'GUYANA'},
:HTI => {:two_char_code =>'HT', :country_name => 'HAITI'},
:HMD => {:two_char_code =>'HM', :country_name => 'HEARD AND MC DONALD ISLANDS'},
:VAT => {:two_char_code =>'VA', :country_name => 'HOLY SEE (VATICAN CITY STATE)'},
:HND => {:two_char_code =>'HN', :country_name => 'HONDURAS'},
:HKG => {:two_char_code =>'HK', :country_name => 'HONG KONG'},
:HUN => {:two_char_code =>'HU', :country_name => 'HUNGARY'},
:ISL => {:two_char_code =>'IS', :country_name => 'ICELAND'},
:IND => {:two_char_code =>'IN', :country_name => 'INDIA'},
:IDN => {:two_char_code =>'ID', :country_name => 'INDONESIA'},
:IRN => {:two_char_code =>'IR', :country_name => 'IRAN (ISLAMIC REPUBLIC OF)'},
:IRQ => {:two_char_code =>'IQ', :country_name => 'IRAQ'},
:IRL => {:two_char_code =>'IE', :country_name => 'IRELAND'},
:ISR => {:two_char_code =>'IL', :country_name => 'ISRAEL'},
:ITA => {:two_char_code =>'IT', :country_name => 'ITALY'},
:JAM => {:two_char_code =>'JM', :country_name => 'JAMAICA'},
:JPN => {:two_char_code =>'JP', :country_name => 'JAPAN'},
:JOR => {:two_char_code =>'JO', :country_name => 'JORDAN'},
:KAZ => {:two_char_code =>'KZ', :country_name => 'KAZAKHSTAN'},
:KEN => {:two_char_code =>'KE', :country_name => 'KENYA'},
:KIR => {:two_char_code =>'KI', :country_name => 'KIRIBATI'},
:PRK => {:two_char_code =>'KP', :country_name => 'KOREA, DEMOCRATIC PEOPLE\'S REPUBLIC OF'},
:KOR => {:two_char_code =>'KR', :country_name => 'KOREA, REPUBLIC OF'},
:KWT => {:two_char_code =>'KW', :country_name => 'KUWAIT'},
:KGZ => {:two_char_code =>'KG', :country_name => 'KYRGYZSTAN'},
:LAO => {:two_char_code =>'LA', :country_name => 'LAO PEOPLE\'S DEMOCRATIC REPUBLIC'},
:LVA => {:two_char_code =>'LV', :country_name => 'LATVIA'},
:LBN => {:two_char_code =>'LB', :country_name => 'LEBANON'},
:LSO => {:two_char_code =>'LS', :country_name => 'LESOTHO'},
:LBR => {:two_char_code =>'LR', :country_name => 'LIBERIA'},
:LBY => {:two_char_code =>'LY', :country_name => 'LIBYAN ARAB JAMAHIRIYA'},
:LIE => {:two_char_code =>'LI', :country_name => 'LIECHTENSTEIN'},
:LTU => {:two_char_code =>'LT', :country_name => 'LITHUANIA'},
:LUX => {:two_char_code =>'LU', :country_name => 'LUXEMBOURG'},
:MAC => {:two_char_code =>'MO', :country_name => 'MACAU'},
:MKD => {:two_char_code =>'MK', :country_name => 'MACEDONIA, THE FORMER YUGOSLAV REPUBLIC OF'},
:MDG => {:two_char_code =>'MG', :country_name => 'MADAGASCAR'},
:MWI => {:two_char_code =>'MW', :country_name => 'MALAWI'},
:MYS => {:two_char_code =>'MY', :country_name => 'MALAYSIA'},
:MDV => {:two_char_code =>'MV', :country_name => 'MALDIVES'},
:MLI => {:two_char_code =>'ML', :country_name => 'MALI'},
:MLT => {:two_char_code =>'MT', :country_name => 'MALTA'},
:MHL => {:two_char_code =>'MH', :country_name => 'MARSHALL ISLANDS'},
:MTQ => {:two_char_code =>'MQ', :country_name => 'MARTINIQUE'},
:MRT => {:two_char_code =>'MR', :country_name => 'MAURITANIA'},
:MUS => {:two_char_code =>'MU', :country_name => 'MAURITIUS'},
:MYT => {:two_char_code =>'YT', :country_name => 'MAYOTTE'},
:MEX => {:two_char_code =>'MX', :country_name => 'MEXICO'},
:FSM => {:two_char_code =>'FM', :country_name => 'MICRONESIA, FEDERATED STATES OF'},
:MDA => {:two_char_code =>'MD', :country_name => 'MOLDOVA, REPUBLIC OF'},
:MCO => {:two_char_code =>'MC', :country_name => 'MONACO'},
:MNG => {:two_char_code =>'MN', :country_name => 'MONGOLIA'},
:MSR => {:two_char_code =>'MS', :country_name => 'MONTSERRAT'},
:MAR => {:two_char_code =>'MA', :country_name => 'MOROCCO'},
:MOZ => {:two_char_code =>'MZ', :country_name => 'MOZAMBIQUE'},
:MMR => {:two_char_code =>'MM', :country_name => 'MYANMAR'},
:NAM => {:two_char_code =>'NA', :country_name => 'NAMIBIA'},
:NRU => {:two_char_code =>'NR', :country_name => 'NAURU'},
:NPL => {:two_char_code =>'NP', :country_name => 'NEPAL'},
:NLD => {:two_char_code =>'NL', :country_name => 'NETHERLANDS'},
:ANT => {:two_char_code =>'AN', :country_name => 'NETHERLANDS ANTILLES'},
:NCL => {:two_char_code =>'NC', :country_name => 'NEW CALEDONIA'},
:NZL => {:two_char_code =>'NZ', :country_name => 'NEW ZEALAND'},
:NIC => {:two_char_code =>'NI', :country_name => 'NICARAGUA'},
:NER => {:two_char_code =>'NE', :country_name => 'NIGER'},
:NGA => {:two_char_code =>'NG', :country_name => 'NIGERIA'},
:NIU => {:two_char_code =>'NU', :country_name => 'NIUE'},
:NFK => {:two_char_code =>'NF', :country_name => 'NORFOLK ISLAND'},
:MNP => {:two_char_code =>'MP', :country_name => 'NORTHERN MARIANA ISLANDS'},
:NOR => {:two_char_code =>'NO', :country_name => 'NORWAY'},
:OMN => {:two_char_code =>'OM', :country_name => 'OMAN'},
:PAK => {:two_char_code =>'PK', :country_name => 'PAKISTAN'},
:PLW => {:two_char_code =>'PW', :country_name => 'PALAU'},
:PAN => {:two_char_code =>'PA', :country_name => 'PANAMA'},
:PNG => {:two_char_code =>'PG', :country_name => 'PAPUA NEW GUINEA'},
:PRY => {:two_char_code =>'PY', :country_name => 'PARAGUAY'},
:PER => {:two_char_code =>'PE', :country_name => 'PERU'},
:PHL => {:two_char_code =>'PH', :country_name => 'PHILIPPINES'},
:PCN => {:two_char_code =>'PN', :country_name => 'PITCAIRN'},
:POL => {:two_char_code =>'PL', :country_name => 'POLAND'},
:PRT => {:two_char_code =>'PT', :country_name => 'PORTUGAL'},
:PRI => {:two_char_code =>'PR', :country_name => 'PUERTO RICO'},
:QAT => {:two_char_code =>'QA', :country_name => 'QATAR'},
:REU => {:two_char_code =>'RE', :country_name => 'REUNION'},
:ROM => {:two_char_code =>'RO', :country_name => 'ROMANIA'},
:RUS => {:two_char_code =>'RU', :country_name => 'RUSSIAN FEDERATION'},
:RWA => {:two_char_code =>'RW', :country_name => 'RWANDA'},
:KNA => {:two_char_code =>'KN', :country_name => 'SAINT KITTS AND NEVIS'},
:LCA => {:two_char_code =>'LC', :country_name => 'SAINT LUCIA'},
:VCT => {:two_char_code =>'VC', :country_name => 'SAINT VINCENT AND THE GRENADINES'},
:WSM => {:two_char_code =>'WS', :country_name => 'SAMOA'},
:SMR => {:two_char_code =>'SM', :country_name => 'SAN MARINO'},
:STP => {:two_char_code =>'ST', :country_name => 'SAO TOME AND PRINCIPE'},
:SAU => {:two_char_code =>'SA', :country_name => 'SAUDI ARABIA'},
:SEN => {:two_char_code =>'SN', :country_name => 'SENEGAL'},
:SYC => {:two_char_code =>'SC', :country_name => 'SEYCHELLES'},
:SLE => {:two_char_code =>'SL', :country_name => 'SIERRA LEONE'},
:SGP => {:two_char_code =>'SG', :country_name => 'SINGAPORE'},
:SVK => {:two_char_code =>'SK', :country_name => 'SLOVAKIA (Slovak Republic)'},
:SVN => {:two_char_code =>'SI', :country_name => 'SLOVENIA'},
:SLB => {:two_char_code =>'SB', :country_name => 'SOLOMON ISLANDS'},
:SOM => {:two_char_code =>'SO', :country_name => 'SOMALIA'},
:ZAF => {:two_char_code =>'ZA', :country_name => 'SOUTH AFRICA'},
:SGS => {:two_char_code =>'GS', :country_name => 'SOUTH GEORGIA AND THE SOUTH SANDWICH ISLANDS'},
:ESP => {:two_char_code =>'ES', :country_name => 'SPAIN'},
:LKA => {:two_char_code =>'LK', :country_name => 'SRI LANKA'},
:SHN => {:two_char_code =>'SH', :country_name => 'ST. HELENA'},
:SPM => {:two_char_code =>'PM', :country_name => 'ST. PIERRE AND MIQUELON'},
:SDN => {:two_char_code =>'SD', :country_name => 'SUDAN'},
:SUR => {:two_char_code =>'SR', :country_name => 'SURINAME'},
:SJM => {:two_char_code =>'SJ', :country_name => 'SVALBARD AND JAN MAYEN ISLANDS'},
:SWZ => {:two_char_code =>'SZ', :country_name => 'SWAZILAND'},
:SWE => {:two_char_code =>'SE', :country_name => 'SWEDEN'},
:CHE => {:two_char_code =>'CH', :country_name => 'SWITZERLAND'},
:SYR => {:two_char_code =>'SY', :country_name => 'SYRIAN ARAB REPUBLIC'},
:TWN => {:two_char_code =>'TW', :country_name => 'TAIWAN, PROVINCE OF CHINA'},
:TJK => {:two_char_code =>'TJ', :country_name => 'TAJIKISTAN'},
:TZA => {:two_char_code =>'TZ', :country_name => 'TANZANIA, UNITED REPUBLIC OF'},
:THA => {:two_char_code =>'TH', :country_name => 'THAILAND'},
:TGO => {:two_char_code =>'TG', :country_name => 'TOGO'},
:TKL => {:two_char_code =>'TK', :country_name => 'TOKELAU'},
:TON => {:two_char_code =>'TO', :country_name => 'TONGA'},
:TTO => {:two_char_code =>'TT', :country_name => 'TRINIDAD AND TOBAGO'},
:TUN => {:two_char_code =>'TN', :country_name => 'TUNISIA'},
:TUR => {:two_char_code =>'TR', :country_name => 'TURKEY'},
:TKM => {:two_char_code =>'TM', :country_name => 'TURKMENISTAN'},
:TCA => {:two_char_code =>'TC', :country_name => 'TURKS AND CAICOS ISLANDS'},
:TUV => {:two_char_code =>'TV', :country_name => 'TUVALU'},
:UGA => {:two_char_code =>'UG', :country_name => 'UGANDA'},
:UKR => {:two_char_code =>'UA', :country_name => 'UKRAINE'},
:ARE => {:two_char_code =>'AE', :country_name => 'UNITED ARAB EMIRATES'},
:GBR => {:two_char_code =>'GB', :country_name => 'UNITED KINGDOM'},
:USA => {:two_char_code =>'US', :country_name => 'UNITED STATES'},
:UMI => {:two_char_code =>'UM', :country_name => 'UNITED STATES MINOR OUTLYING ISLANDS'},
:URY => {:two_char_code =>'UY', :country_name => 'URUGUAY'},
:UZB => {:two_char_code =>'UZ', :country_name => 'UZBEKISTAN'},
:VUT => {:two_char_code =>'VU', :country_name => 'VANUATU'},
:VEN => {:two_char_code =>'VE', :country_name => 'VENEZUELA'},
:VNM => {:two_char_code =>'VN', :country_name => 'VIET NAM'},
:VGB => {:two_char_code =>'VG', :country_name => 'VIRGIN ISLANDS (BRITISH)'},
:VIR => {:two_char_code =>'VI', :country_name => 'VIRGIN ISLANDS (U.S.)'},
:WLF => {:two_char_code =>'WF', :country_name => 'WALLIS AND FUTUNA ISLANDS'},
:ESH => {:two_char_code =>'EH', :country_name => 'WESTERN SAHARA'},
:YEM => {:two_char_code =>'YE', :country_name => 'YEMEN'},
:YUG => {:two_char_code =>'YU', :country_name => 'YUGOSLAVIA'},
:ZAR => {:two_char_code =>'ZR', :country_name => 'ZAIRE'},
:ZMB => {:two_char_code =>'ZM', :country_name => 'ZAMBIA'},
:ZWE => {:two_char_code =>'ZW', :country_name => 'ZIMBABWE'}
}
end

Saturday, July 14, 2012

Google-api-client Authorizing with an API Key in Ruby

The documentation for the Google RESTful APIs is generally pretty good, however when playing with the google-api-client ruby gem, developed by Google to trawl their APIs, I ran into a few issues, particularly when authenticating using an api key (rather than OAuth).

After installing the google-api-client gem, getting a Google API Key (https://code.google.com/apis/console/), and setting up a custom search account (with its prefs widened to all web pages - http://www.google.com/cse/)....

The following allowed me to trawl google search results (copy paste into irb, then inspect response when finished):

  require 'openssl'
  OpenSSL::SSL::VERIFY_PEER = OpenSSL::SSL::VERIFY_NONE

  require 'google/api_client'
  client = Google::APIClient.new(:key => 'your-api-key', :authorization => nil)
  search = client.discovered_api('customsearch')

  response = client.execute(
    :api_method => search.cse.list,
    :parameters => {
      'q' => 'the hoff',
      'key' => 'your-api-key',
      'cx' => 'your-custom-search-id'
    }
  )

THE MOST IMPORTANT BIT was the :authorization param when constructing he client.... this ensures the api key is used when calling, in preference to oauth. Without it you will get 401 Unauthorized response status everytime.

Thursday, July 12, 2012

ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)

Somehow my root account lost its 'ALL PRIVILEGES' option, and instead was listing all individual privileges when running

show grants;

As root instead of:
+----------------------------------------------------------------------------------------------------------------------------------------+
| Grants for root@localhost                                                                                                              |
+----------------------------------------------------------------------------------------------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' IDENTIFIED BY PASSWORD '*76854B38A7923CC05E7857229F508E66E89D69AD' WITH GRANT OPTION |
+----------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

This was preventing me from assigning all privileges to *.* to other users....
The solution for me (in OSX 10.6.8) was to run in terminal:

mysql_upgrade

which rebuilt my grants table, then after logging into mysql as root, retrying the grant cmd:
GRANT ALL PRIVILEGES ON *.* TO 'myuser'@'localhost'