Skip to content

Commit

Permalink
multiple domain url creation logic added
Browse files Browse the repository at this point in the history
  • Loading branch information
kevivmatrix committed Mar 11, 2016
1 parent 1633f6e commit 1122f61
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 9 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,5 @@ end
group :development, :test do
gem 'byebug'
gem 'rspec-rails', '~> 3.4.2'
gem "better_errors"
end
6 changes: 6 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ GEM
tzinfo (~> 1.1)
addressable (2.3.6)
arel (5.0.1.20140414130214)
better_errors (2.1.1)
coderay (>= 1.0.0)
erubis (>= 2.6.6)
rack (>= 0.9.0)
builder (3.2.2)
byebug (3.1.2)
columnize (~> 0.8)
Expand All @@ -40,6 +44,7 @@ GEM
rack (>= 1.0.0)
rack-test (>= 0.5.4)
xpath (~> 2.0)
coderay (1.1.0)
coffee-rails (4.0.1)
coffee-script (>= 2.2.0)
railties (>= 4.0.0, < 5.0)
Expand Down Expand Up @@ -227,6 +232,7 @@ PLATFORMS
ruby

DEPENDENCIES
better_errors
byebug
coffee-rails (~> 4.0.0)
cucumber-rails
Expand Down
44 changes: 37 additions & 7 deletions app/controllers/urls_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,45 @@ def edit
end

def create
urls = create_shortenURL(params[:url], params[:unique_key])

@url = urls[:short_url]
url = urls[:url]
url_to_shorten = params[:url]
is_url_working = URLValidator.new(url: url_to_shorten).works?
domains = params[:domain_name]
unique_key = params[:unique_key]

urls_with_errors = domains.map do |domain|
url = Shortener::ShortenedUrl.new url_params
if is_url_working
if unique_key.present?
if unique_key =~ /\A[a-zA-Z0-9]+\Z/
if Shortener::ShortenedUrl.where(domain_name: domain).
where("lower(unique_key) = ?", unique_key.downcase).exists?
url.errors[:base] << "That short code already exists for #{domain}."
end
else
url.errors[:base] << 'Short codes can only include numbers and letters.'
end
end
url
else
url.errors[:base] << 'That URL doesn\'t seem to work.'
end
end.select do |url|
url.errors.present?
end

respond_to do |format|
if url.works? and @url.errors.messages.empty?
if urls_with_errors.empty?
urls = domains.map do |domain|
url = Shortener::ShortenedUrl.create url: url_to_shorten
url.unique_key = unique_key if unique_key.present?
url.domain_name = domain
url.save
url
end
format.html { redirect_to root_url }
format.json { render :show, status: :created, location: @url }
format.json { render :index, status: :created, location: urls }
else
@url = urls_with_errors.first
format.html { render :new }
format.json { render json: @url.errors, status: :unprocessable_entity }
end
Expand Down Expand Up @@ -105,7 +134,8 @@ def url_params
def load_domains
@current_domain = session[:domain_name]
@request_domain = request.server_name.downcase
@domains = Shortener::ShortenedUrl.select(:domain_name).distinct(:domain_name).map(&:domain_name)
@domains = Shortener::ShortenedUrl.select(:domain_name).where("domain_name is not null").
distinct(:domain_name).map(&:domain_name)
@domains << @request_domain unless @domains.include?(@request_domain)
end

Expand Down
14 changes: 14 additions & 0 deletions features/step_definitions/url_steps.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,24 @@
expect(Shortener::ShortenedUrl.count).to eq count.to_i
end

Then(/^I should have Shorted URL with "(.*)\/(.*)" as url and "([^\"]*)" as target$/) do |domain, unique_key, url|
expect(
Shortener::ShortenedUrl.find_by(
domain_name: domain,
unique_key: unique_key,
url: url
)
).to_not be nil
end

Then(/^I should have (\d+) Shorted URL with domain name as "([^"]*)"$/) do |count, domain_name|
expect(Shortener::ShortenedUrl.where(domain_name: domain_name).count).to eq count.to_i
end

Then /^I should see "([^\"]*)" domain checkbox as checked$/ do |domain|
expect(response).to have_selector("input", id: "domain_name_input_#{domain}", checked: "checked")
end

Then /^I should see "([^\"]*)" domain checkbox as unchecked$/ do |domain|
expect(response).to_not have_selector("input", id: "domain_name_input_#{domain}", checked: "checked")
end
49 changes: 47 additions & 2 deletions features/url_management.feature
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Feature: URL Management
and see list of previously generated short URLs

@admin
Scenario: Admin logs into the dashboard and creates a short URL
Scenario: Dashboard Admin shortens URLs for more than one domain at once
Given Our host is "hkk.sn"
And I am an authorized user with credentials as name and password
And there is a shortened URL http://omnia.ws/blah1 that goes to http://example1.com/
Expand All @@ -17,7 +17,52 @@ Feature: URL Management
When I enter "http://external.com" into the input box as the URL in new url form
And I enter "blah" into the input box as the short code in new url form
And I submit the new url form
Then I should have 2 Shorted URL with domain name as "hkk.sn"
Then I should have 1 Shorted URL with domain name as "hkk.sn"
And I should have 2 Shorted URL with domain name as "omnia.ws"
And I should have Shorted URL with "hkk.sn/blah" as url and "http://external.com" as target
And I should have Shorted URL with "omnia.ws/blah" as url and "http://external.com" as target
When I go to homepage
Then I should see "hkk.sn/blah"
And I should see "omnia.ws/blah"

@admin
Scenario: Dashboard Admin shortens URLs for the selected domain by default
Given Our host is "hkk.sn"
And I am an authorized user with credentials as name and password
And there is a shortened URL http://omnia.ws/blah1 that goes to http://example1.com/
When I go to homepage and provide name and password
And I select omnia.ws in domains dropdown
Then I should see "Custom branded short URLs"
And I should see "Create new short URL"
When I click "Create new short URL"
Then I should see "New URL"
And I should see "hkk.sn" domain checkbox as unchecked
And I should see "omnia.ws" domain checkbox as checked
When I enter "http://external.com" into the input box as the URL in new url form
And I enter "blah" into the input box as the short code in new url form
And I submit the new url form
Then I should have 0 Shorted URL with domain name as "hkk.sn"
And I should have 2 Shorted URL with domain name as "omnia.ws"
And I should have Shorted URL with "omnia.ws/blah" as url and "http://external.com" as target
When I go to homepage
Then I should not see "hkk.sn/blah"
And I should see "omnia.ws/blah"

@admin
Scenario: Dashboard Admin fails to shortens URLs and is provided proper error messages
Given Our host is "hkk.sn"
And I am an authorized user with credentials as name and password
And there is a shortened URL http://omnia.ws/blah that goes to http://external.com/
When I go to homepage and provide name and password
Then I should see "Custom branded short URLs"
And I should see "Create new short URL"
When I click "Create new short URL"
Then I should see "New URL"
And I should see "hkk.sn" domain checkbox as checked
And I should see "omnia.ws" domain checkbox as checked
When I enter "http://external.com" into the input box as the URL in new url form
And I enter "blah" into the input box as the short code in new url form
And I submit the new url form
Then I should have 0 Shorted URL with domain name as "hkk.sn"
And I should have 1 Shorted URL with domain name as "omnia.ws"
And I should see "That short code already exists for omnia.ws."

0 comments on commit 1122f61

Please sign in to comment.