class RedditApi::Query

Attributes

count[R]
endpoint[R]
offset_id[RW]
records[R]
resource_type[R]

Public Class Methods

new(args = {}) click to toggle source
# File lib/reddit_api/query.rb, line 6
def initialize(args = {})
  @count = args.fetch(:count)
  @endpoint = args.fetch(:endpoint, "")
  @offset_id = args.fetch(:offset, "")
  @records = args.fetch(:records, {})
  @resource_type = args.fetch(:resource, "")
end

Public Instance Methods

add_records(new_records) click to toggle source
# File lib/reddit_api/query.rb, line 14
def add_records(new_records)
  if count > 1
    add_multiple_records(new_records.compact)
  else
    add_single_record(new_records)
  end
end
capture_count() click to toggle source
# File lib/reddit_api/query.rb, line 26
def capture_count
  records.length
end
captured_records() click to toggle source
# File lib/reddit_api/query.rb, line 22
def captured_records
  records.values
end

Private Instance Methods

add_data_record(record) click to toggle source
# File lib/reddit_api/query.rb, line 42
def add_data_record(record)
  record_id = record["data"]["id"]
  records[record_id] = record
  update_offset_id(record_id)
end
add_multiple_records(new_records) click to toggle source
# File lib/reddit_api/query.rb, line 35
def add_multiple_records(new_records)
  new_records.each do |record|
    add_data_record(record)
    break if records.length == count
  end
end
add_single_record(record) click to toggle source
# File lib/reddit_api/query.rb, line 48
def add_single_record(record)
  record_id = record["id"]
  records[record_id] = record
  update_offset_id(record_id)
end
update_offset_id(record_id) click to toggle source
# File lib/reddit_api/query.rb, line 54
def update_offset_id(record_id)
  self.offset_id = record_id
end