Class: BadASS::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/badass/client.rb

Overview

A client to periodically check for new toys.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(refresh_rate: 600) ⇒ Client

Creates a client to do API operations.

Parameters:

  • refresh_rate (Integer) (defaults to: 600)

    the rate at which the Client should refresh its list of toys.


5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/badass/client.rb', line 5

def initialize(refresh_rate: 600)
  Thread.new do
    @toys = []
    loop do
      page = 1
      toy_list = []
      loop do
        newtoys = JSON.parse(Net::HTTP.get(URI("https://bad-dragon.com/api/inventory-toys?price[min]=0&price[max]=300&noAccessories=false&cumtube=false&suctionCup=false&sort[field]=price&&sort[direction]=asc&page=#{page}&limit=60")))
        page += 1
        newtoys['toys'].each do |toy|
          toy_list << BadASS::Toy.new(toy)
        end
        break if page > newtoys['totalPages']
      end
      @toys = toy_list
      sleep(refresh_rate)
    end
  end
end

Instance Attribute Details

#toysArray<String> (readonly)

Returns the current toys on the BD API.

Returns:

  • (Array<String>)

    the current toys on the BD API


26
27
28
# File 'lib/badass/client.rb', line 26

def toys
  @toys
end