Law of Demeter

TL;DR

In OO software, do not assume knowledge of anything else.

Longer explanation

The Law of Demeter (LoD) is the principle of least knowledge for developing software.

Roughly speaking other ways of expressing this are:

  • software should be loosely coupled
  • principle of encapsulation
  • principle of “information hiding”

For modern OO languages, when using dot notation simply use 1 dot. i.e. something like this would be forbidden:

dog.legs.walk

as you’re assuming knowledge.

Much longer explanation here: http://en.wikipedia.org/wiki/Law_of_Demeter

Cross-Origin Resource Sharing

Cross-Origin Resource Sharing, or CORS, defines a way to enable client-side cross-origin requests. So, if this API is used on http://siteA.org then a resource on http://siteB.org could opt in to this( e.g. by specifying Access-Control-Allow-Origin: http://siteA.org as a response header).

The problem it aims to solve is that AJAX calls that use XMLHttpRequest to do cross-domain requests (e.g. Site A wants to access a script on Site B) are forbidden by web browsers (see Same-origin policy: http://en.wikipedia.org/wiki/Same-origin_policy).

Note: WebSockets are not subject to the same-origin policy.

CORS is a W3 Recommendation released on 16th January 2014. See http://www.w3.org/TR/access-control/

Alternatives to CORS are:

  • setting the document.domain property
  • Cross-document messaging – e.g. calling the postMessage() method on a Window object
  • JSONP

http://en.wikipedia.org/wiki/Same-origin_policy#Relaxing_the_same-origin_policy

 

Browser support for CORS:

  • >= Firefox 3.5
  • >= Safari 4
  • >= Chrome 3
  • >= IE10 (IE8+ has partial support). i.e. IE9 in Compatibility View would not support CORS
  • >= Opera 12

Sender Signatures – DKIM and SPF

Sender Signatures are used to avoid spam.

If you’re using a service like Outlook or Gmail you won’t even be aware of this.

However, if you send an email programmatically then you need to say who the Sender is. You usually do that using a Reply-To header. Unfortunately spammers have being doing this forever so, to avoid an email being labelled as spam, you’ll need to sign your email.

There are two main methods of creating Sender Signatures: DKIM and SPF.

DKIM (DomainKeys Identified Mail) has emerged from IETF (one of the Internet governing bodies) specifications and uses a signer’s public key which is published in the DNS. More here: http://en.wikipedia.org/wiki/DomainKeys_Identified_Mail

SPF (Sender Policy Framework) adds the list of authorized sending hosts for a domain in a TXT record. More here: http://en.wikipedia.org/wiki/Sender_Policy_Framework

For other methods see: http://en.wikipedia.org/wiki/Email_authentication

Apache: Client denied by server configuration

This error means that the access to the directory on the hard disk was denied by an Apache configuration. It could be that access was denied due to an explicit deny directive or due to an attempt to access a folder that is outside of the DocumentRoot. It can also happen when you are proxying and there’s no access configured for the proxied location. And it is the default response to a PUT request.

To fix this problem, look at the line in your ErrorLog, to find out which folder it is trying to access. 

If a <Directory> block already exists for that folder, make sure it is set to allow access as necessary. If not, add a <Directory> block to your Apache configuration file, allowing access as required. See the example below for folder /usr/local/awstats/htdocs.

<Directory /usr/local/awstats/htdocs>

  Order allow,deny

  Allow from all

</Directory>

More: 

http://wiki.apache.org/httpd/ClientDeniedByServerConfiguration

Using Capistrano to deploy your Rails app

1. if you haven’t got Rails installed locally then do it now. 

As of this blog post Rails 4, which you should be using, is pre-release so: 

gem install rails –pre

but drop the –pre when Rails 4 is no longer pre-release.

–pre means install the prerelease rails gem.

http://stackoverflow.com/questions/4041902/what-does-pre-do-in-gem-install-rails-pre

2. create a simple Rails app

rails new myapp

3. upload your Rails app to github

– create a git rep locally if it doesn’t exist

git init; git add .; git commit -am “first commit”

– create a github repo at 

https://github.com/new 

This should tell you what you need to do next however in short:

git remote add origin git@github.com:you/your_repo.git

git push -u origin master

4. capify with

capify .

(and remember to push to git with:

git add .; git commit -am “with capistrano”

git push -u origin master)

Note: as you’re running Rails you should put your Capistrano config information in config/deploy.rb. So, your Capfile should look like this:

load ‘config/deploy’

and your config/deploy.rb file should have all the Capfile details like 

set :git_username, “your_git_username”

set :repository,  “git@github.com:you/your_repo.git”

etc…

5. Use SSH agent forwarding initially

https://help.github.com/articles/managing-deploy-keys

Specifically, make sure you’ve got it set right locally:

https://help.github.com/articles/using-ssh-agent-forwarding

6. Read also this post if you’re having problems with your ssh key:

e.g. 

default_run_options[:pty] = true

helps debug. And add your host to your known_hosts file by doing ssh git@github.com and entering yes (it doesn’t matter that the login won’t succeed).

http://stackoverflow.com/questions/7863070/capistrano-deploy-host-key-verification-failed

7. Then use:

cap deploy:setup 

and

cap deploy:cold

For more details of what deploy:cold does see:

https://github.com/capistrano/capistrano/blob/master/lib/capistrano/recipes/deploy.rb#L200-L203

Notes

Some issues:

I found Capistrano wasn’t creating directories in the releases directory with this option:

set :deploy_via, :remote_cache

http://stackoverflow.com/questions/8246007/capistrano-will-not-create-releases

Also:

To automatically run ‘bundle install’ on the server, add this to your deploy.rb file:

require ‘bundler/capistrano’

http://gembundler.com/v1.3/deploying.html

8. If using a Dreamhost VPS you may need to unset the DreamHost Managed Apache setting in VPS > Configure Server.

http://wiki.dreamhost.com/VPS#httpd.conf

More on deploying Rails including details on Passenger (aka mod_rails), JRuby, Capistrano and Hosting:

http://rubyonrails.org/deploy

Using Capistrano to deploy your Rails app

1. if you haven’t got Rails installed locally then do it now. 

As of this blog post Rails 4, which you should be using, is pre-release so: 

gem install rails –pre

but drop the –pre when Rails 4 is no longer pre-release.

–pre means install the prerelease rails gem.

http://stackoverflow.com/questions/4041902/what-does-pre-do-in-gem-install-rails-pre

2. create a simple Rails app

rails new myapp

3. upload your Rails app to github

– create a git rep locally if it doesn’t exist

git init; git add .; git commit -am “first commit”

– create a github repo at 

https://github.com/new 

This should tell you what you need to do next however in short:

git remote add origin git@github.com:you/your_repo.git

git push -u origin master

4. capify with

capify .

(and remember to push to git with:

git add .; git commit -am “with capistrano”

git push -u origin master)

Note: as you’re running Rails you should put your Capistrano config information in config/deploy.rb. So, your Capfile should look like this:

load ‘config/deploy’

and your config/deploy.rb file should have all the Capfile details like 

set :git_username, “your_git_username”

set :repository,  “git@github.com:you/your_repo.git”

etc…

5. Use SSH agent forwarding initially

https://help.github.com/articles/managing-deploy-keys

Specifically, make sure you’ve got it set right locally:

https://help.github.com/articles/using-ssh-agent-forwarding

6. Read also this post if you’re having problems with your ssh key:

e.g. 

default_run_options[:pty] = true

helps debug. And add your host to your known_hosts file by doing ssh git@github.com and entering yes (it doesn’t matter that the login won’t succeed).

http://stackoverflow.com/questions/7863070/capistrano-deploy-host-key-verification-failed

and

https://help.github.com/articles/deploying-with-capistrano

7. Then use:

cap deploy:setup 

and

cap deploy:cold

For more details of what deploy:cold does see:

https://github.com/capistrano/capistrano/blob/master/lib/capistrano/recipes/deploy.rb#L200-L203

Notes

Some issues:

I found Capistrano wasn’t creating directories in the releases directory with this option:

set :deploy_via, :remote_cache

http://stackoverflow.com/questions/8246007/capistrano-will-not-create-releases

Also:

To automatically run ‘bundle install’ on the server, add this to your deploy.rb file:

require ‘bundler/capistrano’

http://gembundler.com/v1.3/deploying.html

8. If using a Dreamhost VPS you may need to unset the DreamHost Managed Apache setting in VPS > Configure Server.

http://wiki.dreamhost.com/VPS#httpd.conf

More on deploying Rails including details on Passenger (aka mod_rails), JRuby, Capistrano and Hosting:

http://rubyonrails.org/deploy