Fork me on GitHub

Updates from December, 2009 Toggle Comment Threads | Keyboard Shortcuts

  • britg 10:46 am on December 29, 2009 Permalink | Log in to leave a Comment
    Tags: , , , , sinatra   

    Cross Origin Resource Sharing with Sinatra 

    It’s no lie that I think highly of the potential of Cross Origin Resource Sharing. One of the great things about it is that it doesn’t take much re-wiring of existing server (or client-side) apps to start working cross domain.

    Enabling your server app is as simple as including a few extra headers when you detect a cross origin request. How do you know it’s a cross origin request? You’ll see the Origin: header — all CORS requests will have it. From there, response headers depend on the specifics of the request, but I won’t go over those here — check out the Mozilla Developer Center treatment for in-depth information.

    I’ve been working with Sinatra a lot lately, so I put together an extension for Sinatra that makes enabling Cross Origin requests even easier.

    sudo gem install sinatra-cross_origin

    There are two ways to use the extension: globally or per-route.

    Global

    For when you want to share all your endpoints cross-domain.

    
    require 'sinatra/base'
    require 'sinatra/cross_origin'
    
    class MyApp < Sinatra::Base
      register Sinatra::CrossOrigin
    
      enable cross_origin
    
      get '/' do
        "This is available to cross domain javascript requests automatically"
      end
    end
    

    Per Route

    For when you want to share only some of your routes cross-domain.

    
    require 'sinatra/base'
    require 'sinatra/cross_origin'
    
    class MyApp < Sinatra::Base
      register Sinatra::CrossOrigin
    
      get '/' do
        cross_origin
        "This is available to cross domain javascript requests"
      end
    end
    

    Configuration

    You can mix and match app-wide config and request specific config.

    
    require 'sinatra/base'
    require 'sinatra/cross_origin'
    
    class MyApp < Sinatra::Base
      register Sinatra::CrossOrigin
    
      configure do
        # Comma separate list of remote hosts that are allowed.
        # :any will allow any host
        set :allow_origin, :any
    
        # HTTP methods allowed
        set :allow_methods, [:get, :post]
    
        # Allow cookies to be sent with the requests
        set :allow_credentials, true
      end
    
      get '/' do
        # Only available to GET requests originating from
        # http://example.com.  No cookies allowed.
        cross_origin :allow_origin => 'http://example.com',
          :allow_methods => [:get],
          :allow_credentials => false
        "This is available to cross domain javascripts"
      end
    end
    

    Grab the source at Github: britg/sinatra-cross_origin.

     
    • escort services 9:19 am on March 1, 2011 Permalink

      Cross-origin resource sharing Sending messages is not the only solution for sharing information between different websites. The cross-origin resource sharing API loosens the controls over AJAX calls to anywhere but the home domain. …

    • Chris Castle 4:52 am on July 4, 2011 Permalink

      Hey Brit-  Thanks for putting this together.  I’m trying to make an HTTPS cross-domain request with Basic Auth.  I’m using Sinatra (obviously) on the server and jquery on the client.  I’m getting a 401 Unauthorized response from the server.  In looking at the Request headers I don’t see the browser sending the ‘Authorization’ header.  Here are the request and response headers.  Any idea what’s wrong?  If I hit the Request URL by itself in a browser, the auth box pops up, I enter my credentials, and everything works great — but not when it’s requested via javascript…

      Request URL:https://my.site.com/ec2/describe?env=dev&instanceId=i-b285d4dfRequest Method:GETStatus Code:401 UnauthorizedRequest HeadersAccept:*/*Accept-Charset:ISO-8859-1,utf-8;q=0.7,*;q=0.3Accept-Encoding:gzip,deflate,sdchAccept-Language:en-US,en;q=0.8Connection:keep-aliveHost:my.site.comOrigin:http://localhost:4567Referer:http://localhost:4567/index.htmlUser-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_8) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/13.0.782.41 Safari/535.1Query String Parametersenv:devinstanceId:i-b285d4dfResponse HeadersAccess-Control-Allow-Credentials:trueAccess-Control-Allow-Headers:AuthorizationAccess-Control-Allow-Methods:GET, POST, UPDATE, DELETE, OPTIONS, HEADAccess-Control-Allow-Origin:http://localhost:4567Access-Control-Max-Age:1728000Connection:keep-aliveContent-Length:15Content-Type:text/html;charset=utf-8Server:thin 1.2.11 codename Bat-Shit CrazyWWW-Authenticate:Basic realm=”Restricted Area”

  • britg 12:11 am on December 17, 2009 Permalink | Log in to leave a Comment
    Tags: , ,   

    Scriptstack – Organize and Share Javascripts 

    scriptstackI’ve been hacking on a small project in my free time that I uploaded today: scriptstack.

    What is scriptstack?

    Well, if you’re like me you probably have 4 or 5 javascript files you include in just about every new project or site your start. jQuery. Tooltips. Lightbox. qTip. Or, you just put together a nice portfolio site for a client and you want to save that specific set of javascript plugins for the next time you do something similar.

    Scriptstack aims to be an easy and social way to organize your “stacks” of scripts. You can:

    • Upload scripts.
    • Click and drag them into the order they should be loaded in the browser.
    • Tag them with a few keywords to make them indexable for future search.
    • Download the concatenated stack in minified or raw format.

    That’s about it for now, haha. Release early, release often, right? I should note that there’s no permissions on the stacks. If you create one, it’s editable by anyone right now. I plan to add User accounts and ownership soon.

    Warning: the site probably only works in Firefox.

    Under the hood

    I took this opportunity to expand my horizons as far as the technology under the hood. I’ll go in-depth on these as I continue to develop, but a quick rundown of the tech stack (pun intended but probably shouldn’t be):

    I also open sourced all the code that runs the site here incase you are interested in what poorly written Ruby looks like.

    If you happen to check it out, let me know what you think! And as I said, I will expand on different parts of it here in the near future, so stay tuned.

     
  • britg 11:02 am on December 10, 2009 Permalink | Log in to leave a Comment
    Tags: , , ,   

    On Stack Overflow Careers 

    If you’re not familiar with Stack Overflow Careers, check out Introducing Stack Overflow Careers and Stack Overflow Careers: Amplifying Your Awesome.

    In short, it aims to build a for-pay CV hosting and searching service on top of the success of Stack Overflow.

    stackoverflow I have mixed feelings about Stack Overflow Careers.

    No, I’m not questioning whether or not they should have built it, or whether or not they should charge money for it. I have no problem with Joel or Jeff using their interweb fame to make a buck like this guy seems to (Clarification: William’s beef isn’t with the for-pay aspect, but with the underlying system itself. See first comment.). If they want to, they should.

    America++!

    My quandary is whether I should make an effort to participate in Stack Overflow and Careers. I like the site and use it daily, but I don’t actually participate. I am a consumer, not a contributor. Should I eschew my natural introverted tendencies and force myself to contribute?

    Maybe

    The Stack Overflow group of programmers seem to “get it.” On the whole they are smart, pragmatic, and can communicate in complete sentences (skills listed in reverse order of importance).

    As a freelance programmer, I would like to be attributed with those qualities when looking for work. That’s the true value in associating a job search with Stack Overflow and is the reason why they can charge programmers to list their CV. If you’re recognized as a member of that community, you’re much more valuable to job searchers that are Stack Overflow-aware.

    Maybe there’s some value in participating in the site and quantifying my association with the Stack Overflow type of programmer?

    Nah

    But, ultimately what am I trying to accomplish? Sure, I want to be known as a competent and gets-stuff-done kind of programmer. Stack Overflow is an avenue to quantify that I have these skills; it isn’t the method by which these skills are obtained.

    Joel Spolsky himself said,

    The great software developers, indeed, the best people in every field, are quite simply never on the market.

    He goes on to explain that if you’re a great developer, you’ll be recognized as such through your work.

    I think this says it all. I shouldn’t force myself to participate in a contrived system attempting to quantify competence. Don’t get me wrong, if you enjoy interacting and contributing to Stack Overflow, more power to you! But for me, if I’m doing the right things to further myself as a developer, the rest will take care of itself. More do-ey, less talk-ey.

     
    • wshields 11:30 am on December 10, 2009 Permalink

      Just to clarify, speaking as the author of the linked post: I don't have a problem with Jeff, Joel or anyone making a buck.

      It just crosses a line when a fatally flawed (imho) model is being sold on false promises.

      BTW, the theme for your blog is fantastic. WordPress? What theme?

    • britg 11:46 am on December 10, 2009 Permalink

      Thanks for clarifying. I'll update the post to point to your clarification.

      Glad you like the theme, but I can't really take any credit for it, haha. It's from Woothemes.com — Bueno: http://www.woothemes.com/2009/11/bueno/

c
compose new post
j
next post/next comment
k
previous post/previous comment
r
reply
e
edit
o
show/hide comments
t
go to top
l
go to login
h
show/hide help
esc
cancel