Skip to content

Commit

Permalink
Merge pull request hotsh#451 from clnclarinet/author_domain
Browse files Browse the repository at this point in the history
Author domain
  • Loading branch information
carols10cents committed Jan 21, 2012
2 parents e31c2d4 + abe021c commit b013d40
Show file tree
Hide file tree
Showing 8 changed files with 86 additions and 16 deletions.
1 change: 1 addition & 0 deletions app/controllers/salmon_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ def feeds
author.name = atom_entry.author.portable_contacts.display_name
author.username = atom_entry.author.name
author.remote_url = atom_entry.author.uri
author.domain = atom_entry.author.uri
author.email = atom_entry.author.email
author.email = nil if author.email == ""
author.bio = atom_entry.author.portable_contacts.note
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/sessions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def create
u = User.first :username => params[:username]
if u.nil?
# Grab the domain for this author from the request url
params[:domain] = root_path()[/\:\/\/(.*?)\/$/, 1]
params[:domain] = root_url

author = Author.new params

Expand Down
19 changes: 12 additions & 7 deletions app/models/author.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@ class Author
# of it is val*idated right now. Fun. Then again, not all of it is neccesary.
key :username, String

# This contains the domain that the author's feed originates (nil for local)
# This contains the domain that the author's feed originates.
key :domain, String

# We can get the domain from the remote_url
before_save :get_domain
validates_presence_of :domain

# Normalize the domain so we can use them the same way
before_save :normalize_domain

# The Author has a profile and with that various entries
key :name, String
Expand Down Expand Up @@ -184,10 +186,13 @@ def to_atom
author
end

def get_domain
if self.remote_url
self.domain = remote_url[/\:\/\/(.*?)\//, 1]
end
def normalize_domain
norm = self.domain.gsub(/^.*:\/\//, "")
norm = norm.gsub(/^www./, "")
norm = norm.gsub(/\/.*$/, "")
norm = norm.gsub(/\?.*$/, "")
norm = norm.gsub(/#.*$/, "")
self.domain = norm
end

def self.search(params = {})
Expand Down
4 changes: 1 addition & 3 deletions app/models/authorization.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,7 @@ def self.create_from_hash!(hash, base_uri, user = nil)

# If there isn't a user, create a user and author.
if user.nil?
domain = base_uri[/\:\/\/(.*?)\//, 1]

author = Author.create_from_hash!(hsh, domain)
author = Author.create_from_hash!(hsh, base_uri)
user = User.create(
:author => author,
:username => author.username
Expand Down
1 change: 1 addition & 0 deletions app/models/feed.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ def populate(xrd = nil)
:username => a.name,
:email => a.email,
:remote_url => a.uri,
:domain => a.uri,
:salmon_url => ostatus_feed.salmon,
:bio => a.portable_contacts.note,
:image_url => avatar_url)
Expand Down
9 changes: 9 additions & 0 deletions lib/tasks/fixdb.rake
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
#rake tasks to update stored data
namespace :fixdb do
desc "Set nil Author domains and normalize all domains"
task :author_domains => :environment do
Author.all.each do |a|
a.domain = a.domain || "rstat.us" # Set any nil domains to us
a.normalize_domain
a.save!
end
end

desc "Set nil Author usernames"
task :set_nil_usernames => :environment do
messed_up_authors = Author.where(:username => nil)
Expand Down
56 changes: 56 additions & 0 deletions test/models/author_domain_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
require_relative '../test_helper'

describe "Author domain storage" do
include TestHelper

def root_url # Stub for the root route
"this_sites_domain.com"
end

it "stores a domain if passed in" do
a = Author.create!(:domain => "some.domain")
a.domain.must_equal("some.domain")
end

it "throws an error if the domain is not passed in" do
lambda {
a = Author.create!
}.must_raise (MongoMapper::DocumentNotValid)
end

it "strips http://" do
a = Author.create!(:domain => "http://some.domain")
a.domain.must_equal("some.domain")
end

it "strips http://www." do
a = Author.create!(:domain => "http://www.some.domain")
a.domain.must_equal("some.domain")
end

it "strips www." do
a = Author.create!(:domain => "www.some.domain")
a.domain.must_equal("some.domain")
end

it "strips trailing /" do
a = Author.create!(:domain => "some.domain/")
a.domain.must_equal("some.domain")
end

# Who knows if these last few are possible or likely, but may as well, eh?
it "strips trailing ?" do
a = Author.create!(:domain => "some.domain?foo=bar")
a.domain.must_equal("some.domain")
end

it "strips trailing #" do
a = Author.create!(:domain => "some.domain#blah")
a.domain.must_equal("some.domain")
end

it "strips trailing path" do
a = Author.create!(:domain => "some.domain/one/two?three=four#five")
a.domain.must_equal("some.domain")
end
end
10 changes: 5 additions & 5 deletions test/models/author_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
describe Author do
include TestHelper
before do
@author = Factory.build :author, :username => "james", :email => nil, :image_url => nil, :created_at => 3.days.ago
@author = Factory :author, :username => "james", :email => nil, :image_url => nil, :created_at => 3.days.ago
end

it "creates an author from a hash" do
Expand Down Expand Up @@ -49,7 +49,7 @@

describe "Author#search" do
before do
Factory.build :author, :username => "hipster", :email => nil, :image_url => nil
Factory :author, :username => "hipster", :email => nil, :image_url => nil
end

describe "search param" do
Expand All @@ -74,9 +74,9 @@

describe "letter param" do
before do
Factory.build :author, :username => "9000OVER", :email => nil, :image_url => nil
Factory.build :author, :username => "_______a", :email => nil, :image_url => nil
Factory.build :author, :username => "hacker", :email => nil, :image_url => nil
Factory :author, :username => "9000OVER", :email => nil, :image_url => nil
Factory :author, :username => "_______a", :email => nil, :image_url => nil
Factory :author, :username => "hacker", :email => nil, :image_url => nil
end

it "uses param[:letter] to filter by first letter" do
Expand Down

0 comments on commit b013d40

Please sign in to comment.