Sha256: f7655d53e42867a8db43d03b221390c16d4618036b6a9a205e59fe97f605e4dc

Contents?: true

Size: 867 Bytes

Versions: 1

Compression:

Stored size: 867 Bytes

Contents

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

    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.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::Client.new
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
tracksale-0.0.1 lib/tracksale/campaign.rb