tweepml 0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/README +12 -0
  2. data/lib/tweepml.rb +227 -0
  3. metadata +55 -0
data/README ADDED
@@ -0,0 +1,12 @@
1
+ TweepML is an XML format used to represent a list of Tweeps (Twitter users). The primary goal of this format is to allow people to easily share between application a list of Tweeps. For example, you might want to export a list of tweeps from application A and import into application B. Or, you might want to publish on your website a list of tweeps you recommend people to follow. In addition to provide an aggregation of Tweeps, TweepML allows for an hierarchical grouping of Tweeps as well as tagging. Some of the inspiration for TweepML came from OPML, a simple format to share a list of blogs (blogroll). TweepML is also extensible, which allows applications and services to augment its structure with additional data.
2
+
3
+ Read in a TweepML XML file with the following:
4
+ tml = TweepML.new(File.read('/path/to/tweepml.xml'))
5
+
6
+ The tweep_list from the XML will then be accessible as:
7
+ tml.tweep_list
8
+
9
+ As TweepLists can contain other lists as well as lists of Tweeps, you can iterate through them with the following:
10
+ tml.tweep_list.tweep_lists - returns an array of TweepLists
11
+ or
12
+ tml.tweep_list.tweeps - returns an array of Tweeps
data/lib/tweepml.rb ADDED
@@ -0,0 +1,227 @@
1
+ # TweepML Ruby Implementation - http://tweepml.org/code/ruby
2
+ #
3
+ # Author:: Damon P. Cortesi (@dacort)
4
+ # License:: Apache License, Version 2.0
5
+ #
6
+ # TweepML is an XML format used to represent a list of Tweeps (Twitter users).
7
+ require 'rexml/document'
8
+ include REXML
9
+
10
+ class TweepML
11
+ attr_accessor :title, :description, :link, :date_created, :date_modified, :contact_name, :contact_screen_name,
12
+ :copyright, :generator, :generator_link, :error_code, :error_description, :tags, :tweep_list
13
+
14
+ # Create a new TweepML representation.
15
+ #
16
+ # Optional parameter of a string to parse
17
+ def initialize(source=nil)
18
+ @tweep_list = TweepList.new
19
+ parse(source) if source
20
+ end
21
+
22
+ # Set the title of the TweepML document.
23
+ # Max length is 80 characters
24
+ def title=(title)
25
+ @title = title[0..79]
26
+ end
27
+
28
+ # Set the description of the TweepML document.
29
+ # Max length is 250 characters
30
+ def description=(desc)
31
+ @description = desc[0..249]
32
+ end
33
+
34
+ # Set the _contact_name_ of the TweepML document.
35
+ # Max length is 50 characters
36
+ def contact_name=(contact_name)
37
+ @contact_name = contact_name.gsub("@","")[0..49]
38
+ end
39
+
40
+ # Set the _generator_ of the TweepML document.
41
+ # Max length is 100 characters
42
+ def generator=(generator)
43
+ @generator = generator[0..99]
44
+ end
45
+
46
+ # Set the _generator_link_ of the TweepML document.
47
+ # Max length is 100 characters
48
+ def generator_link=(url)
49
+ @generator_link = url[0..99]
50
+ end
51
+
52
+ # XML representation of the TweepML
53
+ def to_xml
54
+ doc = Document.new
55
+ tml = doc.add_element 'tweepml', {'version' => '1.0'}
56
+
57
+ # Add the header elements
58
+ head = tml.add_element 'head'
59
+ [:title, :description, :link, :date_created, :date_modified, :contact_name, :contact_screen_name,
60
+ :copyright, :error_code, :error_description, :tags].each do |prop|
61
+ unless self.send(prop).nil?
62
+ t = head.add_element prop.to_s
63
+ t.text = self.send(prop)
64
+ end
65
+ end
66
+
67
+ head.add_element("generator").text = "TweepML Ruby Generator v0.1"
68
+ head.add_element("generator_link").text = "http://github.com/dacort/tweepml"
69
+
70
+ # Add the tweep_list element
71
+ tweep_list = tml.add_element 'tweep_list'
72
+
73
+ # Iterate through all tweeps/tweep_lists - should probably use nodes
74
+ self.tweep_list.tweep_lists.each do|tl|
75
+ tweep_list.add_element(tl.to_xml)
76
+ end
77
+
78
+ doc
79
+ end
80
+
81
+ private
82
+
83
+ # Parse the TweepML string _source_ into a Ruby data structure and return it.
84
+ def parse(source)
85
+ doc = Document.new(source)
86
+
87
+ # Parse the head element of the TweepML source
88
+ doc.elements.each("tweepml/head/*") do |element|
89
+ (self.send "#{element.name}=", element.text) if self.respond_to?(element.name)
90
+ end
91
+
92
+ # Parse the first tweep_list element
93
+ tl = self.tweep_list
94
+ root_tl = doc.elements['tweepml/tweep_list']
95
+ root_tl.attributes.each do |key,value|
96
+ (tl.send "#{key}=", value) if tl.respond_to?(key)
97
+ end
98
+ parse_tweep_list(tl, doc.elements['tweepml/tweep_list'])
99
+ end
100
+
101
+ def parse_tweep(tweep_list, element)
102
+ return if (element.attributes['id'].nil? and element.attributes['screen_name'].nil?)
103
+ t = Tweep.new(element.attributes['screen_name'], element.attributes['id'],
104
+ element.attributes['title'],element.attributes['tags'])
105
+
106
+ tweep_list.add_tweep(t)
107
+ end
108
+
109
+ def parse_tweep_list(tweep_list, element)
110
+ # Parse tweep lists
111
+ element.elements.each("*") do |element|
112
+ if element.name.downcase == "tweep"
113
+ # Parse and add the tweep to the root element
114
+ parse_tweep(tweep_list, element)
115
+ elsif element.name.downcase == "tweep_list"
116
+ # Create a new tweep list
117
+ tl = TweepList.new
118
+ element.attributes.each do |key,value|
119
+ (tl.send "#{key}=", value) if tl.respond_to?(key)
120
+ end
121
+ tweep_list.add_tweep_list(tl)
122
+
123
+ # Recursively parse the tweep list
124
+ parse_tweep_list(tl, element)
125
+ end
126
+ end
127
+
128
+ end
129
+ end
130
+
131
+ class TweepList
132
+ attr_accessor :title, :tags
133
+
134
+ def initialize(title=nil, tags=nil)
135
+ @title = title[0..79] unless title.nil?
136
+ @tags = tags
137
+
138
+ @nodes = []
139
+ end
140
+
141
+ # Add a nested TweepList
142
+ def add_tweep_list(tweep_list)
143
+ @nodes.push(tweep_list)
144
+ end
145
+
146
+ # Add a new Tweep
147
+ def add_tweep(tweep)
148
+ @nodes.push(tweep) unless self.find_by_screen_name(tweep.screen_name)
149
+ end
150
+
151
+ # Find a tweep in this list by screen_name
152
+ def find_by_screen_name(screen_name)
153
+ @nodes.select{|t| t.is_a?Tweep and t.screen_name == screen_name}.first
154
+ end
155
+
156
+ # List all tweeps in this list
157
+ def tweeps
158
+ @nodes.select{|t| t.is_a?Tweep}
159
+ end
160
+
161
+ # List all TweepLists in this list
162
+ def tweep_lists
163
+ @nodes.select{|t| t.is_a?TweepList}
164
+ end
165
+
166
+ # XML representation of TweepList
167
+ def to_xml
168
+ tweep_list = Element.new 'tweep_list'
169
+ tweep_list.add_attribute('title', @title) unless @title.nil?
170
+ tweep_list.add_attribute('tags', @tags) unless @tags.nil?
171
+
172
+ @nodes.each{|n| tweep_list.add_element(n.to_xml)}
173
+
174
+ tweep_list
175
+ end
176
+ end
177
+
178
+ class Tweep
179
+ attr_accessor :id, :screen_name, :title, :tags
180
+
181
+ # Create a new Tweep
182
+ def initialize(screen_name, id=nil, title=nil, tags=nil)
183
+ @id = id.to_i unless id.nil?
184
+ @screen_name = screen_name
185
+ @title = title[0..79] unless title.nil?
186
+ @tags = tags
187
+ end
188
+
189
+ # Set the _id_ of the Tweep, enforcing numerals
190
+ def id=(id)
191
+ @id = id.to_i
192
+ end
193
+
194
+ # Set the _title_ of the Tweep.
195
+ # Max length is 80
196
+ def title=(title)
197
+ @title = title[0..79]
198
+ end
199
+
200
+ # String representation of a Tweep
201
+ def to_s
202
+ @screen_name
203
+ end
204
+
205
+ # XML representation of a Tweep
206
+ def to_xml
207
+ tweep = Element.new 'tweep'
208
+ [:id, :screen_name, :title, :tags].each do |prop|
209
+ unless self.send(prop).nil?
210
+ tweep.add_attribute(prop.to_s, "#{self.send(prop)}")
211
+ end
212
+ end
213
+
214
+ tweep
215
+ end
216
+
217
+ # Find a tweep by screen_name
218
+ def self.find_by_screen_name(screen_name)
219
+ found = nil
220
+
221
+ ObjectSpace.each_object(Tweep) { |o|
222
+ found = o if o.screen_name == screen_name
223
+ }
224
+ found
225
+ end
226
+ end
227
+
metadata ADDED
@@ -0,0 +1,55 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tweepml
3
+ version: !ruby/object:Gem::Version
4
+ version: "0.2"
5
+ platform: ruby
6
+ authors:
7
+ - Damon P. Cortesi
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-09-29 00:00:00 -04:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: TweepML is an XML format used to represent a list of Tweeps (Twitter users).
17
+ email: d.lifehacker@gmail.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - README
24
+ files:
25
+ - README
26
+ - lib/tweepml.rb
27
+ has_rdoc: true
28
+ homepage: http://github.com/dacort/tweepml
29
+ post_install_message:
30
+ rdoc_options:
31
+ - --inline-source
32
+ - --charset=UTF-8
33
+ require_paths:
34
+ - lib
35
+ required_ruby_version: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: "0"
40
+ version:
41
+ required_rubygems_version: !ruby/object:Gem::Requirement
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ version: "0"
46
+ version:
47
+ requirements: []
48
+
49
+ rubyforge_project:
50
+ rubygems_version: 1.3.1
51
+ signing_key:
52
+ specification_version: 2
53
+ summary: TweepML is an XML format used to represent a list of Tweeps (Twitter users).
54
+ test_files: []
55
+