diff --git a/.byebug_history b/.byebug_history new file mode 100644 index 0000000..6cb0a53 --- /dev/null +++ b/.byebug_history @@ -0,0 +1,5 @@ +c +params +params[:long_url] +params +@url diff --git a/.ruby-version b/.ruby-version deleted file mode 100644 index 197c4d5..0000000 --- a/.ruby-version +++ /dev/null @@ -1 +0,0 @@ -2.4.0 diff --git a/27JulyNotes2.txt b/27JulyNotes2.txt new file mode 100644 index 0000000..6063d15 --- /dev/null +++ b/27JulyNotes2.txt @@ -0,0 +1,8 @@ +AJAX is a developer's dream, because you can: + +-Update a web page without reloading the page +-Request data from a server - after the page has loaded +-Receive data from a server - after the page has loaded +-Send data to a server - in the background + +document.get(elementbyID) diff --git a/Gemfile b/Gemfile index 74d024e..32ad0a5 100644 --- a/Gemfile +++ b/Gemfile @@ -1,7 +1,7 @@ source 'https://rubygems.org' # Ruby Version -# ruby "2.2.1" +ruby "2.4.0" # Adding Sinatra Drivers gem 'sinatra' diff --git a/Gemfile.lock b/Gemfile.lock index 9d709e3..df897ce 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -91,5 +91,8 @@ DEPENDENCIES sinatra-contrib thin +RUBY VERSION + ruby 2.4.0p0 + BUNDLED WITH - 1.15.1 + 1.15.2 diff --git a/app/controllers/static.rb b/app/controllers/static.rb index 33c1897..b722753 100644 --- a/app/controllers/static.rb +++ b/app/controllers/static.rb @@ -1,3 +1,49 @@ +require 'securerandom' + get '/' do + @urls = Url.all.order("created_at DESC") erb :"static/index" -end \ No newline at end of file +end + +post '/shorten' do + @url = Url.new(long_urls: params["long_urls"], short_urls: SecureRandom.hex(4)) + #creates a ew object called @url then using the short_urls: SecureRandom.hex(4) this generates a secure random output that has a lower probability of redundancy. + if @url.save + # p params + # "example" + redirect '/' #redirects the user back to the 'root' or main web page +else #i need my code to rememeber my error variable when its not saved successfully, thats why i load the erb instead of redirecting to "/" because once i do reidrecting to '/' my code will forget the defiend local variable + #this is the error message given from ActiveRecord because it does not pass the validation stated on m odel urb + @errors = @url.errors.full_messages.join(",") +end + @urls = Url.all.order("created_at DESC") + erb :"static/index" +end + +post '/:url_id/vote' do + # whatever you see on the browser is the value, and then the key i set to id by inserting : here + # 1-find out which url i am upvoting + @url = Url.find(params[:url_id]) + + @url.click_count +=1 + @url.save + redirect '/' + +end + +get '/:shortshort' do + #1 look for this url now + #2 increase the lick count because ijust clicked on the short url + #3 i will redirect to the l;ong url (original website) + #look for this url now + @url = Url.find_ny(short_urls: params[:shortshort]) + @url.click_count +=1 + @url.save + rediirect to "#{@url.long_urls}" +end + +post "/test" do + @url = Url.new(long_urls: params[:long_urls], short_urls: SecureRandom.hex(4)) + @url.save + @url.to_json +end diff --git a/app/models/url.rb b/app/models/url.rb new file mode 100644 index 0000000..51781bc --- /dev/null +++ b/app/models/url.rb @@ -0,0 +1,6 @@ +class Url < ActiveRecord::Base + validates :long_urls, uniqueness: true + validates :long_urls, format: {with: (URI::regexp(['http', 'https'])), message: "Entrered URL is Invalid!"} + # This is Sinatra! Remember to create a migration! + #above validatesusing the "validates at lines 2 & 3" +end diff --git a/app/views/layouts/application.erb b/app/views/layouts/application.erb index 4b47fec..f47a657 100644 --- a/app/views/layouts/application.erb +++ b/app/views/layouts/application.erb @@ -1,8 +1,16 @@