Skip to content

Commit

Permalink
Addressabler, first version. Query still causing issues.
Browse files Browse the repository at this point in the history
  • Loading branch information
Flip Sasser committed Jan 2, 2011
0 parents commit 2ffc180
Show file tree
Hide file tree
Showing 11 changed files with 8,244 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pkg/*
4 changes: 4 additions & 0 deletions README.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Addressabler
=

**Addressabler** extends the Addressable::URI class by adding TLD parsing, domain and subdomain parsing, and query modification.
18 changes: 18 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
require 'rake'

require "jeweler"
Jeweler::Tasks.new do |gemspec|
gemspec.name = "Addressabler"
gemspec.summary = "An Addressable::URI extension adding support for TLDs and query part editing"
gemspec.files = Dir["{lib}/**/*", "README.markdown"]
gemspec.description = %{
Addressabler extends the Addressable::URI class to provide better information about, and manipulation of, URI strings. It adds a tld method, a domain method,
and a subdomain method. It also allows users to easily modify the URL's query values as a hash.
}
gemspec.email = "flip@x451.com"
gemspec.homepage = "http://github.com/flipsasser/addressabler"
gemspec.authors = ["Flip Sasser"]
gemspec.test_files = Dir["{spec}/**/*"]
gemspec.add_development_dependency 'rspec', '>= 2.0'
gemspec.add_dependency 'addressable', '>= 2.2.2'
end
1 change: 1 addition & 0 deletions VERSION
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0.0.1
4 changes: 4 additions & 0 deletions autotest/discover.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
require 'autotest/fsevent'
require 'autotest/growl'

Autotest.add_discovery { "rspec2" }
98 changes: 98 additions & 0 deletions lib/addressabler.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
require 'rubygems'
require 'addressable/uri'
require 'addressabler/query'

module Addressabler
module ClassMethods
def parse_tld(host)
host = host.to_s.split('.')
tlds = []
sub_hash = Addressabler::TLDS
while sub_hash = sub_hash[tld = host.pop]
tlds.unshift(tld)
if sub_hash.has_key? '*'
tlds.unshift(host.pop)
end
end
tlds.join('.')
end
end

module InstanceMethods
def self.included(base)
base.class_eval do
alias_method :original_query, :query
alias_method :query, :query_hash
alias_method :original_query=, :query=
alias_method :query=, :new_query=
alias_method :original_query_values=, :query_values=
alias_method :query_values=, :new_query_values=
end
end

def domain
@domain ||= parse_domain_parts[:domain]
end

def new_query=(new_query)
self.original_query = new_query
@_original_query = true
@query_hash = Addressabler::Query[query_values(:notation => :flat) || {}]
@_original_query = false
end

def query_hash
@_original_query ? original_query : @query_hash ||= Addressabler::Query.new
end

def new_query_values=(new_query_values)
self.original_query_values = new_query_values
@_original_query = true
@query_hash = Addressabler::Query[query_values(:notation => :flat) || {}]
@_original_query = false
end

def subdomain
@subdomain ||= parse_domain_parts[:subdomain]
end

def tld
@tld ||= parse_domain_parts[:tld]
end

private
def parse_domain_parts
return @_domain_parts if defined? @_domain_parts
tld = self.class.parse_tld(host)
begin
subdomain_parts = host.gsub(/\.#{tld}$/, '').split('.')
rescue
raise host.inspect
end
@_domain_parts = {
:domain => subdomain_parts.pop,
:subdomain => subdomain_parts.join('.'),
:tld => tld
}
end
end

# Thanks Domainatrix for the parsing logic!
tlds = {}
File.readlines(File.join(File.dirname(__FILE__), 'tlds')).each do |line|
unless !line.strip! == ''
parts = line.split(".").reverse
sub_hash = tlds
parts.each do |part|
sub_hash = (sub_hash[part] ||= {})
end
end
end
TLDS = tlds
end

Addressable::URI.class_eval do
alias :original_query :query
end
Addressable::URI.extend Addressabler::ClassMethods
Addressable::URI.send :include, Addressabler::InstanceMethods
37 changes: 37 additions & 0 deletions lib/addressabler/query.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
module Addressabler
class Query < ::Hash
STRING_METHODS = "".methods.map(&:to_s)

def ==(value)
case value
when NilClass
empty?
when String
puts "#{to_s.inspect} == #{value.inspect} (#{to_s == value})"
to_s == value
else
super
end
end

def to_s
if empty?
''
else
uri = Addressable::URI.new
uri.query_values = self
uri.original_query
end
end
alias :to_str :to_s

private
def method_missing(method, *args)
if STRING_METHODS.include? method.to_s
to_s.send(method, *args)
else
super
end
end
end
end
Loading

0 comments on commit 2ffc180

Please sign in to comment.