Class: PrizePicks::Collection

Inherits:
Object
  • Object
show all
Defined in:
lib/prizepicks/collection.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data:, included:, current_page:, total_pages:) ⇒ Collection

Returns a new instance of Collection.



15
16
17
18
19
20
# File 'lib/prizepicks/collection.rb', line 15

def initialize(data:, included:, current_page:, total_pages:)
  @data = data
  @included = included
  @current_page = current_page
  @total_pages = total_pages
end

Instance Attribute Details

#current_pageObject (readonly)

Returns the value of attribute current_page.



4
5
6
# File 'lib/prizepicks/collection.rb', line 4

def current_page
  @current_page
end

#dataObject (readonly)

Returns the value of attribute data.



4
5
6
# File 'lib/prizepicks/collection.rb', line 4

def data
  @data
end

#includedObject (readonly)

Returns the value of attribute included.



4
5
6
# File 'lib/prizepicks/collection.rb', line 4

def included
  @included
end

#total_pagesObject (readonly)

Returns the value of attribute total_pages.



4
5
6
# File 'lib/prizepicks/collection.rb', line 4

def total_pages
  @total_pages
end

Class Method Details

.from_response(response, type:) ⇒ Object



6
7
8
9
10
11
12
13
# File 'lib/prizepicks/collection.rb', line 6

def self.from_response(response, type:)
  new(
    data: response['data'].map { |attrs| type.new(attrs) },
    included: parse_included_data(response['included']),
    current_page: response.dig('meta', 'current_page'),
    total_pages: response.dig('meta', 'total_pages')
  )
end

.parse_included_data(included) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/prizepicks/collection.rb', line 22

def self.parse_included_data(included)
  included.map do |attrs|
    type = attrs['type'].capitalize
    class_name = "PrizePicks::#{type}"
    klass = if Object.const_defined?(class_name)
              Object.const_get(class_name)
            else
              PrizePicks::Base
            end
    klass.new(attrs)
  end
end