Class: Mingle::Card

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(xml) ⇒ Card

Returns a new instance of Card.



42
43
44
45
46
47
# 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

Instance Attribute Details

#descriptionObject

Returns the value of attribute description.



40
41
42
# File 'lib/mingle/card.rb', line 40

def description
  @description
end

#nameObject

Returns the value of attribute name.



40
41
42
# File 'lib/mingle/card.rb', line 40

def name
  @name
end

#numberObject

Returns the value of attribute number.



40
41
42
# File 'lib/mingle/card.rb', line 40

def number
  @number
end

Class Method Details

.all(options = {}) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# 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) ⇒ Object



36
37
38
# File 'lib/mingle/card.rb', line 36

def self.api=(api)
  @@api = api
end

.find_by_number(number) ⇒ Object



30
31
32
33
34
# 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

Instance Method Details

#attachmentsObject



53
54
55
# File 'lib/mingle/card.rb', line 53

def attachments
  @attachments ||= Attachment.find_all_by_card_number(number)
end

#save!Object



57
58
59
# File 'lib/mingle/card.rb', line 57

def save!
  @@api.save_card(self)
end

#to_xmlObject



61
62
63
64
65
66
67
# File 'lib/mingle/card.rb', line 61

def to_xml
  Nokogiri::XML::Builder.new { |xml|
    xml.card {
      xml.description(@description)
    }
  }.to_xml
end