Sha256: b1093d69616c75ea3aaf7fa3bd800d14d279cc3ddf71da47c6e9de68f2d960c2

Contents?: true

Size: 1.42 KB

Versions: 2

Compression:

Stored size: 1.42 KB

Contents

module Tracksale
  class Campaign
    attr_accessor :name, :code, :score

    def schedule_dispatch(body)
      self.class.schedule_dispatch(code, body)
    end

    def self.schedule_dispatch(code, body)
      response = client.post('campaign/' + code.to_s + '/dispatch', body)

      return response if response.success?

      raise ArgumentError, response['error'] if response['error']
      raise response.response.error!
    end

    def self.find_by_name(name)
      campaign_found_by_name = raw_all.keep_if { |c| c['name'] == name }.first
      return nil if campaign_found_by_name.nil?
      create_from_response(campaign_found_by_name)
    end

    def self.find_by_code(code)
      campaign_found_by_code = client.get('campaign/' + code.to_s).first
      return nil if campaign_found_by_code.nil?
      create_from_response(campaign_found_by_code)
    end

    def self.all
      raw_all.map { |campaign| create_from_response(campaign) }
    end

    def self.create_from_response(raw_reponse)
      new.tap do |campaign|
        campaign.name = raw_reponse['name']
        campaign.code = raw_reponse['code']
        campaign.score = {
          detractors: raw_reponse['detractors'],
          passives: raw_reponse['passives'],
          promoters: raw_reponse['promoters']
        }
      end
    end

    def self.raw_all
      client.get('campaign')
    end

    def self.client
      Tracksale.configuration.client.new
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
tracksale-0.0.7 lib/tracksale/campaign.rb
tracksale-0.0.5 lib/tracksale/campaign.rb