Class: Coolsms::Finder

Inherits:
Base
  • Object
show all
Defined in:
lib/coolsms/finder.rb

Instance Attribute Summary

Attributes inherited from Base

#attributes, #response, #retrieved

Instance Method Summary collapse

Methods inherited from Base

#[], #id, #refresh!

Constructor Details

#initialize(conditions_hash = {}) ⇒ Finder

Returns a new instance of Finder.



3
4
5
# File 'lib/coolsms/finder.rb', line 3

def initialize(conditions_hash = {})
  @attributes = conditions_hash.with_indifferent_access
end

Instance Method Details

#__duppObject



49
# File 'lib/coolsms/finder.rb', line 49

alias_method :__dupp, :dup

#apiObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/coolsms/finder.rb', line 11

def api
  @api = Coolsms::RestApi::Sent.new
  @api.count = attributes[:per]
  @api.page = attributes[:page]
  @api.rcpt = attributes[:recipient_number]
  @api.start = attributes[:accepted_from]
  @api.end = attributes[:accepted_to]
  @api.status = attributes[:status]
  @api.resultcode = attributes[:result_code]
  @api.notin_resultcode = attributes[:not_in_result_code]
  @api.mid = attributes[:message_id]
  @api.gid = attributes[:group_id]
  @api
end

#dataObject



36
# File 'lib/coolsms/finder.rb', line 36

def data; self[:data] end

#dupObject



50
51
52
53
54
55
# File 'lib/coolsms/finder.rb', line 50

def dup
  duplicated = __dupp
  duplicated.instance_variable_set(:@response, nil)
  duplicated.instance_variable_set(:@retrieved, nil)
  duplicated
end

#has_next?Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/coolsms/finder.rb', line 41

def has_next?
  (((page - 1) * list_count) + data.size) < total_count
end

#has_prev?Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/coolsms/finder.rb', line 45

def has_prev?
  page > 1
end

#list_countObject



37
# File 'lib/coolsms/finder.rb', line 37

def list_count; self[:list_count] end

#next_pageObject



57
58
59
60
61
# File 'lib/coolsms/finder.rb', line 57

def next_page
  finder = self.dup
  finder.page = page + 1
  finder
end

#pageObject



39
# File 'lib/coolsms/finder.rb', line 39

def page; self[:page] end

#page=(num) ⇒ Object



7
8
9
# File 'lib/coolsms/finder.rb', line 7

def page=(num)
  attributes[:page] = num
end

#retrieveObject



26
27
28
29
30
31
32
33
34
# File 'lib/coolsms/finder.rb', line 26

def retrieve
  @response = api.call
  attributes.update(@response.body.with_indifferent_access)
  attributes[:data].map! do |msg_hash|
    msg_hash[:id] = msg_hash.delete(:message_id)
    Coolsms::Message.new(msg_hash)
  end
  super
end

#total_countObject



38
# File 'lib/coolsms/finder.rb', line 38

def total_count; self[:total_count] end