Class: ChargeBee::Model

Inherits:
Object
  • Object
show all
Defined in:
lib/chargebee/models/model.rb

Direct Known Subclasses

Addon, Addon::Tier, Address, AdvanceInvoiceSchedule, AdvanceInvoiceSchedule::FixedIntervalSchedule, AdvanceInvoiceSchedule::SpecificDatesSchedule, AttachedItem, Card, Comment, Contact, ContractTerm, Coupon, Coupon::ItemConstraint, Coupon::ItemConstraintCriteria, CouponCode, CouponSet, CreditNote, CreditNote::Allocation, CreditNote::Discount, CreditNote::Einvoice, CreditNote::LineItem, CreditNote::LineItemDiscount, CreditNote::LineItemTax, CreditNote::LineItemTier, CreditNote::LinkedRefund, CreditNote::Tax, CreditNoteEstimate, CreditNoteEstimate::Discount, CreditNoteEstimate::LineItem, CreditNoteEstimate::LineItemDiscount, CreditNoteEstimate::LineItemTax, CreditNoteEstimate::LineItemTier, CreditNoteEstimate::Tax, Customer, Customer::Balance, Customer::BillingAddress, Customer::ChildAccountAccess, Customer::Contact, Customer::EntityIdentifier, Customer::ParentAccountAccess, Customer::PaymentMethod, Customer::ReferralUrl, Customer::Relationship, DifferentialPrice, DifferentialPrice::ParentPeriod, DifferentialPrice::Tier, Download, EntitlementOverride, Estimate, Event, Event::Webhook, Export, Export::Download, Feature, Feature::Level, Gift, Gift::GiftReceiver, Gift::GiftTimeline, Gift::Gifter, Hierarchy, HostedPage, ImpactedItem, ImpactedItem::Download, ImpactedSubscription, ImpactedSubscription::Download, Invoice, Invoice::AdjustmentCreditNote, Invoice::AppliedCredit, Invoice::BillingAddress, Invoice::Discount, Invoice::DunningAttempt, Invoice::Einvoice, Invoice::IssuedCreditNote, Invoice::LineItem, Invoice::LineItemDiscount, Invoice::LineItemTax, Invoice::LineItemTier, Invoice::LinkedOrder, Invoice::LinkedPayment, Invoice::Note, Invoice::ShippingAddress, Invoice::Tax, InvoiceEstimate, InvoiceEstimate::Discount, InvoiceEstimate::LineItem, InvoiceEstimate::LineItemDiscount, InvoiceEstimate::LineItemTax, InvoiceEstimate::LineItemTier, InvoiceEstimate::Tax, Item, Item::ApplicableItem, ItemEntitlement, ItemFamily, ItemPrice, ItemPrice::AccountingDetail, ItemPrice::TaxDetail, ItemPrice::Tier, Order, Order::BillingAddress, Order::LineItemDiscount, Order::LineItemTax, Order::LinkedCreditNote, Order::OrderLineItem, Order::ResentOrder, Order::ShippingAddress, PaymentIntent, PaymentIntent::PaymentAttempt, PaymentSource, PaymentSource::AmazonPayment, PaymentSource::BankAccount, PaymentSource::Card, PaymentSource::Mandate, PaymentSource::Paypal, PaymentSource::Upi, Plan, Plan::ApplicableAddon, Plan::AttachedAddon, Plan::EventBasedAddon, Plan::Tier, PortalSession, PortalSession::LinkedCustomer, PromotionalCredit, Purchase, Quote, Quote::BillingAddress, Quote::Discount, Quote::LineItem, Quote::LineItemDiscount, Quote::LineItemTax, Quote::LineItemTier, Quote::ShippingAddress, Quote::Tax, QuoteLineGroup, QuoteLineGroup::Discount, QuoteLineGroup::LineItem, QuoteLineGroup::LineItemDiscount, QuoteLineGroup::LineItemTax, QuoteLineGroup::Tax, QuotedCharge, QuotedCharge::Addon, QuotedCharge::Charge, QuotedCharge::Coupon, QuotedCharge::InvoiceItem, QuotedCharge::ItemTier, QuotedSubscription, QuotedSubscription::Addon, QuotedSubscription::Coupon, QuotedSubscription::EventBasedAddon, QuotedSubscription::ItemTier, QuotedSubscription::QuotedContractTerm, QuotedSubscription::SubscriptionItem, ResourceMigration, SiteMigrationDetail, Subscription, Subscription::Addon, Subscription::ChargedEventBasedAddon, Subscription::ChargedItem, Subscription::ContractTerm, Subscription::Coupon, Subscription::EventBasedAddon, Subscription::ItemTier, Subscription::ReferralInfo, Subscription::ShippingAddress, Subscription::SubscriptionItem, SubscriptionEntitlement, SubscriptionEntitlement::Component, SubscriptionEstimate, SubscriptionEstimate::ContractTerm, SubscriptionEstimate::ShippingAddress, TaxWithheld, ThirdPartyPaymentMethod, TimeMachine, Token, Transaction, Transaction::LinkedCreditNote, Transaction::LinkedInvoice, Transaction::LinkedPayment, Transaction::LinkedRefund, UnbilledCharge, UnbilledCharge::Tier, Usage, VirtualBankAccount

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(values, sub_types = {}, dependant_types = {}) ⇒ Model

Returns a new instance of Model.



7
8
9
10
11
# File 'lib/chargebee/models/model.rb', line 7

def initialize(values, sub_types={}, dependant_types={})
  @values = values
  @sub_types = sub_types
  @dependant_types = dependant_types
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, *args, &block) ⇒ Object

Raises:

  • (NoMethodError)


44
45
46
47
48
49
50
51
52
# File 'lib/chargebee/models/model.rb', line 44

def method_missing(m, *args, &block)
  if(@values.has_key?(m))
      return @values[m]
  elsif(m[0,3] == "cf_") # All the custom fields start with prefix cf_. 
      return nil
  end
  message = "There's no method called #{m} #{args} here -- please try again."
  raise NoMethodError, message
end

Class Method Details

.construct(values, sub_types = {}, dependant_types = {}) ⇒ Object



65
66
67
68
69
70
71
# File 'lib/chargebee/models/model.rb', line 65

def self.construct(values, sub_types = {}, dependant_types = {})
  if(values != nil)
    obj = self.new(values, sub_types, dependant_types)
    obj.load(values)
    obj
  end
end

.uri_path(*paths) ⇒ Object



54
55
56
57
58
59
60
61
62
63
# File 'lib/chargebee/models/model.rb', line 54

def self.uri_path(*paths) 
  url = ""
  for path in paths
      if(path.nil? || path.strip.length < 1) 
         raise "Id is empty or nil" 
      end
      url = "#{url}/#{CGI.escape(path.strip).gsub("+","%20").gsub("%21","!").gsub("%24","$").gsub("%26","&").gsub("%27","'").gsub("%28","(").gsub("%29", ")").gsub("%2A","*").gsub("%2B","+").gsub("%2C",",").gsub("%2F", "/").gsub("%3A",":").gsub("%3B",";").gsub("%3D","=").gsub("%3F","?").gsub("%40","@").gsub("%5B","[").gsub("%5D","]")}"
  end
  return url
end

Instance Method Details

#init_dependant(obj, type, sub_types = {}) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/chargebee/models/model.rb', line 73

def init_dependant(obj, type, sub_types = {})
  instance_eval do
    if(obj[type] != nil)
      case obj
      when Hash
        if(@dependant_types[type] != nil)
          dependant_obj = @dependant_types[type].construct(obj[type], sub_types)
          instance_variable_set("@#{type}", dependant_obj)
        end
      end
    end
  end
end

#init_dependant_list(obj, type, sub_types = {}) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/chargebee/models/model.rb', line 87

def init_dependant_list(obj, type, sub_types = {})
  instance_eval do
    if(obj[type] != nil)
      case obj[type]
      when Array
        if(@dependant_types[type] != nil)
          set_val = obj[type].map {|dt| @dependant_types[type].construct(dt, sub_types)}
          instance_variable_set("@#{type}", set_val)
        end
      end
    end
  end
end

#inspectObject



17
18
19
# File 'lib/chargebee/models/model.rb', line 17

def inspect()
  "#<#{self.class}:0x#{self.object_id.to_s(16)} > JSON: " + JSON.pretty_generate(@values)
end

#load(values) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/chargebee/models/model.rb', line 21

def load(values)
  instance_eval do
    values.each do |k, v|
      set_val = nil
      next if v.is_a?(Hash) && @dependant_types[k] != nil

      set_val = if v.is_a?(Hash)
                  (@sub_types[k] != nil) ? @sub_types[k].construct(v) : v
                elsif v.is_a?(Array)
                  if @sub_types[k] != nil
                    v.map { |item| @sub_types[k].construct(item)}
                  else
                    v
                  end
                else
                  v
                end

      instance_variable_set("@#{k}", set_val)
    end
  end
end

#to_s(*args) ⇒ Object



13
14
15
# File 'lib/chargebee/models/model.rb', line 13

def to_s(*args) 
  JSON.pretty_generate(@values) 
end