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 @@ Sinatra Framework + + + + <%= yield %> - \ No newline at end of file + diff --git a/app/views/static/index.erb b/app/views/static/index.erb index 622f8bb..df5cfe8 100644 --- a/app/views/static/index.erb +++ b/app/views/static/index.erb @@ -1,2 +1,39 @@ -

Hello World

-

See Me in views/static/index.html.erb

\ No newline at end of file +

Bitly Clone

+ + +

<%= @errors if @errors %>

+ + +
+ + +
+ + + + + + + + + <% @urls.each do |url| %> + + + + + + + + + <% end %> +
Long URLShort URLClick Count
+ + <%= url.long_urls %> + + + + <%= url.short_urls %> + + <%= url.click_count %> +
+
diff --git a/db/migrate/20170726121009_create_urls.rb b/db/migrate/20170726121009_create_urls.rb new file mode 100644 index 0000000..43bedf7 --- /dev/null +++ b/db/migrate/20170726121009_create_urls.rb @@ -0,0 +1,9 @@ +class CreateUrls < ActiveRecord::Migration[5.0] + def change + create_table :urls do |t| #we just created a new table called "urls" + t.string :long_urls #we created a new column of strings called long_urls + t.string :short_urls #we created a new column of strings called short_urls + t.timestamps #we created a new column called t.timestamps which automatically creates two new columns: one is created_at and one called updated_at + end + end +end diff --git a/db/migrate/20170727104024_add_click_count_to_urls.rb b/db/migrate/20170727104024_add_click_count_to_urls.rb new file mode 100644 index 0000000..67981d6 --- /dev/null +++ b/db/migrate/20170727104024_add_click_count_to_urls.rb @@ -0,0 +1,5 @@ +class AddClickCountToUrls < ActiveRecord::Migration[5.0] + def change + add_column :urls, :click_count, :integer, default: 0 + end +end diff --git a/notes.txt b/notes.txt new file mode 100644 index 0000000..54333ea --- /dev/null +++ b/notes.txt @@ -0,0 +1,23 @@ +incorporate bootstrap cdn link so that i can see my shortened link in a nicer table + +1- validations (url, means i cant enter any invalid, i cant enter repetitive link +-if the entry is saved, then good. if the entry is not valid, how should i deal with it? Should i enter an error message manually? or should i just borrow the validation error message from active record +Click count for the link that we shortened + +***************************************** + +to commit and push changes to Heroku +1- make my changes to my local files +2- test it on my 127.0.0.1:9393 or see link below: (http://127.0.0.1:9393) by typing into my terminal : + - $ rake server +3- when its successful or i approve changes: do below steps: + +$ git add . +$ git commit +- (enter commit message e.g. "i changed background color") +- esc +- :wq! + +$ git push (origin/heroku) master + +***************************************** diff --git a/public/css/application.css b/public/css/application.css index e69de29..afb82c5 100644 --- a/public/css/application.css +++ b/public/css/application.css @@ -0,0 +1,54 @@ +body{ + background-color: yellow; +} + +h1 { + color: red; + font-size: 130px; + font-style: italic; + font-family: Chalkduster; + font-weight: 900; + text-align: center; + margin-top: 90px; +} + +form { + margin-top: 1.75cm; + margin-left: 7cm; +} + +input { + border: 9px solid navy; + font-size: 55px; + +} +tr { + color: red; + +} + +th { + color: red; + column-fill: balance; + background-color: blue; + border-style: dotted; + /*border-spacing: 10px;*/ + color: white; +} + +table { + color: red; + background-color: DeepSkyBlue; + border: 20px solid red; + border-collapse: separate; + border-spacing: 0 1em; + /*border-style: groove;*/ + +} + +#shorten-btn { + -webkit-appearance: button; + font-size: 55px; + height: inherit; + color: red; +} diff --git a/public/js/application.js b/public/js/application.js index e69de29..a070134 100644 --- a/public/js/application.js +++ b/public/js/application.js @@ -0,0 +1,21 @@ +$(document).ready(funciton(){ + $("#create_url").on("submit", function(event){ + event.preventDefault() + form_inputs = $(this).serialize() + $.ajax({ + url: "/test", + method: "post", + data: form_inputs, + dataType: "json", + success: function(data){ + $("tbody").append( + "" + + data.long_url + "" + + "" + + data.short_url + + "0" + ) + } + }) + }) +}) diff --git a/public/test.js b/public/test.js new file mode 100644 index 0000000..7618684 --- /dev/null +++ b/public/test.js @@ -0,0 +1,22 @@ +$(document).ready(function(){ + $("#create_url").on("submit", function(event){ + event.preventDefault() + form_inputs = $(this).serialize() + $.ajax({ + url: "/test", + method: "post", + data: form_inputs, + dataType: "json", + success: function(data){ + debugger + $("tbody").append( + "" + + data.long_urls + "" + + "" + + data.short_urls + + "0" + ) + } + }) + }) +})