Class: CTM::List
- Inherits:
-
Object
- Object
- CTM::List
- Includes:
- Enumerable, HTTParty
- Defined in:
- lib/ctm/list.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#filters ⇒ Object
readonly
Returns the value of attribute filters.
-
#list_type ⇒ Object
readonly
Returns the value of attribute list_type.
-
#objects ⇒ Object
readonly
Returns the value of attribute objects.
-
#page ⇒ Object
Returns the value of attribute page.
-
#per_page ⇒ Object
readonly
Returns the value of attribute per_page.
-
#token ⇒ Object
readonly
Returns the value of attribute token.
-
#total_entries ⇒ Object
readonly
Returns the value of attribute total_entries.
-
#total_pages ⇒ Object
readonly
Returns the value of attribute total_pages.
Instance Method Summary collapse
- #create(options) ⇒ Object
- #each ⇒ Object
- #each_page ⇒ Object
- #find(options = {}) ⇒ Object
- #get(recordid, options = {}) ⇒ Object
-
#initialize(list_type, options = {}, token = nil, fetched_objects = nil) ⇒ List
constructor
e.g.
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, ={}, 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 = [: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 = @filters = {} load_records end |
Instance Attribute Details
#filters ⇒ Object (readonly)
Returns the value of attribute filters.
12 13 14 |
# File 'lib/ctm/list.rb', line 12 def filters @filters end |
#list_type ⇒ Object (readonly)
Returns the value of attribute list_type.
9 10 11 |
# File 'lib/ctm/list.rb', line 9 def list_type @list_type end |
#objects ⇒ Object (readonly)
Returns the value of attribute objects.
9 10 11 |
# File 'lib/ctm/list.rb', line 9 def objects @objects end |
#page ⇒ Object
Returns the value of attribute page.
10 11 12 |
# File 'lib/ctm/list.rb', line 10 def page @page end |
#per_page ⇒ Object (readonly)
Returns the value of attribute per_page.
10 11 12 |
# File 'lib/ctm/list.rb', line 10 def per_page @per_page end |
#token ⇒ Object (readonly)
Returns the value of attribute token.
9 10 11 |
# File 'lib/ctm/list.rb', line 9 def token @token end |
#total_entries ⇒ Object (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_pages ⇒ Object (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() @object_klass.create(.merge(list_type_path: @list_type_path, list_token_type: @list_token_type, account_id: @account_id, token: @token)) end |
#each ⇒ Object
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_page ⇒ Object
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( = {}) return self unless .class == Hash first_name = .delete(:first_name) last_name = .delete(:last_name) [:filter] = .delete(:filter) || "#{first_name} #{last_name}".strip if first_name || last_name @filters = 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, ={}) path_str = "/api/v1/#{@list_type_path}/#{recordid}.json" res = self.class.get(path_str, query: .merge(auth_token: @token)) data = res.parsed_response @object_klass.new(data, @token) end |