class Megam::JSONCompat

Constants

JSON_CLAZ
JSON_MAX_NESTING
MEGAM_ACCOUNT
MEGAM_ACCOUNTCOLLECTION
MEGAM_ADDONS
MEGAM_ADDONSCOLLECTION
MEGAM_ASSEMBLIES
MEGAM_ASSEMBLIESCOLLECTION
MEGAM_ASSEMBLY
MEGAM_ASSEMBLYCOLLECTION
MEGAM_BACKUPS
MEGAM_BACKUPSCOLLECTION
MEGAM_BALANCES
MEGAM_BALANCESCOLLECTION
MEGAM_BILLEDHISTORIES
MEGAM_BILLEDHISTORIESCOLLECTION
MEGAM_BILLINGTRANSACTIONS
MEGAM_BILLINGTRANSACTIONSCOLLECTION
MEGAM_COMPONENTS
MEGAM_COMPONENTSCOLLECTION
MEGAM_CREDITS
MEGAM_CREDITSCOLLECTION
MEGAM_DISKS
MEGAM_DISKSCOLLECTION
MEGAM_DOMAIN
MEGAM_DOMAINCOLLECTION
MEGAM_ERROR
MEGAM_EVENTSALL
MEGAM_EVENTSALLCOLLECTION
MEGAM_EVENTSBILLING
MEGAM_EVENTSBILLINGCOLLECTION
MEGAM_EVENTSCONTAINER
MEGAM_EVENTSCONTAINERCOLLECTION
MEGAM_EVENTSMARKETPLACE
MEGAM_EVENTSSTORAGE
MEGAM_EVENTSSTORAGECOLLECTION
MEGAM_EVENTSVM
MEGAM_EVENTSVMCOLLECTION
MEGAM_EventsMarketplaceCollection
MEGAM_FLAVORS
MEGAM_FLAVORSCOLLECTION
MEGAM_LICENSE
MEGAM_LICENSECOLLECTION
MEGAM_MARKETPLACE
MEGAM_MARKETPLACECOLLECTION
MEGAM_ORGANIZATION
MEGAM_ORGANIZATIONSCOLLECTION
MEGAM_PROMOS
MEGAM_QUOTAS
MEGAM_QUOTASCOLLECTION
MEGAM_RAWIMAGES
MEGAM_RAWIMAGESCOLLECTION
MEGAM_REPORTS
MEGAM_REPORTSCOLLECTION
MEGAM_REQUEST
MEGAM_REQUESTCOLLECTION
MEGAM_SENSORS
MEGAM_SENSORSCOLLECTION
MEGAM_SNAPSHOTS
MEGAM_SNAPSHOTSCOLLECTION
MEGAM_SSHKEY
MEGAM_SSHKEYCOLLECTION
MEGAM_SUBSCRIPTIONS
MEGAM_SUBSCRIPTIONSCOLLECTION

Public Class Methods

class_for_json_class(json_class) click to toggle source

Map JSON_CLAZ to a Class object. We use a case instead of a Hash assigned to a constant because otherwise this file could not be loaded until all the constants were defined, which means you’d have to load the world to get json.

# File lib/megam/core/json_compat.rb, line 157
def class_for_json_class(json_class)
    case json_class
    when MEGAM_ERROR
        Megam::Error
    when MEGAM_ACCOUNT
        Megam::Account
    when MEGAM_ACCOUNTCOLLECTION
        Megam::AccountCollection
    when MEGAM_ASSEMBLIES
        Megam::Assemblies
    when MEGAM_ASSEMBLIESCOLLECTION
        Megam::AssembliesCollection
    when MEGAM_ASSEMBLY
        Megam::Assembly
    when MEGAM_ASSEMBLYCOLLECTION
        Megam::AssemblyCollection
    when MEGAM_COMPONENTS
        Megam::Components
    when MEGAM_COMPONENTSCOLLECTION
        Megam::ComponentsCollection
    when MEGAM_REQUEST
        Megam::Request
    when MEGAM_REQUESTCOLLECTION
        Megam::RequestCollection
    when MEGAM_SSHKEY
        Megam::SshKey
    when MEGAM_SSHKEYCOLLECTION
        Megam::SshKeyCollection
    when MEGAM_EVENTSVM
        Megam::EventsVm
    when MEGAM_EVENTSVMCOLLECTION
        Megam::EventsVmCollection
    when MEGAM_EVENTSMARKETPLACE
        Megam::EventsMarketplace
    when MEGAM_EventsMarketplaceCollection
        Megam::EventsMarketplaceCollection
    when MEGAM_LICENSE
        Megam::License
    when MEGAM_LICENSECOLLECTION
        Megam::LicenseCollection
    when MEGAM_FLAVORS
        Megam::Flavors
    when MEGAM_FLAVORSCOLLECTION
        Megam::FlavorsCollection
    when MEGAM_EVENTSALL
        Megam::EventsAll
    when MEGAM_EVENTSALLCOLLECTION
        Megam::EventsAllCollection
    when MEGAM_EVENTSCONTAINER
        Megam::EventsContainer
    when MEGAM_EVENTSCONTAINERCOLLECTION
        Megam::EventsContainerCollection
    when MEGAM_EVENTSBILLING
        Megam::EventsBilling
    when MEGAM_EVENTSBILLINGCOLLECTION
        Megam::EventsBillingCollection
    when MEGAM_EVENTSSTORAGE
        Megam::EventsStorage
    when MEGAM_EVENTSSTORAGECOLLECTION
        Megam::EventsStorageCollection
    when MEGAM_MARKETPLACE
        Megam::MarketPlace
    when MEGAM_MARKETPLACECOLLECTION
        Megam::MarketPlaceCollection
    when MEGAM_ORGANIZATION
        Megam::Organizations
    when MEGAM_ORGANIZATIONSCOLLECTION
        Megam::OrganizationsCollection
    when MEGAM_DOMAIN
        Megam::Domains
    when MEGAM_DOMAINCOLLECTION
        Megam::DomainsCollection
    when MEGAM_SENSORS
        Megam::Sensors
    when MEGAM_SENSORSCOLLECTION
        Megam::SensorsCollection
    when MEGAM_SNAPSHOTS
        Megam::Snapshots
    when MEGAM_SNAPSHOTSCOLLECTION
        Megam::SnapshotsCollection
      when MEGAM_BACKUPS
          Megam::Backups
      when MEGAM_BACKUPSCOLLECTION
          Megam::BackupsCollection
    when MEGAM_BALANCES
        Megam::Balances
    when MEGAM_BALANCESCOLLECTION
        Megam::BalancesCollection
      when MEGAM_CREDITS
          Megam::Credits
      when MEGAM_CREDITSCOLLECTION
          Megam::CreditsCollection
    when MEGAM_BILLEDHISTORIES
        Megam::Billedhistories
    when MEGAM_BILLEDHISTORIESCOLLECTION
        Megam::BilledhistoriesCollection
    when MEGAM_BILLINGTRANSACTIONS
        Megam::Billingtransactions
    when MEGAM_BILLINGTRANSACTIONSCOLLECTION
        Megam::BillingtransactionsCollection
    when MEGAM_SUBSCRIPTIONS
        Megam::Subscriptions
    when MEGAM_SUBSCRIPTIONSCOLLECTION
        Megam::SubscriptionsCollection
    when MEGAM_DISKS
        Megam::Disks
    when MEGAM_DISKSCOLLECTION
        Megam::DisksCollection
    when MEGAM_ADDONS
        Megam::Addons
    when MEGAM_ADDONSCOLLECTION
        Megam::AddonsCollection
    when MEGAM_REPORTS
        Megam::Reports
    when MEGAM_REPORTSCOLLECTION
        Megam::ReportsCollection
    when MEGAM_QUOTAS
        Megam::Quotas
    when MEGAM_QUOTASCOLLECTION
        Megam::QuotasCollection
    when MEGAM_RAWIMAGES
         Megam::Rawimages
    when MEGAM_RAWIMAGESCOLLECTION
        Megam::RawimagesCollection
    when MEGAM_PROMOS
        Megam::Promos
    else
        fail JSON::ParserError, "Unsupported `json_class` type '#{json_class}'"
    end
end
from_json(source, opts = {}) click to toggle source

Just call the JSON gem’s parse method with a modified :max_nesting field

# File lib/megam/core/json_compat.rb, line 89
def from_json(source, opts = {})
    obj = parse(source, opts)

    # JSON gem requires top level object to be a Hash or Array (otherwise
    # you get the "must contain two octets" error). Yajl doesn't impose the
    # same limitation. For compatibility, we re-impose this condition.
    unless obj.kind_of?(Hash) || obj.kind_of?(Array)
        raise JSON::ParseError, "Top level JSON object must be a Hash or Array. (actual: #{obj.class})"
    end

    # The old default in the json gem (which we are mimicing because we
    # sadly rely on this misfeature) is to "create additions" i.e., convert
    # JSON objects into ruby objects. Explicit :create_additions => false
    # is required to turn it off.
    if opts[:create_additions].nil? || opts[:create_additions]
        map_to_rb_obj(obj)
    else
        obj
    end
end
map_hash_to_rb_obj(json_hash) click to toggle source
# File lib/megam/core/json_compat.rb, line 128
def map_hash_to_rb_obj(json_hash)
    json_hash.each do |key, value|
        json_hash[key] = map_to_rb_obj(value)
    end
    json_hash
end
map_to_rb_obj(json_obj) click to toggle source

Look at an object that’s a basic type (from json parse) and convert it to an instance of Chef classes if desired.

# File lib/megam/core/json_compat.rb, line 112
def map_to_rb_obj(json_obj)
    case json_obj
    when Hash
        mapped_hash = map_hash_to_rb_obj(json_obj)
        if json_obj.has_key?(JSON_CLAZ) && (class_to_inflate = class_for_json_class(json_obj[JSON_CLAZ]))
            class_to_inflate.json_create(mapped_hash)
        else
            mapped_hash
        end
    when Array
        json_obj.map { |e| map_to_rb_obj(e) }
    else
        json_obj
    end
end
parse(source, opts = {}) click to toggle source

API to use to avoid create_addtions

# File lib/megam/core/json_compat.rb, line 80
def parse(source, opts = {})
    begin
        FFI_Yajl::Parser.parse(source, opts)
    rescue FFI_Yajl::ParseError => e
        raise StandardError, e.message
    end
end
to_json(obj, opts = nil) click to toggle source
# File lib/megam/core/json_compat.rb, line 135
def to_json(obj, opts = nil)
    begin
        FFI_Yajl::Encoder.encode(obj, opts)
    rescue FFI_Yajl::EncodeError => e
        raise JSON::GeneratorError, e.message
    end
end
to_json_pretty(obj, opts = nil) click to toggle source
# File lib/megam/core/json_compat.rb, line 144
def to_json_pretty(obj, opts = nil)
    opts ||= {}
    options_map = {}
    options_map[:pretty] = true
    options_map[:indent] = opts[:indent] if opts.has_key?(:indent)
    to_json(obj, options_map).chomp
end