Friday 31 July 2015

Google Wallet Offers Expiry Dates

I've been working with Google Wallet API for the last week and it's been interesting, but also a little frustrating.

 If you're reading this then you've probably come to the realisation that setting up Google Wallet Offers is not a simple task, especially when compared to setting up Apple's Passbook Coupons.

I'm on the last leg of this project now and one of the final tasks is to roll expiry dates into the Google Offers I've created.

There's a handy snippet of code found at https://developers.google.com/wallet/objects/offers/tutorial which suggests it should be no big deal...

$validTimeInterval = new Google_TimeInterval();
$startDateTime = new Google_DateTime();
$startDateTime->setDate('2013-06-12T23:20:50.52Z');
$validTimeInterval->setStart($startDateTime);
$endDateTime = new Google_DateTime();
$endDateTime->setDate('2013-12-12T23:20:50.52Z');
$validTimeInterval->setEnd($endDateTime);

and then during the Offer creation:

$offerObject->setValidTimeInterval($validTimeInterval);


Unfortunately there's a little wrinkle there - the Google_TimeInterval and Google_DateTime classes were not included in any of the API libraries I couldn't find, and I couldn't find anything useful about where to get them.


Solution:

What you should be looking for is Google_Service_Walletobjects_TimeInterval and Google_Service_Walletobjects_DateTime so your code will be like this:

$validTimeInterval = new Google_Service_Walletobjects_TimeInterval();
$startDateTime = new Google_Service_Walletobjects_DateTime();
$startDateTime->setDate('2013-06-12T23:20:50.52Z');
$validTimeInterval->setStart($startDateTime);
$endDateTime = new Google_Service_Walletobjects_DateTime();
$endDateTime->setDate('2013-12-12T23:20:50.52Z');
$validTimeInterval->setEnd($endDateTime);

Tuesday 4 February 2014

Safari: Setting third party iframe cookies

Safari is known to be strict about permissions in iframes, especially when the domain of the iframe page is different from the domain of the parent page. Some would even say paranoically strict.

Safari will block you from setting cookies for the third-party domain (the different domain in the iframe), unless you already have cookies set for that domain.

Here's a snippet of javascript I pulled together last week that as a way to get around the iframe cookie security. It works great if your page is nice and light and loads fast, otherwise it can feel pretty clunky with the triple loading...

window.onload=function(){
 if(navigator.userAgent.indexOf('Safari')!=-1&&navigator.userAgent.indexOf('Chrome')==-1){
  var cookies=document.cookie;
  if(top.location!=document.location){
   if(!cookies){
    href=document.location.href;
    href=(href.indexOf('?')==-1)?href+'?':href+'&';
    top.location.href =href+'reref='+encodeURIComponent(document.referrer);
   }
  } else {
   ts=new Date().getTime();document.cookie='ts='+ts;
   rerefidx=document.location.href.indexOf('reref=');
   if(rerefidx!=-1){
    href=decodeURIComponent(document.location.href.substr(rerefidx+6));
    window.location.replace(href);
   }
  }
 }
}

Here's what it basically does:
  • The javascript is placed in the page loaded inside the iframe. 
  • If the JS is run inside the iframe and the browser is Safari and there are no cookies set, then we frame-burst the page to take over the window and append the original parent page URL as a parameter. 
  • If the JS is run not inside a frame and the browser is Safari, then we set a timestamp cookie (now that we are out of the iframe) and if a reref param exists we redirect back to the original page.
  • If the JS is run inside the iframe and the browser is Safari and there ARE cookies set, then we do nothing.

And one more time in pseudo-code

if(browser is Safari){
 if(we are in an iframe){
  if(no cookies){
   set window_url = iframe_url + reref=iframe_parent_url;
  }
 } else {
  set a timestamp cookie;
  if(reref exists in url){
   redirect to original url so we have iframe again
  }
 }
}

Tuesday 21 January 2014

WordPress: Remove/hide Google+ field from Contact Info on user admin page

This WordPress tweak is pretty unlikely to come up for most developers, but I was asked to remove the Google+ field from the Contact Info section of the User admin page. Here's a quick snippet to paste into functions.php to do so:

function custom_admin_js() {
  // hide WP's Googleplus input on user profile
  if( strpos($_SERVER["REQUEST_URI"], '/user-edit.php')) {
    echo '<script type="text/javascript"> jQuery(\'#your-profile input[name="googleplus"]\').parent().parent().hide(); </script>';
  }
}
add_action('admin_footer', 'custom_admin_js');

Monday 22 July 2013

Checking A File's Upload Size In The Browser

This really just a quick note so I don't lose the code snippet I just used...

I'm working on a form which allows users to upload a file, but we want to stop them uploading giant things. Ultimately the server's maximum upload filesize will stop anything too huge getting through, but it's nice to remind people of the limit before they bother uploading.

This is a great little snippet to do that in current versions of Chrome, Firefox and Safari.

$('#contact_file').bind('change', function() { 
  filebytes = this.files[0].size;
  if(filebytes>1024*1024*5) {
    alert("The file you selected is TOO LARGE and will be rejected by the server. Please select a smaller file."); 
  }
});

If you can't work out what is happening there, then "contact_file" is the id from your file input and 1024*1024*5 is the limit you want to impose, in this case 5MB. Don't forget to check that the server side limit is in line with this.

It won't work in IE* or some older browser versions, but it's a nice extra courtesy for users where it can be executed. You could also hook it up to the submit button if you want to do a check at that stage.

*Apparently there's an ActiveX thing you can do for IE users, but I am not so inclined.

Sunday 7 July 2013

How To: International Exchange Rates in iOS Stocks App

I like to keep an eye on the exchange rate for the US Dollar against the British Pound Sterling GBP and the Euro, and have been using the Stocks app built into iOS. I don't have any stocks, so I only use the application for tracking exchange rates and it performs well for my needs.




Over the last few weeks I have shared this functionality with a few friends who also want to keep track of exchange rates and realized that setting these up in the Stocks app is not at all obvious, some would say impossible unless you know the process. Thankfully, once you know the process, it's a piece of cake.

Here are the steps

  1. Open the Stocks app
  2. Tap the ( i ) icon in the bottom right hand corner
  3. Tap the +  icon in the top left corner
  4. Enter the currency rate described as AAABBB=X in the search box (e.g: GBPUSD=X )
  5. Tap the item in the list
  6. Click the "Done" button in the top right corner



How To: Import an Excel CSV file with Sequel Pro

I've been working with a database migration project lately that is bringing together data from three different sources. One of those sources is a big Excel spreadsheet. The data in the spreadsheet has been problematic due as each row included cells with pipe delimited items, typos and other strangenesses that have slowed the pat to a relational database. On top of "ropey data", I also had some problems with importing the data into mySQL.

Usually I would just Save As a CSV (comma separated values) file, and then import the CSV file using OSX application Sequel Pro. This time I was getting fields by split due to some of the fields containing quotes and commas in their text content. I tried to looking for options to use different delimiters, but this is not available Excel (certainly not the version I use), I tried opening the spreadsheet in Google Docs and saving as CSV from there, but this returned the same result. It seemed that Sequel Pro's import engine was not able to interpret the "" encoding of " content and the age-old options of backslashing  \" or custom delimiters are no longer available. Upgrading to the latest version of Sequel Pro didn't help either.

I did a bit of searching and eventually found a source that outlined the solution to the problem. Realistically, it's a simple thing I should've thought of myself, but if I missed it and others have too, I thought it was worth documenting here so that others could save time looking for a solution.

The problem here is that the CSV you have uses Excel-style quote escaping, where escaped double quotes are represented by "" instead of using backslash escaping - \" . 
When you select the CSV file in the import dialog, you can see there's a "Fields escaped by" setting underneath the file selection dialog.  If you change this to a double quote, instead of a backslash, you'll find your file is imported correctly (and your choice will be remembered in future).

Here's an image of what you need to alter for Excel files to be processed properly:


Don't forget that you've made this change when it comes to importing other CSV files. The last part of the quote above states "and your choice will be remembered in future", so you *may* need to change it back to backslash \ for other files.

Wednesday 5 September 2012

GarageBand '11 : Upgrade your instruments for free

I've been writing and playing music for a number of years and lately have been using Apple's GarageBand to capture my writing ideas. It's certainly not a fully fledged DAW, but I've found it to be more than adequate for getting an idea fleshed out to a demo quality recording.

Here are some example tracks to show what I've been producing with GarageBand lately:











The more I have used GarageBand, the more I have found its limitations (although these are not unreasonable limits for the application that it is). The main one for me is that the software instruments are  a minimal set, some of which are what I would consider novelty sounds (basically unusable) and others are flawed with pitch issues (e.g. Fretless Electric Bass). Coupling this with not being able to MIDI OUT to an external device (of which I have a few), the software instrument set can become a bit frustrating.

I really don't like the default Electric Piano instrument (sounds too crunchy like a clav to me), I want a sound like a classic Rhodes or a Yamaha DX7, so I decided to find out how to get one without throwing any money at it.


Audio Unit Instruments / Modules

The first solutions I found were from 4Front Technologies. I downloaded and installed their E-Piano (based on Yamaha DX7 e-piano), R-Piano (based on Rhodes Stage Piano) and the 4Front Bass since I was noticing some pitchiness on Fingerstyle Electric Bass (specifically playing A1). I'd recommend grabbing them all and the Upright Piano so you can install them all in one hit,

Here's the process to install and use these instruments:
  • Download the instruments you want from 4Front
  • Unzip the file and copy the .component file to Library/Audio/Plug-Ins/Components
  • Quit GarageBand if it is running
  • Open GarageBand and open a project 
  • Create a new Software Instrument track and open the Track Info pane ( ⌘+I )
  • Click the Edit tab and then the Sound Generator list, your new instrument(s) should appear in the Audio Unit Modules section
  • Select the instrument you wish to use
  • Adjust any instrument settings or Effects

Select your new instrument in the "Audio Unit Modules" section

If you want the instrument to show up on the Browse tab in the correct list, then follow these steps:
  • Select the instrument group on the Browse tab that you want the instrument to be saved into, and the select icon that you want to be used.
  • Click the Edit tab and select the instrument as the Sound Generator
  • Adjust any instrument settings or Effects
  • Click on "Save Instrument..." and enter the name for this instrument

Obviously the choice of free instruments available is not as broad as those available for purchase, but there are some good ones out there. There is a good freeware AU list at Don't Crack.com and an extensive list at KVR Audio, although many are only VST plugins, not AU. Just remember that you get what you pay for, so don't be upset if some don't work well.

UPDATE: You can also find a good selection of plugins which are available in AU format in "The 27 best free VST plug-ins in the world today" (2012) on MusicRadar.com


Soundfonts (.sf2)

Another option for expanding your instrument arsenal is Soundfonts. These files have been around since the 1990s, so there is a large variety in both instruments and quality. There are probably a lot more

There are a few quirks about using these for instrument sounds. GarageBand will use .sf2 files, but not the archive formats .sfark or .sfpack. Both these formats appear to be abandoned now and unpacking the soundfonts on OSX is next to impossible.

Although some soundfonts are available as "collections", these are a little less easy to use in GarageBand. Unless you can send a MIDI Program Change to the track, you'll be stuck using the first sound/voice in that collection. I tend to use single instrument soundfonts so that I can save them as an instrument once installed.

Some sources I have used for .sf2 soundfont files are HammerSound, ЯK Hive, NTONYX and  www.johannes.fr. Probably the largest soundfont archive is sf2midi.com, although it has some drawbacks: you need to register to download, the free downloads are slow (and require too many clicks in my opinion), and the zip files cannot be opened by OSX's Archive Utility (10.6)  - luckily they can be unzipped by the free app The Unarchiver. Nearly all of the .sf2 files I have tried to date have worked well, but some just don't work.


Here's the process to install and use a soundfont in GarageBand:
  • Find and download the desired soundfont .sf2 file
  • Copy the file to Library/Audio/Sounds/Banks
  • Quit GarageBand if it is running
  • Open GarageBand and open a project 
  • Create a new Software Instrument track and open the Track Info pane ( ⌘+I )
  • Click the Edit tab and then the Sound Generator list and select DLSMusicDevice from the Audio Unit Modules section
  • Double-Click the large button to the left of the DLSMusicDevice to edit the settings
  • Select your soundfont in the Apple Sound Bank Synthesizer list (QuickTime Music Synthesizer is the default value)
You can save the soundfont instrument so it is listed on the Browse tab by using the same steps outlined above for AU Instruments.



Pro Tip: If you want to organise your soundfont files into instrument types, or to keep a set of files together, etc. then you can create folders within Library/Audio/Sounds/Banks folder and this will be reflected in the Sound Bank list.

UPDATE 28-OCT-2012: I just found this post that links to downloads for "3.5 Gigabytes Of HQ Orchestral SF2" at newgrounds.com. I haven't tried them all yet, but those that I have tried are sounding pretty good. I also discovered freesf2.com which also has some great free SF2 files (although you'll need sfArk).

UPDATE 25-FEB-2014: Blogger's stats tell me that this post is getting quite a bit of traffic and that a chunk of that is coming from thegaragebandguide.com :)