Skip to content
This repository has been archived by the owner on Nov 27, 2022. It is now read-only.

Commit

Permalink
Merge pull request #6 from mallowlabs/id/6
Browse files Browse the repository at this point in the history
Blacklist mechanism is needed.
  • Loading branch information
mzp committed Jan 28, 2014
2 parents 4d4c407 + d49286f commit e225e23
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 2 deletions.
6 changes: 4 additions & 2 deletions app/controllers/quotes_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,10 @@ def html

def show
@url = params[:u]
@thumbnail = ThumbnailRule.quote @url
@page = HtmlRule.quote @url
unless Blacklist.include?(@url)
@thumbnail = ThumbnailRule.quote @url
@page = HtmlRule.quote @url
end
render status: 404, text: '404 Not found' unless @page
end

Expand Down
10 changes: 10 additions & 0 deletions app/models/blacklist.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class Blacklist
DEFAULT_LIST = ['localhost', '127.0.0.1']

def self.include?(url)
additional_list = (ENV['BLACKLIST'] || '').split
uri = URI.parse(url)
(DEFAULT_LIST + additional_list).include?(uri.host)
end
end

5 changes: 5 additions & 0 deletions spec/controllers/quotes_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,9 @@
subject { response }
its(:response_code) { should eq 404 }
end
context 'show with blacklisted url' do
before { get :show, :u => 'http://localhost/' }
subject { response }
its(:response_code) { should eq 404 }
end
end
20 changes: 20 additions & 0 deletions spec/models/blacklist_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# -*- coding: utf-8 -*-
require 'spec_helper'

describe Blacklist do
describe "include?" do
context "matched" do
subject { Blacklist.include?('http://localhost/') }
it { should be_true }
end
context "unmatched" do
subject { Blacklist.include?('http://twitter.com/') }
it { should be_false }
end
context "use ENV" do
before { ENV['BLACKLIST'] = 'www.codefirst.org' }
subject { Blacklist.include?('http://www.codefirst.org/') }
it { should be_true }
end
end
end

0 comments on commit e225e23

Please sign in to comment.