class Mingle::Card

Constants

DOCTYPE_REGEX

Attributes

description[RW]
name[RW]
number[RW]

Public Class Methods

all(options={}) click to toggle source
# File lib/mingle/card.rb, line 7
def self.all(options={})
  options = {starting_from: '1'}.merge(options)
  Logging.debug "Mingle API => #{@@api.inspect}"
  starting_card_number = options[:starting_with].to_i
  max_card_number = unless options[:limit]
    mql = "SELECT MAX(number)"
    results = @@api.execute_mql(mql)
    Logging.debug "#{mql}: #{results.inspect}"
     results[0]['max_number'].to_i
   else
     starting_card_number + options[:limit].to_i
   end
  Enumerator.new do |yielder|
    (starting_card_number..max_card_number).to_a.each do |number|
      begin
        yielder.yield Card.find_by_number(number)
      rescue => e
        Logging.error "Unable to fetch card ##{number} due to #{e.message}"
      end
    end
  end
end
api=(api) click to toggle source
# File lib/mingle/card.rb, line 36
def self.api=(api)
  @@api = api
end
find_by_number(number) click to toggle source
# File lib/mingle/card.rb, line 30
def self.find_by_number(number)
  Logging.debug("fetching card ##{number}")
  card_xml = @@api.get_card(number)
  Card.new(card_xml)
end
new(xml) click to toggle source
# File lib/mingle/card.rb, line 42
def initialize(xml)
  document = Nokogiri::XML.parse(xml)
  @description = document.xpath('./card/description').text
  @number = document.xpath('./card/number').text.to_i
  @name = document.xpath('./card/name').text
end

Public Instance Methods

attachments() click to toggle source
# File lib/mingle/card.rb, line 53
def attachments
  @attachments ||= Attachment.find_all_by_card_number(number)
end
description=(desc) click to toggle source
# File lib/mingle/card.rb, line 49
def description=(desc)
  @description = remove_enclosing_html_doc_and_body(desc)
end
save!() click to toggle source
# File lib/mingle/card.rb, line 57
def save!
  @@api.save_card(self)
end
to_xml() click to toggle source
# File lib/mingle/card.rb, line 61
def to_xml
  Nokogiri::XML::Builder.new { |xml|
    xml.card {
      xml.description(@description)
    }
  }.to_xml
end

Private Instance Methods

remove_enclosing_html_doc_and_body(html) click to toggle source
# File lib/mingle/card.rb, line 73
def remove_enclosing_html_doc_and_body(html)
  html.gsub(DOCTYPE_REGEX, '').strip
    .gsub(/<html>/, '').strip
    .gsub(/<body>/, '').strip
    .gsub(/<\/html>$/, '').strip
    .gsub(/<\/body>$/, '').strip
end