Class: ChargeBee::Model

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

Direct Known Subclasses

Addon, Addon::TaxProvidersField, Addon::Tier, Address, AdvanceInvoiceSchedule, AdvanceInvoiceSchedule::FixedIntervalSchedule, AdvanceInvoiceSchedule::SpecificDatesSchedule, AttachedItem, Attribute, BusinessEntity, BusinessEntityTransfer, Card, Comment, Contact, ContractTerm, Coupon, Coupon::CouponConstraint, Coupon::ItemConstraint, Coupon::ItemConstraintCriteria, CouponCode, CouponSet, CreditNote, CreditNote::Allocation, CreditNote::BillingAddress, CreditNote::Discount, CreditNote::Einvoice, CreditNote::LineItem, CreditNote::LineItemDiscount, CreditNote::LineItemTax, CreditNote::LineItemTier, CreditNote::LinkedRefund, CreditNote::ShippingAddress, CreditNote::SiteDetailsAtCreation, CreditNote::Tax, CreditNote::TaxOrigin, CreditNoteEstimate, CreditNoteEstimate::Discount, CreditNoteEstimate::LineItem, CreditNoteEstimate::LineItemDiscount, CreditNoteEstimate::LineItemTax, CreditNoteEstimate::LineItemTier, CreditNoteEstimate::Tax, CsvTaxRule, Currency, Customer, Customer::Balance, Customer::BillingAddress, Customer::ChildAccountAccess, Customer::Contact, Customer::EntityIdentifier, Customer::ParentAccountAccess, Customer::PaymentMethod, Customer::ReferralUrl, Customer::Relationship, Customer::TaxProvidersField, CustomerEntitlement, DifferentialPrice, DifferentialPrice::ParentPeriod, DifferentialPrice::Tier, Discount, Download, Entitlement, EntitlementOverride, Estimate, Event, Event::Webhook, Export, Export::Download, Feature, Feature::Level, GatewayErrorDetail, Gift, Gift::GiftReceiver, Gift::GiftTimeline, Gift::Gifter, Hierarchy, HostedPage, ImpactedItem, ImpactedItem::Download, ImpactedItemPrice, ImpactedItemPrice::Download, ImpactedSubscription, ImpactedSubscription::Download, InAppSubscription, 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::SiteDetailsAtCreation, Invoice::StatementDescriptor, Invoice::Tax, Invoice::TaxOrigin, InvoiceEstimate, InvoiceEstimate::Discount, InvoiceEstimate::LineItem, InvoiceEstimate::LineItemDiscount, InvoiceEstimate::LineItemTax, InvoiceEstimate::LineItemTier, InvoiceEstimate::Tax, Item, Item::ApplicableItem, Item::BundleConfiguration, Item::BundleItem, ItemEntitlement, ItemFamily, ItemPrice, ItemPrice::AccountingDetail, ItemPrice::TaxDetail, ItemPrice::TaxProvidersField, ItemPrice::Tier, Metadata, NonSubscription, Order, Order::BillingAddress, Order::LineItemDiscount, Order::LineItemTax, Order::LinkedCreditNote, Order::OrderLineItem, Order::ResentOrder, Order::ShippingAddress, PaymentIntent, PaymentIntent::PaymentAttempt, PaymentReferenceNumber, PaymentSchedule, PaymentSchedule::ScheduleEntry, PaymentScheduleScheme, PaymentScheduleScheme::PreferredSchedule, PaymentSource, PaymentSource::AmazonPayment, PaymentSource::BankAccount, PaymentSource::BillingAddress, PaymentSource::Card, PaymentSource::CustVoucherSource, PaymentSource::KlarnaPayNow, PaymentSource::Mandate, PaymentSource::Paypal, PaymentSource::Upi, PaymentSource::Venmo, PaymentVoucher, PaymentVoucher::LinkedInvoice, Plan, Plan::ApplicableAddon, Plan::AttachedAddon, Plan::EventBasedAddon, Plan::TaxProvidersField, Plan::Tier, PortalSession, PortalSession::LinkedCustomer, PriceVariant, PriceVariant::Attribute, PricingPageSession, 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, Ramp, Ramp::CouponsToAdd, Ramp::DiscountsToAdd, Ramp::ItemTier, Ramp::ItemsToAdd, Ramp::ItemsToUpdate, Ramp::StatusTransitionReason, ResourceMigration, SiteMigrationDetail, Subscription, Subscription::Addon, Subscription::ChargedEventBasedAddon, Subscription::ChargedItem, Subscription::ContractTerm, Subscription::Coupon, Subscription::Discount, Subscription::EventBasedAddon, Subscription::ItemTier, Subscription::ReferralInfo, Subscription::ShippingAddress, Subscription::SubscriptionItem, SubscriptionEntitlement, SubscriptionEntitlement::Component, SubscriptionEstimate, SubscriptionEstimate::ContractTerm, SubscriptionEstimate::ShippingAddress, TaxWithheld, ThirdPartyPaymentMethod, TimeMachine, Token, Transaction, Transaction::GatewayErrorDetail, 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)


48
49
50
51
52
53
54
55
56
# File 'lib/chargebee/models/model.rb', line 48

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



69
70
71
72
73
74
75
# File 'lib/chargebee/models/model.rb', line 69

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



58
59
60
61
62
63
64
65
66
67
# File 'lib/chargebee/models/model.rb', line 58

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("%2F", "/")}"
  end
  return url
end

Instance Method Details

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



77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/chargebee/models/model.rb', line 77

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



91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/chargebee/models/model.rb', line 91

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("@#{replace_white_space_with_underscore(k)}", set_val)
    end
  end
end

#replace_white_space_with_underscore(s) ⇒ Object



44
45
46
# File 'lib/chargebee/models/model.rb', line 44

def replace_white_space_with_underscore(s)
  s.to_s.tr(" ", "_")
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