Class: Magnolia::Mark

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

Overview

The mark class is used to store information about each bookmark after it is found, created or updated. Each method that is not in the DONT_PROCESS constant returns and array of Mark objects.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeMark

Returns a new instance of Mark.



374
375
376
# File 'lib/magnolia/magnolia.rb', line 374

def initialize
  @tags = []
end

Instance Attribute Details

#a_tagObject (readonly)

Creates a link to the url of the bookmark with the title as the text



388
389
390
# File 'lib/magnolia/magnolia.rb', line 388

def a_tag
  @a_tag
end

#createdObject

Returns the value of attribute created.



379
380
381
# File 'lib/magnolia/magnolia.rb', line 379

def created
  @created
end

#descriptionObject

Returns the value of attribute description.



377
378
379
# File 'lib/magnolia/magnolia.rb', line 377

def description
  @description
end

#idObject

Returns the value of attribute id.



377
378
379
# File 'lib/magnolia/magnolia.rb', line 377

def id
  @id
end

#img_tagObject (readonly)

Creates an image tag for the screenshot image with the title as the alt text



383
384
385
# File 'lib/magnolia/magnolia.rb', line 383

def img_tag
  @img_tag
end

#ownerObject

Returns the value of attribute owner.



379
380
381
# File 'lib/magnolia/magnolia.rb', line 379

def owner
  @owner
end

#privateObject

Returns the value of attribute private.



378
379
380
# File 'lib/magnolia/magnolia.rb', line 378

def private
  @private
end

#ratingObject

Returns the value of attribute rating.



378
379
380
# File 'lib/magnolia/magnolia.rb', line 378

def rating
  @rating
end

#screenshotObject

Returns the value of attribute screenshot.



378
379
380
# File 'lib/magnolia/magnolia.rb', line 378

def screenshot
  @screenshot
end

#tagsObject

Returns the value of attribute tags.



379
380
381
# File 'lib/magnolia/magnolia.rb', line 379

def tags
  @tags
end

#titleObject

Returns the value of attribute title.



377
378
379
# File 'lib/magnolia/magnolia.rb', line 377

def title
  @title
end

#updatedObject

Returns the value of attribute updated.



379
380
381
# File 'lib/magnolia/magnolia.rb', line 379

def updated
  @updated
end

#urlObject

Returns the value of attribute url.



377
378
379
# File 'lib/magnolia/magnolia.rb', line 377

def url
  @url
end

Class Method Details

.find(id) ⇒ Object

Finds a single bookmark by it’s id (short name). If not found raises a Magnolia::RequestError



448
449
450
451
# File 'lib/magnolia/magnolia.rb', line 448

def find(id)
  marks = Magnolia.get(:id =>id)
  marks[0]
end

.from_xml(xml) ⇒ Object

Converts xml into a mark



454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
# File 'lib/magnolia/magnolia.rb', line 454

def from_xml(xml)
  mark             = Mark.new()
  mark.title       = xml.elements['title'].text
  mark.url         = xml.elements['url'].text
  mark.description = xml.elements['description'].text
  mark.screenshot  = xml.elements['screenshot'].text
  mark.id          = xml.attributes['id']
  mark.rating      = xml.attributes['rating']
  mark.private     = xml.attributes['private']
  mark.created     = xml.attributes['created']
  mark.updated     = xml.attributes['updated']
  mark.owner       = xml.attributes['owner']
  REXML::XPath.each(xml, 'tags/tag') do |tag|
    mark.tags << tag.attributes['name']
  end
  mark
end

Instance Method Details

#saveObject

This method allows working with bookmarks much like ActiveRecord.

Examples

mark = Magnolia::Mark.new
mark.title = 'Addicted To New'
mark.url = 'http://addictedtonew.com/'
mark.tags = ['john nunemaker', 'me'] # you can also use comma-seperated list mark.tags = 'john nunemaker, me'
mark.save
puts mark.id # this is the id of the newly created bookmark

You can also use it to update bookmarks like so:

mark = Magnolia::Mark.find(:id => 'someshortname')
mark.title = 'Addicted To New by John Nunemaker'
mark.save


408
409
410
411
412
413
414
415
416
417
418
419
# File 'lib/magnolia/magnolia.rb', line 408

def save
  if self.id == nil
    # create a new bookmark
    mark    = to_h
    marks   = Magnolia.add(mark)
    self.id = marks[0].id
  else
    # update an existing bookmark
    mark    = to_h
    marks   = Magnolia.update(mark)
  end
end