Skip to content

Commit

Permalink
feat: return included response in collection
Browse files Browse the repository at this point in the history
  • Loading branch information
troyizzle committed Feb 24, 2023
1 parent e720ad3 commit a816739
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 6 deletions.
19 changes: 17 additions & 2 deletions lib/prizepicks/collection.rb
Original file line number Diff line number Diff line change
@@ -1,20 +1,35 @@
# frozen_string_literal: true
module PrizePicks
class Collection
attr_reader :data, :total_pages, :current_page
attr_reader :data, :included, :total_pages, :current_page

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

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

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
end
end
12 changes: 11 additions & 1 deletion test/prizepicks/api/endpoints/test_entries.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,24 @@ def test_entries
stub = stub_request(PrizePicks::Api::Endpoints::Entries::ENDPOINT,
response: stub_response(fixture: 'entries'))
client = init_client(email: 'fake@fake.com', password: 'fake123!',
stub: stub)
stub:)
resp = client.entries
assert_equal PrizePicks::Collection, resp.class
first_entry = resp.data.first
assert_equal PrizePicks::Entry, first_entry.class
assert_equal 2000, first_entry.amount_bet_cents
assert_equal '134928677', first_entry.data['id']
end

def test_entries_included
stub = stub_request(PrizePicks::Api::Endpoints::Entries::ENDPOINT,
response: stub_response(fixture: 'entries'))
client = init_client(email: 'fake@fake.com', password: 'fake123!',
stub:)
resp = client.entries
included = resp.included
assert_equal 2, included.count
end
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion test/prizepicks/api/endpoints/test_leagues.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def test_leagues_endpoint
def test_entries
stub = stub_request(PrizePicks::Api::Endpoints::Leagues::ENDPOINT,
response: stub_response(fixture: 'leagues'))
client = init_client(stub: stub)
client = init_client(stub:)
resp = client.leagues
assert_equal PrizePicks::Collection, resp.class
assert_equal PrizePicks::League, resp.data.first.class
Expand Down
2 changes: 1 addition & 1 deletion test/prizepicks/api/endpoints/test_projections.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def test_projections_endpoint
def test_projections
stub = stub_request(PrizePicks::Api::Endpoints::Projections::ENDPOINT,
response: stub_response(fixture: 'projections'))
client = init_client(stub: stub)
client = init_client(stub:)
resp = client.projections
assert_equal PrizePicks::Collection, resp.class
assert_equal PrizePicks::Projection, resp.data.first.class
Expand Down
2 changes: 1 addition & 1 deletion test/prizepicks/api/endpoints/test_sign_in.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def test_sign_in
},
})
client = init_client(email: 'fake@fake.com', password: 'fake123!',
stub: stub)
stub:)
resp = client.sign_in
assert_equal PrizePicks::User, resp.class
assert_equal 'example@example.com', resp.email
Expand Down

0 comments on commit a816739

Please sign in to comment.