Class: DataValidation::List

Inherits:
Object
  • Object
show all
Defined in:
lib/data_validation/list.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api) ⇒ List

Returns a new instance of List.



54
55
56
57
# File 'lib/data_validation/list.rb', line 54

def initialize(api)
  @api = api
  @slug = nil
end

Instance Attribute Details

#slugObject (readonly)

Returns the value of attribute slug.



8
9
10
# File 'lib/data_validation/list.rb', line 8

def slug
  @slug
end

Class Method Details

.create(api, members) ⇒ Object



10
11
12
13
14
# File 'lib/data_validation/list.rb', line 10

def self.create(api, members)
  list = DataValidation::List.new(api)
  list.create(members)
  list
end

.get(api, slug) ⇒ Object



16
17
18
19
20
# File 'lib/data_validation/list.rb', line 16

def self.get(api, slug)
  list = DataValidation::List.new(api)
  list.get(slug)
  list
end

Instance Method Details

#create(members) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
# File 'lib/data_validation/list.rb', line 59

def create(members)
  data = members.map{|m| m + ",\n"}.join()
  response = @api.post("list/?header=false&email=0&metadata=false", data, { 'Content-Type' => 'text/csv' })
  if @api.valid_response?(response)
    raw = JSON.parse(response.body)
    raw_list = raw["list"][0]
    @slug = raw_list["slug"]
  else
    raise DataValidation::Error, "could not create list"
  end
end

#create_jobObject



22
23
24
# File 'lib/data_validation/list.rb', line 22

def create_job
  DataValidation::Job.create(@api, self)
end

#get(slug) ⇒ Object



71
72
73
# File 'lib/data_validation/list.rb', line 71

def get(slug)
  @slug = slug
end

#get_job(slug) ⇒ Object



26
27
28
# File 'lib/data_validation/list.rb', line 26

def get_job(slug)
  DataValidation::Job.get(@api, self, slug)
end

#membersObject



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/data_validation/list.rb', line 30

def members
  data = []
  response = @api.get("list/#{@slug}/export.csv")
  if @api.valid_response?(response)
    body = response.body
    CSV.parse(body) do |row|
      address = row[1]
      analysis = {}
      analysis['grade']     = row[2]
      analysis['click']     = row[3]
      analysis['open']      = row[4]
      analysis['hard']      = row[5]
      analysis['optout']    = row[6]
      analysis['complain']  = row[7]
      analysis['trap']      = row[8]
      analysis['deceased']  = row[9]
      data << DataValidation::Member.new(address, analysis)
    end
  else
    raise DataValidation::Error, "could not get members for list #{@slug}"
  end
  data
end