Permalink
Browse files

Initial commit of the Gister gem

  • Loading branch information...
0 parents commit a1fd458ed00e0094ddd49f4e7d5c5d73a6af1899 @ericallam ericallam committed Mar 12, 2012
Showing with 712 additions and 0 deletions.
  1. +17 −0 .gitignore
  2. +4 −0 Gemfile
  3. +22 −0 LICENSE
  4. +29 −0 README.md
  5. +2 −0 Rakefile
  6. +23 −0 gister.gemspec
  7. +7 −0 lib/gister.rb
  8. +72 −0 lib/gister/fetcher.rb
  9. +48 −0 lib/gister/middleware.rb
  10. +9 −0 lib/gister/railtie.rb
  11. +3 −0 lib/gister/version.rb
  12. +75 −0 spec/fetcher_spec.rb
  13. +308 −0 spec/fixtures/vcr_cassettes/gister.yml
  14. +10 −0 spec/helper.rb
  15. +83 −0 spec/middleware_spec.rb
@@ -0,0 +1,17 @@
+*.gem
+*.rbc
+.bundle
+.config
+.yardoc
+Gemfile.lock
+InstalledFiles
+_yardoc
+coverage
+doc/
+lib/bundler/man
+pkg
+rdoc
+spec/reports
+test/tmp
+test/version_tmp
+tmp
@@ -0,0 +1,4 @@
+source 'https://rubygems.org'
+
+# Specify your gem's dependencies in gister.gemspec
+gemspec
22 LICENSE
@@ -0,0 +1,22 @@
+Copyright (c) 2012 Eric Allam
+
+MIT License
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,29 @@
+# Gister
+
+TODO: Write a gem description
+
+## Installation
+
+Add this line to your application's Gemfile:
+
+ gem 'gister'
+
+And then execute:
+
+ $ bundle
+
+Or install it yourself as:
+
+ $ gem install gister
+
+## Usage
+
+TODO: Write usage instructions here
+
+## Contributing
+
+1. Fork it
+2. Create your feature branch (`git checkout -b my-new-feature`)
+3. Commit your changes (`git commit -am 'Added some feature'`)
+4. Push to the branch (`git push origin my-new-feature`)
+5. Create new Pull Request
@@ -0,0 +1,2 @@
+#!/usr/bin/env rake
+require "bundler/gem_tasks"
@@ -0,0 +1,23 @@
+# -*- encoding: utf-8 -*-
+require File.expand_path('../lib/gister/version', __FILE__)
+
+Gem::Specification.new do |gem|
+ gem.authors = ["Eric Allam"]
+ gem.email = ["rubymaverick@gmail.com"]
+ gem.description = %q{Provides a Middleware to cache embedded gist content}
+ gem.summary = %q{Provides a Middleware to cache embedded gist content}
+ gem.homepage = ""
+
+ gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
+ gem.files = `git ls-files`.split("\n")
+ gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
+ gem.name = "gister"
+ gem.require_paths = ["lib"]
+ gem.version = Gister::VERSION
+
+ gem.add_development_dependency "rspec"
+ gem.add_development_dependency "vcr", "~> 2.0.0"
+ gem.add_development_dependency "webmock"
+
+ gem.add_runtime_dependency "faraday", "0.6.1"
+end
@@ -0,0 +1,7 @@
+require 'gister/railtie' if defined?(Rails)
+
+module Gister
+ def self.fetcher
+ @fetcher ||= Fetcher.new
+ end
+end
@@ -0,0 +1,72 @@
+require 'faraday'
+
+module Gister
+ class Fetcher
+ ClientError = Class.new(StandardError)
+ GistNotFound = Class.new(ClientError)
+
+ def initialize
+ @store = Hash.new
+ end
+
+ def fetch(key)
+ if result = get(key)
+ result
+ else
+ result = fetch_result(key)
+ set key, result
+ result
+ end
+ end
+
+ def get(key)
+ @store[key]
+ end
+
+ def set(key, value)
+ @store[key] = value
+ end
+
+ private
+
+ def fetch_result(key)
+ path, params = parse_uri(key)
+
+ wrap_exceptions do
+ response = connection.get(path) do |req|
+ req.params = params
+ end
+ response.body
+ end
+ end
+
+ def wrap_exceptions
+ yield
+ rescue Faraday::Error::ResourceNotFound
+ raise GistNotFound.new($!)
+ rescue Faraday::Error::ClientError
+ raise ClientError.new($1)
+ end
+
+ def connection
+ @connection ||= Faraday.new(url: "https://gist.github.com") do |builder|
+ builder.use Faraday::Request::JSON
+ builder.use Faraday::Response::RaiseError
+ builder.use Faraday::Adapter::NetHttp
+ end
+ end
+
+ def parse_uri(uri)
+ uri = URI.parse(uri)
+ [uri.path, parse_params(uri.query)]
+ end
+
+ def parse_params(query_string)
+ query_string.split("&").inject(Hash.new) do |hash, kv|
+ key, value = kv.split("=")
+ hash[key] = value
+ hash
+ end
+ end
+ end
+end
@@ -0,0 +1,48 @@
+require 'open-uri'
+
+require_relative 'fetcher'
+
+module Gister
+ class Middleware
+ def initialize(app, fetcher=Fetcher.new)
+ @app = app
+ @fetcher = fetcher
+ end
+
+ def call(env)
+ request = Rack::Request.new env
+ if request.path =~ /^\/gist\/[0-9]+.json/
+ respond_to_failures do
+ [200, {'Content-Type' => 'application/javascript'}, [get_body_content(request)]]
+ end
+ else
+ @app.call(env)
+ end
+ end
+
+ private
+
+ def respond_to_failures
+ yield
+ rescue Fetcher::ClientError
+ [404, {}, [""]]
+ end
+
+ def get_body_content(request)
+ path = request.path.gsub("/gist/", '')
+ path = "https://gist.github.com/#{path}?file=#{request.params["file"]}"
+
+ response = fetch_by_path path
+ wrap_in_jsonp(request.params["callback"], response)
+ end
+
+ def wrap_in_jsonp(callback, response)
+ "#{callback}(#{response})"
+ end
+
+ def fetch_by_path(path)
+ @fetcher.fetch path
+ end
+
+ end
+end
@@ -0,0 +1,9 @@
+require_relative 'middleware'
+
+module Gister
+ class Railtie < Rails::Railtie
+ initializer "gister.configure_rails_initialization" do |app|
+ app.middleware.insert_before ActionDispatch::Cookies, Gister::Middleware, Gister.fetcher
+ end
+ end
+end
@@ -0,0 +1,3 @@
+module Gister
+ VERSION = "0.0.1"
+end
@@ -0,0 +1,75 @@
+require 'gister/fetcher'
+require 'helper'
+
+describe Gister::Fetcher do
+ subject { described_class.new }
+ let(:key) {
+ "https://gist.github.com/1111.json?file=app.js"
+ }
+
+ describe "set" do
+ it "should store the object based on it's key" do
+ subject.set(key, "hello")
+ subject.get(key).should == "hello"
+ end
+ end
+
+ describe "fetch" do
+ use_vcr_cassette "gister", record: :new_episodes
+
+ context "when the gist is already in the cache" do
+ let(:cached_response) {
+ "jQuery({})"
+ }
+
+ before do
+ subject.set(key, cached_response)
+ end
+
+ it "should return the response from the cache" do
+ subject.fetch(key).should == cached_response
+ end
+ end
+
+ context "when the gist is not already in the cache" do
+
+ context "and there is a successful response from gist" do
+ let(:key) {
+ "https://gist.github.com/1996296.json?file=challenge-1-2.js"
+ }
+
+ it "should return the response gist" do
+ subject.fetch(key).should include("Anatomy of Backbone 1-2")
+ end
+
+ it "should store the response from gist into the cache" do
+ subject.fetch(key)
+ subject.get(key).should_not be_nil
+ end
+ end
+
+ context "and gist responds with a 404" do
+ let(:key) {
+ "https://gist.github.com/199629612981921.json?file=challenge-1-2.js"
+ }
+
+ it "should raise a GistNotFound error" do
+ expect { subject.fetch(key) }.to raise_error(described_class::GistNotFound)
+ end
+ end
+
+ context "and gist responds with a 500" do
+ let(:key) {
+ "https://gist.github.com/client_error122.json?file=challenge-1-2.js"
+ }
+
+ it "should raise a ClientError error" do
+ expect { subject.fetch(key) }.to raise_error(described_class::ClientError)
+ end
+ end
+
+ end
+ end
+
+
+end
Oops, something went wrong.

0 comments on commit a1fd458

Please sign in to comment.