Skip to content

Commit

Permalink
first working version
Browse files Browse the repository at this point in the history
  • Loading branch information
rodrigomanhaes committed Aug 20, 2010
1 parent 7211fd7 commit 84eb90a
Show file tree
Hide file tree
Showing 23 changed files with 350 additions and 48 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -1,4 +1,5 @@
config/database.yml
log
tmp
*.sqlite3

83 changes: 83 additions & 0 deletions app/controllers/blogs_controller.rb
@@ -0,0 +1,83 @@
class BlogsController < ApplicationController
# GET /blogs
# GET /blogs.xml
def index
@blogs = Blog.all

respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => @blogs }
end
end

# GET /blogs/1
# GET /blogs/1.xml
def show
@blog = Blog.find(params[:id])

respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => @blog }
end
end

# GET /blogs/new
# GET /blogs/new.xml
def new
@blog = Blog.new

respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => @blog }
end
end

# GET /blogs/1/edit
def edit
@blog = Blog.find(params[:id])
end

# POST /blogs
# POST /blogs.xml
def create
@blog = Blog.new(params[:blog])

respond_to do |format|
if @blog.save
format.html { redirect_to(@blog, :notice => 'Blog was successfully created.') }
format.xml { render :xml => @blog, :status => :created, :location => @blog }
else
format.html { render :action => "new" }
format.xml { render :xml => @blog.errors, :status => :unprocessable_entity }
end
end
end

# PUT /blogs/1
# PUT /blogs/1.xml
def update
@blog = Blog.find(params[:id])

respond_to do |format|
if @blog.update_attributes(params[:blog])
format.html { redirect_to(@blog, :notice => 'Blog was successfully updated.') }
format.xml { head :ok }
else
format.html { render :action => "edit" }
format.xml { render :xml => @blog.errors, :status => :unprocessable_entity }
end
end
end

# DELETE /blogs/1
# DELETE /blogs/1.xml
def destroy
@blog = Blog.find(params[:id])
@blog.destroy

respond_to do |format|
format.html { redirect_to(blogs_url) }
format.xml { head :ok }
end
end
end
13 changes: 13 additions & 0 deletions app/controllers/feeds_controller.rb
@@ -0,0 +1,13 @@
class FeedsController < ApplicationController
def index
@entries = Blog.all.
map(&:entries).
flatten.
compact.
sort_by(&:date_published).
reverse
render :layout => false
response.headers["Content-Type"] = "application/xml; charset=utf-8"
end
end

2 changes: 2 additions & 0 deletions app/helpers/blogs_helper.rb
@@ -0,0 +1,2 @@
module BlogsHelper
end
2 changes: 2 additions & 0 deletions app/helpers/feeds_helper.rb
@@ -0,0 +1,2 @@
module FeedsHelper
end
16 changes: 16 additions & 0 deletions app/models/blog.rb
@@ -0,0 +1,16 @@
require 'open-uri'
require 'feed-normalizer'

class Blog < ActiveRecord::Base
validates_presence_of :url
validates_uniqueness_of :url

def entries
feeds = FeedNormalizer::FeedNormalizer.parse(open(url.strip)).entries
feeds.each do |feed|
feed.date_published ||= Date.today - 100.years
end
feeds
end
end

16 changes: 16 additions & 0 deletions app/views/blogs/edit.html.erb
@@ -0,0 +1,16 @@
<h1>Editing blog</h1>

<% form_for(@blog) do |f| %>
<%= f.error_messages %>

<p>
<%= f.label :url %><br />
<%= f.text_field :url %>
</p>
<p>
<%= f.submit 'Update' %>
</p>
<% end %>
<%= link_to 'Show', @blog %> |
<%= link_to 'Back', blogs_path %>
20 changes: 20 additions & 0 deletions app/views/blogs/index.html.erb
@@ -0,0 +1,20 @@
<h1>Listing blogs</h1>

<table>
<tr>
<th>Url</th>
</tr>

<% @blogs.each do |blog| %>
<tr>
<td><%=h blog.url %></td>
<td><%= link_to 'Show', blog %></td>
<td><%= link_to 'Edit', edit_blog_path(blog) %></td>
<td><%= link_to 'Destroy', blog, :confirm => 'Are you sure?', :method => :delete %></td>
</tr>
<% end %>
</table>

<br />

<%= link_to 'New blog', new_blog_path %>
15 changes: 15 additions & 0 deletions app/views/blogs/new.html.erb
@@ -0,0 +1,15 @@
<h1>New blog</h1>

<% form_for(@blog) do |f| %>
<%= f.error_messages %>

<p>
<%= f.label :url %><br />
<%= f.text_field :url %>
</p>
<p>
<%= f.submit 'Create' %>
</p>
<% end %>
<%= link_to 'Back', blogs_path %>
8 changes: 8 additions & 0 deletions app/views/blogs/show.html.erb
@@ -0,0 +1,8 @@
<p>
<b>Url:</b>
<%=h @blog.url %>
</p>


<%= link_to 'Edit', edit_blog_path(@blog) %> |
<%= link_to 'Back', blogs_path %>
21 changes: 21 additions & 0 deletions app/views/feeds/index.rxml
@@ -0,0 +1,21 @@
xml.instruct!

xml.rss "version" => "2.0", "xmlns:dc" => "http://purl.org/dc/elements/1.1/" do
xml.channel do
xml.title "Planeta NSI"
xml.link url_for :only_path => false, :controller => 'index'
xml.description "Textos e artigos dos membros do NSI"

@entries.each do |article|
xml.item do
author = article.author.include?('(') ? article.author.split('(')[1].chop : article.author
xml.title "[#{author}] #{article.title}]"
xml.link article.url
xml.description article.content
xml.guid article.url
xml.pubDate article.date_published || Date.today - 100.years
end
end
end
end

17 changes: 17 additions & 0 deletions app/views/layouts/blogs.html.erb
@@ -0,0 +1,17 @@
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="content-type" content="text/html;charset=UTF-8" />
<title>Blogs: <%= controller.action_name %></title>
<%= stylesheet_link_tag 'scaffold' %>
</head>
<body>

<p style="color: green"><%= notice %></p>

<%= yield %>

</body>
</html>
6 changes: 5 additions & 1 deletion config/environment.rb
Expand Up @@ -20,6 +20,9 @@
# config.gem "sqlite3-ruby", :lib => "sqlite3"
# config.gem "aws-s3", :lib => "aws/s3"

config.gem 'feed-normalizer'
config.gem 'inherited_resources', :version => '1.0.6'

# Only load the plugins named here, in the order given (default is alphabetical).
# :all can be used as a placeholder for all plugins not explicitly named
# config.plugins = [ :exception_notification, :ssl_requirement, :all ]
Expand All @@ -38,4 +41,5 @@
# The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
# config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}')]
# config.i18n.default_locale = :de
end
end

2 changes: 2 additions & 0 deletions config/routes.rb
@@ -1,4 +1,6 @@
ActionController::Routing::Routes.draw do |map|
map.resources :blogs

# The priority is based upon order of creation: first created -> highest priority.

# Sample of regular route:
Expand Down
14 changes: 14 additions & 0 deletions db/migrate/20100820042939_create_blogs.rb
@@ -0,0 +1,14 @@
class CreateBlogs < ActiveRecord::Migration
def self.up
create_table :blogs do |t|
t.string :url

t.timestamps
end
end

def self.down
drop_table :blogs
end
end

20 changes: 20 additions & 0 deletions db/schema.rb
@@ -0,0 +1,20 @@
# This file is auto-generated from the current state of the database. Instead of editing this file,
# please use the migrations feature of Active Record to incrementally modify your database, and
# then regenerate this schema definition.
#
# Note that this schema.rb definition is the authoritative source for your database schema. If you need
# to create the application database on another system, you should be using db:schema:load, not running
# all the migrations from scratch. The latter is a flawed and unsustainable approach (the more migrations
# you'll amass, the slower it'll run and the greater likelihood for issues).
#
# It's strongly recommended to check this file into your version control system.

ActiveRecord::Schema.define(:version => 20100820042939) do

create_table "blogs", :force => true do |t|
t.string "url"
t.datetime "created_at"
t.datetime "updated_at"
end

end
54 changes: 54 additions & 0 deletions public/stylesheets/scaffold.css
@@ -0,0 +1,54 @@
body { background-color: #fff; color: #333; }

body, p, ol, ul, td {
font-family: verdana, arial, helvetica, sans-serif;
font-size: 13px;
line-height: 18px;
}

pre {
background-color: #eee;
padding: 10px;
font-size: 11px;
}

a { color: #000; }
a:visited { color: #666; }
a:hover { color: #fff; background-color:#000; }

.fieldWithErrors {
padding: 2px;
background-color: red;
display: table;
}

#errorExplanation {
width: 400px;
border: 2px solid red;
padding: 7px;
padding-bottom: 12px;
margin-bottom: 20px;
background-color: #f0f0f0;
}

#errorExplanation h2 {
text-align: left;
font-weight: bold;
padding: 5px 5px 5px 15px;
font-size: 12px;
margin: -7px;
background-color: #c00;
color: #fff;
}

#errorExplanation p {
color: #333;
margin-bottom: 0;
padding: 5px;
}

#errorExplanation ul li {
font-size: 12px;
list-style: square;
}

10 changes: 10 additions & 0 deletions spec/controllers/feeds_controller_spec.rb
@@ -0,0 +1,10 @@
require 'spec_helper'

describe FeedsController do

#Delete this example and add some real ones
it "should use FeedsController" do
controller.should be_an_instance_of(FeedsController)
end

end
7 changes: 7 additions & 0 deletions spec/fixtures/blogs.yml
@@ -0,0 +1,7 @@
# Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html

one:
url: MyString

two:
url: MyString
11 changes: 11 additions & 0 deletions spec/helpers/feeds_helper_spec.rb
@@ -0,0 +1,11 @@
require 'spec_helper'

describe FeedsHelper do

#Delete this example and add some real ones or delete this file
it "should be included in the object returned by #helper" do
included_modules = (class << helper; self; end).send :included_modules
included_modules.should include(FeedsHelper)
end

end
13 changes: 13 additions & 0 deletions spec/models/blog_spec.rb
@@ -0,0 +1,13 @@
require 'spec_helper'

describe Blog do
before(:each) do
@valid_attributes = {
:url => "value for url"
}
end

it "should create a new instance given valid attributes" do
Blog.create!(@valid_attributes)
end
end

0 comments on commit 84eb90a

Please sign in to comment.