Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Begin work for standalone api server

  • Loading branch information...
commit 94c39cd4c7a013edc2bdbed75da0196cd4bc7c26 1 parent 8745d87
@evanphx evanphx authored
View
2  Gemfile
@@ -12,9 +12,11 @@ gem 'gravtastic'
gem 'high_voltage'
gem 'jquery-rails'
gem 'mail'
+gem 'memcached'
gem 'multi_json'
gem 'paul_revere'
gem 'pg'
+gem 'puma'
gem 'rack'
gem 'rack-maintenance', :require => 'rack/maintenance'
gem 'rdoc'
View
5 Gemfile.lock
@@ -119,6 +119,7 @@ GEM
i18n (>= 0.4.0)
mime-types (~> 1.16)
treetop (~> 1.4.8)
+ memcached (1.4.3)
method_source (0.8)
mime-types (1.19)
multi_json (1.3.6)
@@ -142,6 +143,8 @@ GEM
coderay (~> 1.0.5)
method_source (~> 0.8)
slop (~> 3.3.1)
+ puma (1.6.3)
+ rack (~> 1.2)
rack (1.4.1)
rack-cache (1.2)
rack (>= 0.4)
@@ -249,6 +252,7 @@ DEPENDENCIES
jruby-openssl
launchy
mail
+ memcached
multi_json
newrelic-redis
newrelic_rpm
@@ -256,6 +260,7 @@ DEPENDENCIES
paul_revere
pg
pry
+ puma
rack
rack-maintenance
rack-test
View
78 app/middleware/api_v1_depedencies.rb
@@ -0,0 +1,78 @@
+require 'memcached'
+
+class V1MarshaledDepedencies
+
+ LIMIT = 250
+
+ BadRequest = [404, {}, ["Go away"]]
+ TooMany = [413, {},
+ ["Too many gems to resolve, please request less than #{LIMIT}"]]
+
+ def data_for(name, ary, cache)
+ gem = Rubygem.find_by_name(name)
+ raise "Unknown gem - #{name}" unless gem
+
+ these = []
+
+ gem.versions.each do |ver|
+ deps = ver.dependencies.find_all { |d| d.scope == "runtime" }
+
+ data = {
+ :name => name,
+ :number => ver.number,
+ :platform => ver.platform,
+ :dependencies => deps.map { |d| d.requirements.split(" ", 2) }
+ }
+ these << data
+ ary << data
+ end
+
+ if cache
+ cache.set "gem.#{name}", Marshal.dump(these)
+ end
+
+ ary
+ end
+
+ CACHE = Memcached.new("localhost:11211")
+
+ def call(env)
+ request = Rack::Request.new(env)
+
+ return BadRequest unless request.path == "/api/v1/dependencies"
+
+ gems = request.params['gems']
+
+ return BadRequest unless gems
+
+ gems = gems.split(",")
+
+ return TooMany if gems.size > LIMIT
+
+ ary = []
+ cache = CACHE.clone
+
+ gems.each do |g|
+ begin
+ data = cache.get "gem.#{g}"
+ ary += Marshal.load(data)
+ rescue Memcached::NotFound
+ begin
+ data_for g, ary, cache
+ rescue
+ return BadRequest
+ end
+ rescue Memcached::ServerIsMarkedDead
+ begin
+ data_for g, ary, nil
+ rescue
+ return BadRequest
+ end
+ end
+ end
+
+ body = Marshal.dump ary
+
+ [200, {}, [body]]
+ end
+end
View
58 test/unit/dependencies_middleware_test.rb
@@ -0,0 +1,58 @@
+require 'test_helper'
+
+class DependenciesMiddlewareTest < ActiveSupport::TestCase
+ def app
+ V1MarshaledDepedencies.new
+ end
+
+ context "on GET to index" do
+ setup do
+ @versions = [create(:version), create(:version)].each do |version|
+ create(:dependency, :version => version)
+ end
+ get "/api/v1/dependencies",
+ :gems => @versions.map(&:rubygem).map(&:name).join(',')
+ end
+
+ should "return Marshalled array of hashes" do
+ assert_equal 200, last_response.status
+
+ array = Marshal.load(last_response.body)
+ assert_kind_of Array, array
+ array.each {|hash| assert_kind_of Hash, hash }
+ end
+
+ should "return correct Marshalled values" do
+ assert_equal 200, last_response.status
+
+ array = Marshal.load(last_response.body)
+ array.each_with_index do |hash, idx|
+ assert_equal @versions[idx].rubygem.name, hash[:name]
+ assert_equal @versions[idx].number, hash[:number]
+ assert_kind_of Array, hash[:dependencies]
+ assert_equal 1, hash[:dependencies].size
+ end
+ end
+ end
+
+ context "on GET to index with too many gems" do
+ setup do
+ gems = Array.new Dependency::LIMIT + 1, 'gem'
+ get "/api/v1/dependencies", :gems => gems.join(',')
+ end
+
+ should "see too many gems text" do
+ assert_match %r!Too many gems to resolve!, last_response.body
+ end
+ end
+
+ context "on GET for an unknown gem" do
+ setup do
+ get "/api/v1/dependencies", :gems => "not-there"
+ end
+
+ should "return a 404 status" do
+ assert_equal 404, last_response.status
+ end
+ end
+end
View
BIN  vendor/cache/memcached-1.4.3.gem
Binary file not shown
View
BIN  vendor/cache/puma-1.6.3.gem
Binary file not shown
Please sign in to comment.
Something went wrong with that request. Please try again.