Class: Client

Inherits:
Struct
  • Object
show all
Extended by:
GluttonRatelimit
Defined in:
lib/client.rb

Constant Summary collapse

MUTEX =
Mutex.new
OUTPUT =
Mutex.new

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#stateObject

Returns the value of attribute state

Returns:

  • (Object)

    the current value of state



6
7
8
# File 'lib/client.rb', line 6

def state
  @state
end

#storeObject

Returns the value of attribute store

Returns:

  • (Object)

    the current value of store



6
7
8
# File 'lib/client.rb', line 6

def store
  @store
end

#tokenObject

Returns the value of attribute token

Returns:

  • (Object)

    the current value of token



6
7
8
# File 'lib/client.rb', line 6

def token
  @token
end

#verboseObject

Returns the value of attribute verbose

Returns:

  • (Object)

    the current value of verbose



6
7
8
# File 'lib/client.rb', line 6

def verbose
  @verbose
end

Class Method Details

.requests_per_second=(rps) ⇒ Object



11
12
13
14
15
16
# File 'lib/client.rb', line 11

def self.requests_per_second=(rps)
  rps = rps.to_r
  if rps > 0
    rate_limit :get, rps.numerator, rps.denominator
  end
end

Instance Method Details

#get(path, after: 0, page_size: 5000) ⇒ Object



48
49
50
# File 'lib/client.rb', line 48

def get(path, after: 0, page_size: 5000)
  connection.get("/api/2.0/#{path}", after: after, page_size: page_size).body
end

#output(object) ⇒ Object



42
43
44
45
46
# File 'lib/client.rb', line 42

def output(object)
  OUTPUT.synchronize do
    puts JSON.generate(object)
  end
end

#process(model, after = 0) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/client.rb', line 18

def process(model, after = 0)
  MUTEX.synchronize do
    self.state ||= Concurrent::Hash.new()
  end

  state['value'] ||= Concurrent::Hash.new()

  return unless model.path
  after = state['value'][model.stream].to_i

  records = get(model.path, after: after)

  if Array(records['data']).flatten.compact.any?
    records['data'].peach(50) do |record|
      model.new(record, self).records.flatten.each do |model_record|
        output model_record
      end
    end
    state['value'][model.stream] = records['version']['max']
  end

  output(type: :STATE, value: state)
end