Class: SmartyStreets::Batch

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/smartystreets_ruby_sdk/batch.rb

Overview

The Batch class is used to send up to 100 lookups at once

Constant Summary collapse

MAX_BATCH_SIZE =
100

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeBatch

Returns a new instance of Batch.



9
10
11
12
# File 'lib/smartystreets_ruby_sdk/batch.rb', line 9

def initialize
  @named_lookups = {}
  @all_lookups = []
end

Instance Attribute Details

#all_lookupsObject (readonly)

Returns the value of attribute all_lookups.



7
8
9
# File 'lib/smartystreets_ruby_sdk/batch.rb', line 7

def all_lookups
  @all_lookups
end

#named_lookupsObject (readonly)

Returns the value of attribute named_lookups.



7
8
9
# File 'lib/smartystreets_ruby_sdk/batch.rb', line 7

def named_lookups
  @named_lookups
end

Instance Method Details

#[](index) ⇒ Object



54
55
56
# File 'lib/smartystreets_ruby_sdk/batch.rb', line 54

def [](index)
  @all_lookups[index]
end

#add(lookup) ⇒ Object



14
15
16
17
18
19
20
21
22
23
# File 'lib/smartystreets_ruby_sdk/batch.rb', line 14

def add(lookup)
  return false if full?

  @all_lookups.push(lookup)

  return true if lookup.input_id.nil?

  @named_lookups[lookup.input_id] = lookup
  true
end

#clearObject



25
26
27
28
# File 'lib/smartystreets_ruby_sdk/batch.rb', line 25

def clear
  @named_lookups.clear
  @all_lookups.clear
end

#each(&block) ⇒ Object



50
51
52
# File 'lib/smartystreets_ruby_sdk/batch.rb', line 50

def each(&block)
  @all_lookups.each(&block)
end

#empty?Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/smartystreets_ruby_sdk/batch.rb', line 34

def empty?
  size.zero?
end

#full?Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/smartystreets_ruby_sdk/batch.rb', line 30

def full?
  size >= MAX_BATCH_SIZE
end

#get_by_index(index) ⇒ Object



46
47
48
# File 'lib/smartystreets_ruby_sdk/batch.rb', line 46

def get_by_index(index)
  @all_lookups[index]
end

#get_by_input_id(input_id) ⇒ Object



42
43
44
# File 'lib/smartystreets_ruby_sdk/batch.rb', line 42

def get_by_input_id(input_id)
  @named_lookups[input_id]
end

#sizeObject



38
39
40
# File 'lib/smartystreets_ruby_sdk/batch.rb', line 38

def size
  @all_lookups.length
end