Class: AtomPost

Inherits:
Object
  • Object
show all
Defined in:
lib/Html2Feedbooks/app.rb

Overview

def colour(text, colour_code) “#colour_code#texte[0m” end def green(text); colour(text, “e[32m”); end def red(text); colour(text, “e[31m”); end def yellow(text); colour(text, “e[33m”); end def blue(text); colour(text, “e[34m”); end

Constant Summary collapse

HTMLENCODER =
HTMLEntities.new

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(addrs = nil) ⇒ AtomPost

Returns a new instance of AtomPost.



26
27
28
# File 'lib/Html2Feedbooks/app.rb', line 26

def initialize(addrs=nil)
	self.addr=addrs unless addrs.nil?
end

Instance Attribute Details

#addrObject

Returns the value of attribute addr.



21
22
23
# File 'lib/Html2Feedbooks/app.rb', line 21

def addr
  @addr
end

#authorObject

Returns the value of attribute author.



20
21
22
# File 'lib/Html2Feedbooks/app.rb', line 20

def author
  @author
end

#contentObject

Returns the value of attribute content.



18
19
20
# File 'lib/Html2Feedbooks/app.rb', line 18

def content
  @content
end

#dateObject

Returns the value of attribute date.



19
20
21
# File 'lib/Html2Feedbooks/app.rb', line 19

def date
  @date
end

#passObject

Returns the value of attribute pass.



23
24
25
# File 'lib/Html2Feedbooks/app.rb', line 23

def pass
  @pass
end

#titleObject

Returns the value of attribute title.



17
18
19
# File 'lib/Html2Feedbooks/app.rb', line 17

def title
  @title
end

#typeObject

Returns the value of attribute type.



24
25
26
# File 'lib/Html2Feedbooks/app.rb', line 24

def type
  @type
end

#userObject

Returns the value of attribute user.



22
23
24
# File 'lib/Html2Feedbooks/app.rb', line 22

def user
  @user
end

Instance Method Details

#decode_text(txt) ⇒ Object



88
89
90
91
92
93
# File 'lib/Html2Feedbooks/app.rb', line 88

def decode_text(txt)
	return txt if txt.blank?
	m=Nokogiri::XML("<text>#{txt}</text>")
	m.traverse{|t| next unless t.text?; HTMLENCODER.decode(t.text)}
	m.root.inner_html
end

#down_url(entry_url) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/Html2Feedbooks/app.rb', line 30

def down_url(entry_url)
	#STDERR.puts "scanning #{entry_url}"
	url=URI.parse(entry_url)
	Net::HTTP.start(url.host,url.port) {|http|
		req = Net::HTTP::Get.new(url.path)
		req.basic_auth user,pass  unless user.nil?
		response = http.request(req)
		doc=Nokogiri::XML(response.body).remove_namespaces!
		e=doc.at('//entry/link[@rel="down"]')
		return 	URI.parse(e[:href]).path unless e.nil? 
	}
end

#force_decimal_entities(txt) ⇒ Object



84
85
86
# File 'lib/Html2Feedbooks/app.rb', line 84

def force_decimal_entities(txt)
	HTMLENCODER.encode(HTMLENCODER.decode(txt),:decimal)
end

#recode_text(txt) ⇒ Object



77
78
79
80
81
82
# File 'lib/Html2Feedbooks/app.rb', line 77

def recode_text(txt)
	return txt if txt.blank?
	m=Nokogiri::XML("<text>#{txt}</text>")
	m.traverse{|t| next unless t.text?;t.text=force_decimal_entities(t.text) if t.text.match(/&[a-z][a-z0-9]+;/i)}
	m.root.inner_html
end

#sendObject

Raises:

  • (StandardError)


43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/Html2Feedbooks/app.rb', line 43

def send
	raise StandardError.new('Missing Address') if addr.nil?
	#3: Detailed control
	url = URI.parse(addr)
	#STDERR.puts "sending to #{url}"
	req = Net::HTTP::Post.new(url.path)
	req.basic_auth user,pass  unless user.nil?
	req.body  = '<?xml version="1.0"?>'+"\n"
	req.body  +='<entry xmlns="http://www.w3.org/2005/Atom">'+"\n"
	req.body  +='<title>'+decode_text(title)+'</title>'+"\n"
	req.body  +='<id>'+Digest::MD5.hexdigest(title+content)+'</id>'+"\n"
	req.body  +='<updated>'+date.xmlschema+'</updated>'+"\n"
	req.body  +='<author><name>'+author+'</name></author>'+"\n"
	req.body  +='<content>'+recode_text(content)+'</content>'+"\n"
	req.body  +='<category label="'+type+'" term="'+type+'" />'+"\n" unless type.nil?
	req.body  +='</entry>'+"\n"

	req.set_content_type('application/atom+xml;type=entry')

#	STDERR.puts	red("Send \n #{req.body.size > 500 ? req.body[0..250]+'[...]'+req.body[-250..-1]: req.body}")

	res = Net::HTTP.new(url.host, url.port).start {|http| http.request(req) }
	case res
	when Net::HTTPRedirection
		self.addr=res['location']
		return send
	when Net::HTTPSuccess
#			STDERR.puts green(res['location']) if res['location']
		res['location'] if res['location']
	else
		res.error!
	end
end