From 0f88947279a3fdf1db5fa2ec62dd7d8853bbccfe Mon Sep 17 00:00:00 2001 From: Sam Saffron Date: Wed, 6 Feb 2013 16:22:11 +1100 Subject: [PATCH] fix onebox for your own site --- Gemfile.lock | 8 +++---- app/models/site_setting.rb | 5 +++++ config/locales/en.yml | 1 + lib/discourse.rb | 8 +++++-- lib/oneboxer.rb | 1 + lib/oneboxer/base.rb | 1 - lib/oneboxer/base_onebox.rb | 4 ++-- lib/oneboxer/discourse_onebox.rb | 5 +++-- spec/components/oneboxer_spec.rb | 37 +++++++++++++++++++++++++++++++- 9 files changed, 58 insertions(+), 12 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index 2025f9bee8e..15a5bbebb93 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -84,7 +84,7 @@ GEM activesupport (= 3.2.11) arel (~> 3.0.2) tzinfo (~> 0.3.29) - activerecord-postgres-hstore (0.7.0) + activerecord-postgres-hstore (0.7.1) rails rake activeresource (3.2.11) @@ -100,9 +100,9 @@ GEM arel (3.0.2) barber (0.2.0) execjs - better_errors (0.3.2) + better_errors (0.5.0) coderay (>= 1.0.0) - erubis (>= 2.7.0) + erubis (>= 2.6.6) binding_of_caller (0.6.8) bourne (1.1.2) mocha (= 0.10.5) @@ -158,7 +158,7 @@ GEM pry (>= 0.9.10) terminal-table (>= 1.4.3) thor (>= 0.14.6) - guard-jasmine (1.12.1) + guard-jasmine (1.12.2) childprocess guard (>= 1.1.0) multi_json diff --git a/app/models/site_setting.rb b/app/models/site_setting.rb index c89f930f34f..eeec027e9c4 100644 --- a/app/models/site_setting.rb +++ b/app/models/site_setting.rb @@ -31,11 +31,16 @@ class SiteSetting < ActiveRecord::Base client_setting(:flush_timings_secs, 5) + # settings only available server side setting(:auto_track_topics_after, 60000) setting(:long_polling_interval, 15000) setting(:flags_required_to_hide_post, 3) setting(:cooldown_minutes_after_hiding_posts, 10) + + # used mainly for dev, force hostname for Discourse.base_url + # You would usually use multisite for this + setting(:force_hostname, '') setting(:port, Rails.env.development? ? 3000 : '') setting(:enable_private_messages, true) setting(:use_ssl, false) diff --git a/config/locales/en.yml b/config/locales/en.yml index 31974f7cfa3..43049288062 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -256,6 +256,7 @@ en: system_username: "Username that sends system messages" send_welcome_message: "Do new users get a welcome private message?" port: "If you'd like to specify a port in the URL. Useful in development mode. Leave blank for none." + force_hostname: "If you'd like to specify a hostname in the URL. Useful in development mode. Leave blank for none." invite_expiry_days: "How long (in days) that invite keys are valid." diff --git a/lib/discourse.rb b/lib/discourse.rb index cc25ccc2bcb..1893fa56a03 100644 --- a/lib/discourse.rb +++ b/lib/discourse.rb @@ -21,8 +21,12 @@ module Discourse def self.base_url protocol = "http" protocol = "https" if SiteSetting.use_ssl? - result = "#{protocol}://#{current_hostname}" - result << ":#{SiteSetting.port}" if SiteSetting.port.present? + if SiteSetting.force_hostname.present? + result = "#{protocol}://#{SiteSetting.force_hostname}" + else + result = "#{protocol}://#{current_hostname}" + end + result << ":#{SiteSetting.port}" if SiteSetting.port.present? && SiteSetting.port.to_i > 0 result end diff --git a/lib/oneboxer.rb b/lib/oneboxer.rb index d2070ff89b3..4775e13be90 100644 --- a/lib/oneboxer.rb +++ b/lib/oneboxer.rb @@ -20,6 +20,7 @@ module Oneboxer # Return a oneboxer for a given URL def self.onebox_for_url(url) matchers.each do |regexp, oneboxer| + regexp = regexp.call if regexp.class == Proc return oneboxer.new(url) if url =~ regexp end nil diff --git a/lib/oneboxer/base.rb b/lib/oneboxer/base.rb index e35e33d3c13..fa0891e2310 100644 --- a/lib/oneboxer/base.rb +++ b/lib/oneboxer/base.rb @@ -19,7 +19,6 @@ module Oneboxer result['description'] = node['content'] if node end - result end end diff --git a/lib/oneboxer/base_onebox.rb b/lib/oneboxer/base_onebox.rb index 5f5f7acea77..fd8f2fce43c 100644 --- a/lib/oneboxer/base_onebox.rb +++ b/lib/oneboxer/base_onebox.rb @@ -8,8 +8,8 @@ module Oneboxer attr_accessor :regexp attr_accessor :favicon_file - def matcher(regexp) - self.regexp = regexp + def matcher(regexp=nil,&blk) + self.regexp = regexp || blk end def favicon(favicon_file) diff --git a/lib/oneboxer/discourse_onebox.rb b/lib/oneboxer/discourse_onebox.rb index 0c9fe34f484..a1e8c4aa6f5 100644 --- a/lib/oneboxer/discourse_onebox.rb +++ b/lib/oneboxer/discourse_onebox.rb @@ -5,8 +5,9 @@ module Oneboxer class DiscourseOnebox < BaseOnebox include ActionView::Helpers::DateHelper - # TODO: we need to remove these hardcoded urls ASAP - matcher /^https?\:\/\/(dev.discourse.org|localhost\:3000|l.discourse|discuss.emberjs.com)\/.*$/ + matcher do + Regexp.new "^#{Discourse.base_url.gsub(".","\\.")}.*$", true + end def onebox uri = URI::parse(@url) diff --git a/spec/components/oneboxer_spec.rb b/spec/components/oneboxer_spec.rb index 7ce5a44df0c..51a3815cf48 100644 --- a/spec/components/oneboxer_spec.rb +++ b/spec/components/oneboxer_spec.rb @@ -1,6 +1,40 @@ require 'spec_helper' require 'oneboxer' +describe "Dynamic Oneboxer" do + class DummyDynamicOnebox < Oneboxer::BaseOnebox + matcher do + /^https?:\/\/dummy2.localhost/ + end + + def onebox + "dummy2!" + end + end + + before do + Oneboxer.add_onebox DummyDynamicOnebox + @dummy_onebox_url = "http://dummy2.localhost/dummy-object" + end + + context 'find onebox for url' do + + it 'returns blank with an unknown url' do + Oneboxer.onebox_for_url('http://asdfasdfasdfasdf.asdf').should be_blank + end + + it 'returns something when matched' do + Oneboxer.onebox_for_url(@dummy_onebox_url).should be_present + end + + it 'returns an instance of our class when matched' do + Oneboxer.onebox_for_url(@dummy_onebox_url).kind_of?(DummyDynamicOnebox).should be_true + end + + end + +end + describe Oneboxer do # A class to help us test @@ -12,6 +46,7 @@ describe Oneboxer do end end + before do Oneboxer.add_onebox DummyOnebox @dummy_onebox_url = "http://dummy.localhost/dummy-object" @@ -129,4 +164,4 @@ describe Oneboxer do end - \ No newline at end of file +