the most awesome guy ever.

The Blog of Darryl E. Clarke

  Random musings from a jaded coder who just needs a hug.

Archive for the ‘Coding’ Category

A Quickie on Using Android Studio on Ubuntu

Friday, May 17th, 2013

The original post below applies to old versions. For 0.4.x plus, you don’t need to do this. You just need to have a proper Oracle JRE/JDK installed system wide which you can do with this:

sudo add-apt-repository ppa:webupd8team/java
sudo apt-get update && sudo apt-get install oracle-jdk7-installer

With the recent preview release of Android Studio it’s important to know that it won’t work properly on Ubuntu with the OpenJDK that’s available.

Fortunately, it’s easy to get it going.

  1. Download the official Oracle JDK
  2. Unpack it (I put mine in ~/SDK/jdk1.7.0_21/ )
  3. Point your JAVA_HOME to this.
    ~/android-studio/bin$ export JAVA_HOME=~/SDK/jdk1.7.0_21/
  4. Run the android studio startup script.
    ~/android-studio/bin$ ./studio.sh
  5. Make awesome android apps.

Have Fun!

Tags: , , ,
Posted in Coding

Allowing the Facebook Debugger Through .htaccess

Thursday, November 1st, 2012

Here’s a short story; When I develop Facebook web apps, I do it under a password protected development site. Facebook hates this. It complains that it can’t reach urls, it can’t get meta data, it can’t do this, it can’t do that. The downside to not having a password is the fact that anybody can hit the site. (sandboxing is almost useless, these days.)

So, the quick solution: Allow Facebook to hit it, but only via their external meta data scraper.

A quick edit (well, not so quick, it was something obscure.) of my .htaccess rules, and voila! Facebook can debug and people still can’t hit it (easily)

SetEnvIf User-Agent ^facebookexternalhit.*$ Facebook=1

AuthType Basic
AuthName "Art & Science DEV Server"
AuthUserFile /home/dclarke/www/dev/.htpasswd
Require valid-user

order allow,deny
Allow from env=Facebook
Satisfy Any

First, set an environment variable based on if it is the Facebook user agent. Then, allow access. The key here is the ‘satisfy any’ line, which means you can get in if you have a user and password, or that environment flag is set. The downside is now you all know you can just set your user agent to Facebook and get access to my dev sites. 😉

Tags: , ,
Posted in Coding

Experiment: The Viewport Viewer

Saturday, May 5th, 2012

As a little experiment, while I redesign my own site, I decided to create a ‘Viewport Viewer

It’s a little tool that’ll load up a site in an iframe and let you re-size the iframe to some specifics, like an iPhone, or iPad in portrait mode. Go play with it and see.

It’s pretty straight forward: enter your URL, then toggle between sizes as you see fit. If your site is responsive it should fit in the iframe with out any issues.

It’s by no means meant to replace actual hardware testing with your sites, but it is quite handy during the initial phases of your responsive web development.

There are some downsides to it, however. One that I can think of is that it does not utilize ‘min-device-width‘ or ‘max-device-width‘, or any other ‘device‘ related queries. Because, well, it’s all in a browser and not on a device.

Anyway, there’s the tool, hopefully it’s of some use to you. If not, oh well. It’s been useful to me.

Tags: , , ,
Posted in Coding

Queueing With Zend Queue and MemcacheQ

Thursday, November 18th, 2010

I was bored last night so I thought I’d enhance one of my applications with a little bit of Queueing. (And for the record both Queueing and Queuing are valid spellings depending on your locale)

The concept is fairly straight forward. Normally when a web application runs, it runs in a linear pattern. It starts, it does stuff, it does more stuff, then it finishes with a web page. That’s pretty much it. The problem is, the user has to wait for it to do stuff and more stuff. And when they have to wait for more and more stuff, it gets slow. If some of that stuff can be offloaded because it has no affect on what the user sees right now – why not? That’s where a Queue comes in to play. Send a command to a queue that something else can process for you.

So enter Zend Queue. A feature of Zend Framework that will let you use various queue systems to your advantage. I chose MemcacheQ because it was simple to install and I knew that I’d need not do anything to my configurations to utilize it. MemcacheQ is a light-weight, fast, persistant queueing system (It’ll remember what it has when it gets shut down) based on Memcache’s protocol. So accessing the data in the queue is a piece of cake.

I tried using Zend_Queue_Adapter_Memcacheq in Zend Framework 1.10.8 – but it failed. It doesn’t seem to work properly with MemcacheQ 0.2.0 which is the latest version. That lead me to write my own little adapter – based on the existing one – to fix the incompatibilities. I also added automatic serialization to the messages sent to the queue. This allows me to send useful items, instead of just strings. I implemented the queue system in a way that I can now do queued inserts and updates on database rows wherever I see the need.  It’s also setup in a way that if sending a message to the queue fails, it will do the desired action right away (slower for the user, but no loss of data.)  After that I was set.

I send my messages to my queue as described in Zend Framework’s documentation ($queue->send($bundle)); My bundle is a small array containing a few useful items. One: a class name, two: a method to call, three: parameters for the method. And that’s it. Now I’m queueing magical commands.  I need to process the queue.

I then wrote a queue processor. This processor is a very small command line tool that utilizes my existing Zend Framework applications configuration – so it has all the access it needs to the application database and code base. The queue processor pulls in a small set of bundles from the queue – executes them – and waits for a short period of time and then processes more.  In the event that a command fails, the queue processor will re-queue the item and wait to try again.  This is to prevent data loss.  If for some reason the database is unavailable the updates and inserts will persist in the queue until they can be executed properly.

I hope I’ve explained it in enough detail that you can understand the process. If not, leave a comment and I’ll try and clarify it. I’m not posting any code just yet because it’s not “pretty.”

So how well did it do? Judge for yourself. You can tell when I enabled it.

Tags: , , , ,
Posted in Coding, PHP, Zend Framework

Tell Your Redirector How to Do It’s Job

Sunday, October 17th, 2010

There’s a big difference between a redirect that uses a 301 and a 302 code on a website. If you don’t know the difference, you should find out.
301 says “This stuff has moved permanently and you shouldn’t come here anymore.”
302 says “You just need to go here, but this place will still answer you later.”

The appropriate use isn’t too complicated if you understand those two little statements. Within a web application there are many redirects that take place. If you use the 301 in these situations web browsers might get confused and your application won’t work properly.

By default the Zend Framework action helper aptly called “redirector” defaults to using 302’s; which is fine for most cases. But if you happen to be in a refactoring mode and rearranging your code and url structure it’s super important to use 302’s or your search engine rankings can fall right off the table.

Telling the _redirect helper to use a 301 code is really quite easy:

$this->_helper->redirector->setCode(301);
$this->_redirect('/newplace');

Unfortunately it’s a bit strange, because at first you think you can just use ‘$this->_redirect(‘destination’, code); but you can’t and that sucks.  If you have multiple case redirects and some might be 301’s or 302’s you should make sure you set the code back before redirecting.

Tags: , , , , ,
Posted in Coding, PHP, Zend Framework

The Quickest Way To Tweet via Zend Framework’s Twitter Service

Sunday, September 26th, 2010

This is a quick and dirty method.  It assumes a couple of things.

  1. You’ve already created your ‘app’ on twitter.
  2. You’ve already gotten your personal OAuth tokens from twitter.

First you need to setup your tokens;

$token = new Zend_Oauth_Token_Access();
$token->setToken("YOUR_PERSONAL_TOKEN")
      ->setTokenSecret("YOUR_PERSONAL_SECRET");

Second, you need to setup the Zend_Service_Twitter using your access token as well as the application tokens provided by twitter.

$options = array(
 'accessToken'    => $token,
 'callbackUrl'    => 'http://' .$_SERVER['HTTP_HOST']. '/',
 'consumerKey'    => "YOUR_APPLICATION_KEY",
 'consumerSecret' => "YOUR_APPLICATION_SECRET");

 $twitter = new Zend_Service_Twitter($options);
 $response = $twitter->status->update($message);

And that’s it.

If you want to do other things like authenticate multiple users, you’ll have to do a few other steps. Twitter was nice enough to make it “easy” for a single user to create their own app and post without any goofy intermediate authentication steps.

Tags: , ,
Posted in Coding, PHP, Zend Framework