Class: CTM::List

Inherits:
Object
  • Object
show all
Includes:
Enumerable, HTTParty
Defined in:
lib/ctm/list.rb

Direct Known Subclasses

MessageList, NumberList, ReceivingNumberList

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(list_type, options = {}, token = nil, fetched_objects = nil) ⇒ List

e.g. Account, token



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/ctm/list.rb', line 15

def initialize(list_type, options={}, token=nil, fetched_objects=nil)
  @list_type = list_type
  @list_token_type = list_type.underscore.pluralize
  @object_klass = CTM.module_eval(list_type)
  @token = token || CTM::Auth.token
  @account_id = options[:account_id]
  if @account_id
    @list_type_path = "accounts/#{@account_id}/#{@list_token_type}"
  else
    @list_type_path = @list_token_type
  end

  @fetched_objects = fetched_objects
  @options = options
  @filters = {}
  load_records
end

Instance Attribute Details

#filtersObject (readonly)

Returns the value of attribute filters.



12
13
14
# File 'lib/ctm/list.rb', line 12

def filters
  @filters
end

#list_typeObject (readonly)

Returns the value of attribute list_type.



9
10
11
# File 'lib/ctm/list.rb', line 9

def list_type
  @list_type
end

#objectsObject (readonly)

Returns the value of attribute objects.



9
10
11
# File 'lib/ctm/list.rb', line 9

def objects
  @objects
end

#pageObject

Returns the value of attribute page.



10
11
12
# File 'lib/ctm/list.rb', line 10

def page
  @page
end

#per_pageObject (readonly)

Returns the value of attribute per_page.



10
11
12
# File 'lib/ctm/list.rb', line 10

def per_page
  @per_page
end

#tokenObject (readonly)

Returns the value of attribute token.



9
10
11
# File 'lib/ctm/list.rb', line 9

def token
  @token
end

#total_entriesObject (readonly)

Returns the value of attribute total_entries.



9
10
11
# File 'lib/ctm/list.rb', line 9

def total_entries
  @total_entries
end

#total_pagesObject (readonly)

Returns the value of attribute total_pages.



10
11
12
# File 'lib/ctm/list.rb', line 10

def total_pages
  @total_pages
end

Instance Method Details

#create(options) ⇒ Object



58
59
60
61
62
63
# File 'lib/ctm/list.rb', line 58

def create(options)
  @object_klass.create(options.merge(list_type_path: @list_type_path,
                                     list_token_type: @list_token_type,
                                     account_id: @account_id,
                                     token: @token))
end

#eachObject



50
51
52
53
54
55
56
# File 'lib/ctm/list.rb', line 50

def each
  return to_enum(:each) unless block_given?
  load_records
  @objects.each do |obj|
    yield obj
  end
end

#each_pageObject



39
40
41
42
43
44
45
46
47
48
# File 'lib/ctm/list.rb', line 39

def each_page
  # Trick to allow .each_page.each_with_index from:
  # http://stackoverflow.com/a/18089712
  return to_enum(:each_page) unless block_given?

  1.upto(@total_pages) do |pnum|
    self.page = pnum
    yield self
  end
end

#find(options = {}) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/ctm/list.rb', line 65

def find(options = {})
  return self unless options.class == Hash

  first_name = options.delete(:first_name)
  last_name  = options.delete(:last_name)
  options[:filter] = options.delete(:filter) || "#{first_name} #{last_name}".strip if first_name || last_name

  @filters = options

  self.page = 1
  self
end

#get(recordid, options = {}) ⇒ Object



78
79
80
81
82
83
# File 'lib/ctm/list.rb', line 78

def get(recordid, options={})
  path_str = "/api/v1/#{@list_type_path}/#{recordid}.json"
  res = self.class.get(path_str, query: options.merge(auth_token: @token))
  data = res.parsed_response
  @object_klass.new(data, @token)
end