Class: FakeChargify::Subscription

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#activated_atObject

Returns the value of attribute activated_at.



7
8
9
# File 'lib/fake_chargify/subscription.rb', line 7

def activated_at
  @activated_at
end

#balance_in_centsObject

Returns the value of attribute balance_in_cents.



7
8
9
# File 'lib/fake_chargify/subscription.rb', line 7

def balance_in_cents
  @balance_in_cents
end

#created_atObject

Returns the value of attribute created_at.



7
8
9
# File 'lib/fake_chargify/subscription.rb', line 7

def created_at
  @created_at
end

#credit_cardObject

Returns the value of attribute credit_card.



7
8
9
# File 'lib/fake_chargify/subscription.rb', line 7

def credit_card
  @credit_card
end

#current_period_ends_atObject

Returns the value of attribute current_period_ends_at.



7
8
9
# File 'lib/fake_chargify/subscription.rb', line 7

def current_period_ends_at
  @current_period_ends_at
end

#current_period_started_atObject

Returns the value of attribute current_period_started_at.



7
8
9
# File 'lib/fake_chargify/subscription.rb', line 7

def current_period_started_at
  @current_period_started_at
end

#customerObject

Returns the value of attribute customer.



7
8
9
# File 'lib/fake_chargify/subscription.rb', line 7

def customer
  @customer
end

#expires_atObject

Returns the value of attribute expires_at.



7
8
9
# File 'lib/fake_chargify/subscription.rb', line 7

def expires_at
  @expires_at
end

#idObject

Returns the value of attribute id.



7
8
9
# File 'lib/fake_chargify/subscription.rb', line 7

def id
  @id
end

#productObject

Returns the value of attribute product.



7
8
9
# File 'lib/fake_chargify/subscription.rb', line 7

def product
  @product
end

#stateObject

Returns the value of attribute state.



7
8
9
# File 'lib/fake_chargify/subscription.rb', line 7

def state
  @state
end

#trial_ended_atObject

Returns the value of attribute trial_ended_at.



7
8
9
# File 'lib/fake_chargify/subscription.rb', line 7

def trial_ended_at
  @trial_ended_at
end

#trial_started_atObject

Returns the value of attribute trial_started_at.



7
8
9
# File 'lib/fake_chargify/subscription.rb', line 7

def trial_started_at
  @trial_started_at
end

#updated_atObject

Returns the value of attribute updated_at.



7
8
9
# File 'lib/fake_chargify/subscription.rb', line 7

def updated_at
  @updated_at
end

Class Method Details

.from_xml(xml) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/fake_chargify/subscription.rb', line 33

def self.from_xml(xml)
  subscription = Subscription.new
  doc = Nokogiri::XML.parse(xml)
  doc.xpath('//subscription').map do |e|
    subscription.id = e.xpath('id').text.to_i
    subscription.balance_in_cents = e.xpath('balance_in_cents').text.to_i
    unless e.xpath('product_handle').empty?
      subscription.product = Product.new
      subscription.product.handle = e.xpath('product_handle').text
    end
    if e.xpath('customer_reference').empty?
      e.xpath('customer_attributes').map do |c|
        subscription.customer = Customer.new
        subscription.customer.first_name = c.xpath('first_name').text
        subscription.customer.last_name = c.xpath('last_name').text
        subscription.customer.email = c.xpath('email').text
      end
    else
      customers = FakeChargify.customers.repository.select { |c| c.reference == e.xpath('customer_reference').text }
      subscription.customer = customers.first
    end
    e.xpath('credit_card_attributes').map do |cc|
      subscription.credit_card = CreditCard.new
      subscription.credit_card.full_number = cc.xpath('full_number').text
      subscription.credit_card.expiration_month = cc.xpath('expiration_month').text.to_i
      subscription.credit_card.expiration_year = cc.xpath('expiration_year').text.to_i
    end
  end
  subscription
end

Instance Method Details

#to_xmlObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/fake_chargify/subscription.rb', line 11

def to_xml
  builder = Nokogiri::XML::Builder.new do |xml|
    xml.subscription {
      xml.id_ id
      xml.state state
      xml.balance_in_cents balance_in_cents
      xml.current_period_started_at current_period_started_at
      xml.current_period_ends_at current_period_ends_at
      xml.trial_started_at trial_started_at
      xml.trial_ended_at trial_ended_at
      xml.activated_at activated_at
      xml.expires_at expires_at
      xml.created_at created_at
      xml.updated_at updated_at
      xml << Nokogiri.XML(customer.to_xml).root.to_xml unless customer.nil?
      xml << Nokogiri.XML(product.to_xml).root.to_xml unless product.nil?
      xml << Nokogiri.XML(credit_card.to_xml).root.to_xml unless credit_card.nil?
    }
  end
  builder.to_xml
end