forked from reddavis/embedit
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Michael Bumann
committed
Sep 28, 2008
1 parent
0bba6c2
commit 5ba8f53
Showing
2 changed files
with
63 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
module Embedit | ||
|
||
class Twitter | ||
|
||
attr_reader :title, :url, :format | ||
|
||
def initialize(url) | ||
@format = 'photo' | ||
@url = url | ||
end | ||
|
||
def html(options = {}) | ||
attributes = options.collect {|k,v| "#{k}=\"#{v}\" "}.join(" ") | ||
%{<img src="http://twictur.es/i/#{status_id}.gif" #{attributes} />} | ||
end | ||
|
||
def status_id | ||
@status_id ||= url[/twitter\.com\/(\S+)\/statuses\/(\d+)/,2] | ||
end | ||
|
||
def title | ||
@title ||= page.at("title").inner_html | ||
end | ||
|
||
def self.match(url) | ||
url.match(/twitter\.com\/(\S+)\/statuses\/(\d+)/) | ||
end | ||
|
||
private | ||
|
||
def page | ||
@page ||= Hpricot(open(@url)) | ||
end | ||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
require File.dirname(__FILE__) + '/spec_helper.rb' | ||
|
||
describe "Twitter" do | ||
|
||
it "should show true on valid url" do | ||
Embedit::Media.new("http://twitter.com/Bumi/statuses/937196371").should be_valid | ||
end | ||
|
||
it "should return the correct title" do | ||
Embedit::Media.new("http://twitter.com/Bumi/statuses/937196371").title.should eql("Twitter / Michael Bumann: I love github!") | ||
end | ||
|
||
it "should show format as photo" do | ||
Embedit::Media.new("http://twitter.com/Bumi/statuses/937196371").format.should eql("photo") | ||
end | ||
|
||
it "should accept width and height options for embeded code" do | ||
embed = Embedit::Media.new("http://twitter.com/Bumi/statuses/937196371").html(:width=>200, :height => 100) | ||
embed.match(/width="200"/).should_not be_nil | ||
embed.match(/height="100"/).should_not be_nil | ||
end | ||
|
||
it "should return correct the embed code" do | ||
Embedit::Media.new("http://twitter.com/Bumi/statuses/937196371").html.should eql('<img src="http://twictur.es/i/937196371.gif" />') | ||
end | ||
|
||
end |