diff options
| author | Teddy Wing | 2013-04-07 00:39:40 -0400 |
|---|---|---|
| committer | Teddy Wing | 2013-04-07 00:39:40 -0400 |
| commit | bfa6c83b8c9cfca37eb5c1ac07f2098b898c771b (patch) | |
| tree | 0e46a3c8a44071ee2e65b9748326c39c6a3e5eaa | |
| download | clips-bfa6c83b8c9cfca37eb5c1ac07f2098b898c771b.tar.bz2 | |
Initial commit v0.0.1
* List clips.
* Route to post a new clip URL.
* Bookmarlet to post a clip.
| -rw-r--r-- | .gitignore | 7 | ||||
| -rw-r--r-- | Gemfile | 12 | ||||
| -rw-r--r-- | Gemfile.lock | 40 | ||||
| -rw-r--r-- | assets/bookmarklet.js | 16 | ||||
| -rw-r--r-- | clip.rb | 63 | ||||
| -rw-r--r-- | config.ru | 11 | ||||
| -rw-r--r-- | views/clips.json.erb | 1 | ||||
| -rw-r--r-- | views/index.html.erb | 13 | ||||
| -rw-r--r-- | views/layout.html.erb | 23 |
9 files changed, 186 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..786b3f9 --- /dev/null +++ b/.gitignore @@ -0,0 +1,7 @@ +# Bundle +.bundle/ +vendor/ + +# Database +config/database.yml +db/*.sqlite3
\ No newline at end of file @@ -0,0 +1,12 @@ +source 'https://rubygems.org' + +gem 'rack' +gem 'camping' + +gem 'sqlite3' +gem 'activerecord' +gem 'tilt' + +group :development do + gem 'rerun' +end
\ No newline at end of file diff --git a/Gemfile.lock b/Gemfile.lock new file mode 100644 index 0000000..9db3645 --- /dev/null +++ b/Gemfile.lock @@ -0,0 +1,40 @@ +GEM + remote: https://rubygems.org/ + specs: + activemodel (3.2.13) + activesupport (= 3.2.13) + builder (~> 3.0.0) + activerecord (3.2.13) + activemodel (= 3.2.13) + activesupport (= 3.2.13) + arel (~> 3.0.2) + tzinfo (~> 0.3.29) + activesupport (3.2.13) + i18n (= 0.6.1) + multi_json (~> 1.0) + arel (3.0.2) + builder (3.0.4) + camping (2.1.532) + mab (>= 0.0.3) + rack (>= 1.0) + i18n (0.6.1) + listen (0.7.3) + mab (0.0.3) + multi_json (1.7.2) + rack (1.5.2) + rerun (0.8.0) + listen + sqlite3 (1.3.7) + tilt (1.3.6) + tzinfo (0.3.37) + +PLATFORMS + ruby + +DEPENDENCIES + activerecord + camping + rack + rerun + sqlite3 + tilt diff --git a/assets/bookmarklet.js b/assets/bookmarklet.js new file mode 100644 index 0000000..ebed77a --- /dev/null +++ b/assets/bookmarklet.js @@ -0,0 +1,16 @@ +(function() { + var xhr = new XMLHttpRequest(); + var params = 'url=' + window.location.href; + xhr.open('POST', 'http://localhost:9292/clips', true); + xhr.onreadystatechange = function() { + if (this.readyState == 4) { + if (xhr.status == 200) { + if (xhr.responseText.success) { + // window.alert('Clipped'); + } + } + } + }; + xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); + xhr.send(params); +})()
\ No newline at end of file @@ -0,0 +1,63 @@ +# Clip app + +Camping.goes :Clip + +module Clip + set :views, File.dirname(__FILE__) + '/views' +end + +module Clip::Controllers + class Index < R '/' + def get + @clips = Clip.all + + render :index + end + end + + class Clips < R '/clips' + def post + @headers['Access-Control-Allow-Origin'] = '*' + + @url_regex = /\b((?:https?:\/\/|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}\/?)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s\`!()\[\]{};:\'\".,<>?]))/i + + @result = {} + + if @input.url and @url_regex.match(@input.url) and not Clip.find_by_url(@input.url) + Clip.create(:url => @input.url) + + @result[:success] = true + else + @result[:success] = false + end + + @result = @result.to_json + + render :clips, :layout => false + end + end +end + + +module Clip::Models + class Clip < Base + end + + class BasicFields < V 1.0 + def self.up + create_table Clip.table_name do |t| + t.text :url + t.timestamps + end + end + + def self.down + drop_table Clip.table_name + end + end +end + + +def Clip.create + Clip::Models.create_schema +end diff --git a/config.ru b/config.ru new file mode 100644 index 0000000..9bd3e16 --- /dev/null +++ b/config.ru @@ -0,0 +1,11 @@ +%w(rack active_record camping camping/session camping/reloader).each { |r| require r } + +require './clip.rb' +run Clip + + +dbconfig = YAML.load(File.read('config/database.yml')) +environment = ENV['DATABASE_URL'] ? 'production' : 'development' +Camping::Models::Base.establish_connection dbconfig[environment] + +Clip.create if Clip.respond_to? :create diff --git a/views/clips.json.erb b/views/clips.json.erb new file mode 100644 index 0000000..6dc0abe --- /dev/null +++ b/views/clips.json.erb @@ -0,0 +1 @@ +<%= @result %>
\ No newline at end of file diff --git a/views/index.html.erb b/views/index.html.erb new file mode 100644 index 0000000..cb5b103 --- /dev/null +++ b/views/index.html.erb @@ -0,0 +1,13 @@ +<div> + <a href="javascript:(function(){var xhr=new XMLHttpRequest();var params='url='+window.location.href;xhr.open('POST','http://localhost:9292/clips',true);xhr.onreadystatechange=function(){if(this.readyState==4){if(xhr.status==200){if(xhr.responseText.success){}}}};xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');xhr.send(params);})()" onclick="alert('Drag this link onto your browser bookmarks bar.'); return false;">Clips</a> +</div> + +<% unless @clips.empty? %> + <ul> + <% @clips.each do |c| %> + <li><%= c.url %></li> + <% end %> + </ul> +<% else %> + No clips to display +<% end %> diff --git a/views/layout.html.erb b/views/layout.html.erb new file mode 100644 index 0000000..845929f --- /dev/null +++ b/views/layout.html.erb @@ -0,0 +1,23 @@ +<!doctype html> +<html lang="en"> +<head> +<meta http-equiv="content-type" content="text/html; charset=utf-8"> + <meta charset="utf-8"> + <title>Clips</title> + <meta name="description" content=""> + + <meta name="viewport" content="width=device-width"> + <link rel="stylesheet" href="css/style.css"> +</head> +<body> + <header> + + </header> + <div role="main"> + <%= yield %> + </div> + <footer> + + </footer> +</body> +</html>
\ No newline at end of file |
