Sha256: f6f7f46f1fd81b37cf33e8cfa25cce61720624ebdd25a0ba6a340d2acbeb258c

Contents?: true

Size: 1.6 KB

Versions: 1

Compression:

Stored size: 1.6 KB

Contents

require 'json'

module Eucalyptus
  class Resource
    def self.api_path
      raise "You must implement #{__method__.to_s}"
    end

    def self.known_fields
      raise "You must implement #{__method__.to_s}"
    end

    def self.parent
      Account.all.last
    end

    def self.find(id, fields: [], graph: Eucalyptus.graph)
      fields.concat(self.known_fields).uniq

      response = graph.get_object(id, fields: fields)
      self.new(response)
    end

    def self.all(graph: Eucalyptus.graph, parent: self.parent, options: {})
      if options[:fields]
        options[:fields].concat(self.known_fields).uniq
      else
        options[:fields] = self.known_fields
      end

      response = graph.get_connection(parent.id, api_path, options)
      response.collect{ |res| self.new(res) }
    end


    def initialize(response)
      @response = Response.new(response)
    end

    def method_missing(method_sym, *args, &block)
      @response.send(method_sym)
    end

    def respond_to?(method_sym, include_private = false)
      @response.respond_to?(method_sym)
    end

    def update(graph: Eucalyptus.graph, fields:)
      graph.put_connections(self.id, "", fields)
    end

    def delete(graph: Eucalyptus.graph)
      graph.delete_object(self.id)
    end

    def ads(options={}) 
      Ad.all(parent: self, options: options)
    end

    def campaigns(options={}) 
      Campaign.all(parent: self, options: options)
    end

    def ad_sets(options={}) 
      AdSet.all(parent: self, options: options)
    end

    def insights(options={})
      Insight.all(parent: self, options: options)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
eucalyptus-0.2.13 lib/eucalyptus/resource.rb