PayPal Treated Me Right

I recently tried integrating PayPal’s APIs with a site I work on and I jumped the gun before reading (I know, rookie mistake). I had integrated many other payment processing systems and I assumed wrongly that PayPal would accept test credit card numbers for testing. They flagged this as fraud, which I suppose is valid, and locked my account, limiting my ability to finalize my site.

They gave me a list of things to do to reinstate my account, and I did them, then they got more aggressive and asked for more validation, specifically the billing details that matched my credit card number—a test number that I obviously couldn’t provide. I explained this to them and they reinstated my account. I had little hope up to this point due to PayPal’s bad PR lately, but they solved my problem with a minimum of fuss, though it did take a while. Score one for the good guys.

Git back to where you used to be

I’m still not as versed in Git as I used to be. In various situations, I find myself looking at things and saying, well, I’ve backed up my own files, I know where I used to be. How do I just wipe everything clean and start over?

Obviously there’s always delete the dir and re-clone the files. With multi-gigabyte repos over a cell connection this isn’t the best option. Here’s a list of situations and their remedies as I’ve found them. All commands are run in the main directory of your project.

I’ve got changes, but I don’t want to commit them:

#just go ahead and delete everything you know about. 
git reset --hard

I’ve got changes and new files to add, but I just want to wipe everything out:

#hey git, there's all sorts of things in here to pay attention to
git add .
#just go ahead and delete everything you know about. 
git reset --hard

My commits have diverged, but I just wanna be where the origin is, not where I am:

#just go ahead and go back to where you were. 
git reset --hard @{upstream}

I’ve got commits staged, but I just want to wipe everything out:

#wipe out the last commit. Repeat as necessary
git reset --soft "HEAD^"

nginx 403 on osx

I’ve got nginx up and running (trying to replace my mamp stack with it) and I kept running into a 403 error regardless of configuration. Turns out this was due to a config error. In OSX, you need to specify a user:group in the nginx config file that match a user that has access to the files you want to serve.

For example, my computer’s user is mengel and my group is staff. My config file is as follows:

user mengel staff;
 
events {
    worker_connections 1024;
}
 
http {
    server {
        listen 80;
        server_name localhost;
        root    /Users/mengel/Documents/Workspace/localhost;
        location /{ 
           autoindex on;
           autoindex_exact_size off;
        }
    }
}

Multiple Email Addresses with a Default in Git

Someone pointed out to me that my commits to github were showing up using my work email address (and therefore my commits weren’t connected to the proper account). I do personal and professional work on the same laptop and I find myself wanting to commit one git repo via one email address and one repo to another.

The first idea I had was to set the global email address to the default and then set individual email addresses on a per-project basis. Git always uses the global email address if one’s set. Thanks, git. Anyway, I have a super-hacky workaround for this based partially on my PS1 code for my bash profile. The first thing you have to do is open your ~/.gitconfig file and remove the user.email line. This was originally set using git config --global user.email your@email.com, but we’ll be handling this more manually:

parse_git(){
	gb=$(git branch 2> /dev/null | sed -e '/^[^*]/d' -e "s/* \(.*\)/\1/")
	if [[ -z $gb ]]
	then echo -e ""
	else
		ge=$(git config user.email)
		if [[ -z $ge ]]
		then 
			git config user.email "morgan@gallerus.com"
			echo -e "morgan@gallerus.com:"$gb'\n\r'
		else echo -e $ge":"$gb'\n\r'
		fi
	fi
}
PS1="\[\033[1;33m\]\$(parse_git)\[\033[0;0m\]\[\033[1;32m\]\w\[\033[0;0m\]\n>"

This first line takes the output from git branch and finds a line with a * in it indicating that branch is selected. If it finds one, it re-searches for the name specifically. If either of these is not found, an empty string is returned.

in bash, [[ -z ]] indicates empty. If we find that the output of the branch script is empty, we stop checking. Otherwise, we set the local email address to the default.

Finally, we output the various bits for use in our PS1 shell prompt, but you don’t have to do that if you don’t want to.

The final output looks something like this (only more colorful)

morgan@gallerus.com:feedback
~/Documents/Workspace/gallerus
>

which is now how I like to set up my bash environment.

Selenium ChromeDriver used to work

I used to use this code:

from selenium import webdriver
desktop_browser_list = [webdriver.Chrome("./drivers/chromedriver")]

But then Chrome apparently got fancier and doesn’t work with the old chromedriver. So I downloaded the new version, but it threw various errors.
So, now I have to switch to

from selenium import webdriver
options = webdriver.ChromeOptions()
options.binary_location = "/Applications/Google Chrome.app/Contents/MacOS/Google Chrome"
desktop_browser_list = [webdriver.Chrome(executable_path="./drivers/chromedriver", chrome_options=options)]

Tammer Saleh : The Number One Trait of a Great Developer

An excellent explanation about why simpler code and code choices are better.

 

When I look around at other companies hiring Ruby on Rails developers, I see them focusing on three major traits: Super-smart; Large community following; Deep Ruby knowledge. They’re all wrong. While these are great aspects in moderation, they all miss the number one quality of a fantastic developer: Judgement.

Tammer Saleh : The Number One Trait of a Great Developer.

 

Setting Size on wx.TextEntryDialog in Python WXWidgets

The process of showing a dialog is easy, but making a text prompt for the user that’s more than one line and in which you’re able to see more than one line is a more tricky thing. Here’s the syntax that worked for me.

dlg_msg = 'Hey user! Enter your text below:'
dlg_title = 'This is a title'
dlg_default_style = "{}"
dlg = wx.TextEntryDialog(parent=self.view, message=dlg_msg, caption=dlg_title, defaultValue=dlg_default_style, style=wx.OK | wx.CANCEL | wx.TE_MULTILINE)
dlg.SetValue("default value\non two lines")
if dlg.ShowModal() == wx.ID_OK:
    print(dlg.GetValue())

The vars above the declaration are just for readability. Please note that dlg_default_style is a string, not a dict.

Please note that the default style, though empty, is necessary for the multiline entry to be picked up (at least on OSX)

Understanding Express Middleware in Node.js

Middleware is one of those interesting concepts that once you get it is very, very helpful. I learned this from making the autominify middleware for express.

The concept goes like this:
Express is initialized. Each time you add an app.use call, it appends it to a list of things to try every time someone requests a file. When a file is requested, each of these is called in turn and, if they don’t care about the request, they call a next() function and Express moves onto the next app.use call. If nothing claims the request, a 404 is called.

The syntax is pretty simple, but there’s some useful things I’ve done in autominify that you might want to use, as well.

This is the simplesty example of this I could think of. An inline function only executes if “helloworld” is somewhere in your url. This could be as simple as
http://localhost:3000?helloworld=true
and it would produce “helloworld” as the page.
If you didn’t have helloworld in your url, it would process as normal.

app.use(function(req, res, next){
	if(req.url.indexOf('helloworld') != -1){
		res.writeHead(200, {'Content-Type': 'text/plain'});
		res.end('helloworld');
	}
	else
	{
		next();
	}
});

Remember that app.use executes in order, so if something else handles the request before you do, you’ll never see your code work.

If you’re looking to make a module, here’s the basics you might need.

//in app.js
var myModule = require('myModule');
app.use(myModule(true));
//file called myModule.js
module.exports = function (goodbye) {
	var mainText = "hello world";
	if(goodbye)
	{
		mainText = "goodbye world"
	}
 
	return function (req, res, next) {
		if(req.url.indexOf('helloworld') != -1){
			res.writeHead(200, {'Content-Type': 'text/plain'});
			res.end(mainText);
		}
		else
		{
			next();
		}
	}
}

You’ve passed in true as an init param. This would mean that all sessions that are handled with this middleware (any request with helloworld in the url) would receive “goodbye world” as the text on the page.

PHP, Python, GoLang, and Node.js all served by Apache port 80

I dabble in node.js, php, python, and more. Often, these services will produce webservers, and I’d like to try them out in my browser. I also would like to be able to test redirects for things like oauth. In order to get a proper passback from somewhere like Facebook, I need to simulate a full website url without port. It also would be nice to do this without having to shut down one, adjust settings, and restart the other.

The concept:
/etc/hosts or Windows/system32/drivers/hosts will handle incoming request handling to a url

  • Apache handles all requests from port 80
  • Apache uses VirtualHosts to redirect traffic from port 80 to various ports
  • Node/Python/Go/Etc. Will handle requests from specific ports.

An example:
/etc/hosts:

127.0.0.1   local.simplestexplanation.com

This tells your computer that every program that requests data from local.simplestexplanation.com should be forwarded to your computer. This is true for all protocols, http, https, ftp, etc.

httpd.conf:

<VirtualHost *:80>
    ServerName local.simplestexplanation.com
    ProxyPass / http://127.0.0.1:3000/ retry=0
</VirtualHost>

We’ve told apache to create a VirtualHost on port 80. The servername matches the entry in /etc/hosts. The proxypass indicates to where we should forward this request, in this case, to local port 3000. retry=0 tells apache not to cache the results and to always try to serve the page (even if node was down last time)

app.js:

https.createServer(options, function (req, res) {
  res.writeHead(200);
  res.end("hello world\n");
}).listen(3000);

This is a basic node server using port 3000. If we receive a request to http://local.simplestexplanation.com we should see it handled by node.

Similar servers can be set up in Python, GoLang, etc. And as long as they don’t steal port 80 from Apache, they can handle requests as normal. I’ve found this very useful.