Class: Site

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

Direct Known Subclasses

Flkr, Jts, RobotCoop, Y2

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params) ⇒ Site

Returns a new instance of Site.



207
208
209
210
211
212
213
214
215
216
217
218
219
# File 'lib/tRuTag.rb', line 207

def initialize(params)
	#$stderr.print params['sitename'] + "\n"
	params.delete_if{ | k, v | v=="" || v==nil }	 

	
	@attrib={ 'url' => %{www.#{params['sitename']}.com}, 'validcode' => %{200},
			 'needsauth' => %{401}, 'all_tags_path' => %{/tag/}, 'all_tags_path_post_tag' => "",
			 'xmlelement' => %{feed/tags/tag}, 'space_replacement' => '+'}
		
	@attrib.update(params)		
	
	#$stderr.print(%{#{@attrib['sitename']} is using #{self.class} })
end

Instance Attribute Details

#attribObject (readonly)

This is a base class, you will want to instantiate a class that inherits from site.

To create a site object you will need to pass in a hash containing the following: username: username if this is for input pwd: if it is necessary for the site. offline: (optional) The XML file to use instad of the URL



205
206
207
# File 'lib/tRuTag.rb', line 205

def attrib
  @attrib
end

Instance Method Details

#add_tags(tbin) ⇒ Object



221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
# File 'lib/tRuTag.rb', line 221

def add_tags(tbin)
	required_attribute('username')
	required_attribute('tag_api_page')
	required_attribute('xmlelement')
	required_attribute('parse_tags')

	#get the UML
	uml_page=get_uml
					
	#parse the UML
	begin
		doc=REXML::Document.new uml_page
	rescue Exception, REXML::ParseException
		Trutag::loggit(2, %{Error parsing XML from: #{@attrib['sitename']}})				
	end
	
	if doc != nil
		# parse tags is found in websites.yml
		# example ofthe string parsetags: {tbin[@attrib['tel'].text.downcase] = tbin.fetch(@attrib['tel'].text.downcase, 0) + Integer(@attrib['tel'].attributes['count'])}
		# Ruby rocks!
		
		doc.elements.each(@attrib['xmlelement']) do |element|
		eval @attrib['parse_tags']
		end
	end
end

#get_umlObject



248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
# File 'lib/tRuTag.rb', line 248

def get_uml
	if  @attrib.fetch('isoffline', FALSE)
		if @attrib.has_key?('offline')
			if File.exist?(@attrib['offline'])
				data = File.open(@attrib['offline'], "r") 
			else
				Trutag::loggit(1, %{#@attrib['offline'] does not exist})
			end
		else
			Trutag::loggit(0, %{No offline file configured for class: #{self.class}})			
		end
	else
		required_attribute('url')
		required_attribute('needsauth')
		required_attribute('validcode')
		required_attribute('username')	
		required_attribute('tag_api_page')
		
		data = Net::HTTP.start(@attrib['url']) { |http|
			subpage = @attrib['tag_api_page']
		
			if @attrib.has_key?('api_key')
				subpage = subpage.gsub(/<api_key>/, @attrib['api_key'])
			end
			
			if @attrib.has_key?('username')
				subpage = subpage.gsub(/<username>/, @attrib['username'])
			end	
			
			req = Net::HTTP::Get.new(subpage,  {"tRuTag" => $agent})
			
			if http.request(req).code == @attrib['needsauth'] then
				required_attribute('pwd')
				req.basic_auth(@attrib['username'], @attrib['pwd'])
			end
			
			if http.request(req).code != @attrib['validcode'] then
				Trutag::loggit(1, %{Received response: #{http.request(req).code} - #{http.request(req).message} from #{@attrib['sitename']}})
			end

			http.request(req).body
		}	
	end
	data
end

#required_attribute(attribute) ⇒ Object



294
295
296
297
298
299
300
301
302
# File 'lib/tRuTag.rb', line 294

def required_attribute(attribute)
	if @attrib != nil
		if !@attrib.has_key?(attribute) || @attrib[attribute] == nil
			raise ArgumentError, %{#{@attrib['sitename']} requires attribute: #{attribute}}, caller
		end
	else
		raise ArgumentError, %{#{self.class} @attrib == nil}	
	end
end