Skip to content

Commit

Permalink
feat: add more endpoints
Browse files Browse the repository at this point in the history
* Add a new endpoint for `Projections`
* Add a new endpoint for `Leagues`
* Responses now return an object with the data mapped
* Add test coverage
  • Loading branch information
troyizzle committed Feb 20, 2023
1 parent eccb01e commit 75cff34
Show file tree
Hide file tree
Showing 31 changed files with 878 additions and 27 deletions.
41 changes: 41 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
name: release

on:
push:
branches:
- main

jobs:
release-please:
runs-on: ubuntu-latest
steps:
- uses: GoogleCloudPlatform/release-please-action@v2
id: release
with:
release-type: ruby
package-name: prizepicks
bump-minor-pre-major: true
version-file: "lib/prizepicks/version.rb"
# Checkout code if release was created
- uses: actions/checkout@v2
if: ${{ steps.release.outputs.release_created }}
# Setup ruby if a release was created
- uses: ruby/setup-ruby@v1
with:
ruby-version: 3.0.0
if: ${{ steps.release.outputs.release_created }}
# Bundle install
- run: bundle install
if: ${{ steps.release.outputs.release_created }}
# Publish
- name: publish gem
run: |
mkdir -p $HOME/.gem
touch $HOME/.gem/credentials
chmod 0600 $HOME/.gem/credentials
printf -- "---\n:rubygems_api_key: ${GEM_HOST_API_KEY}\n" > $HOME/.gem/credentials
gem build *.gemspec
gem push *.gem
env:
GEM_HOST_API_KEY: "${{secrets.RUBYGEMS_AUTH_TOKEN}}"
if: ${{ steps.release.outputs.release_created }}
29 changes: 19 additions & 10 deletions lib/prizepicks.rb
Original file line number Diff line number Diff line change
@@ -1,17 +1,26 @@
# frozen_string_literal: true

require 'prizepicks/version'
require 'prizepicks/config'

require 'faraday'
require 'faraday-cookie_jar'

require 'prizepicks/version'
# require 'prizepicks/config'
require_relative 'prizepicks/faraday/connection'
require_relative 'prizepicks/faraday/request'
require_relative 'prizepicks/api/endpoints'
require_relative 'prizepicks/client'
# require_relative 'prizepicks/client'

module PrizePicks
autoload :Client, 'prizepicks/client'
autoload :Collection, 'prizepicks/collection'
autoload :Config, 'prizepicks/config'
autoload :Error, 'prizepicks/error'

# Responses
autoload :Responses, 'prizepicks/api/responses'

# module Prizepicks
# class Error < StandardError; end
# # Your code goes here...
# end
# Object
autoload :Base, 'prizepicks/objects/base'
autoload :Entry, 'prizepicks/objects/entry'
autoload :League, 'prizepicks/objects/league'
autoload :Projection, 'prizepicks/objects/projection'
autoload :User, 'prizepicks/objects/user'
end
4 changes: 4 additions & 0 deletions lib/prizepicks/api/endpoints.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
# frozen_string_literal: true

require_relative 'endpoints/entries'
require_relative 'endpoints/leagues'
require_relative 'endpoints/projections'
require_relative 'endpoints/sign_in'

module PrizePicks
module Api
module Endpoints
include Entries
include Leagues
include Projections
include SignIn
end
end
Expand Down
8 changes: 7 additions & 1 deletion lib/prizepicks/api/endpoints/entries.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,14 @@ module PrizePicks
module Api
module Endpoints
module Entries
ENDPOINT = '/v1/entries'

def entries(options = {})
request(:get, 'v1/entries', options)
raise ArgumentError, 'email is required' unless @email
raise ArgumentError, 'password is required' unless @password

resp = request(:get, ENDPOINT, options)
Collection.from_response(resp, type: Entry)
end
end
end
Expand Down
15 changes: 15 additions & 0 deletions lib/prizepicks/api/endpoints/leagues.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# frozen_string_literal: true
module PrizePicks
module Api
module Endpoints
module Leagues
ENDPOINT = '/v1/leagues'

def leagues(options = {})
resp = request(:get, ENDPOINT, options)
Collection.from_response(resp, type: League)
end
end
end
end
end
15 changes: 15 additions & 0 deletions lib/prizepicks/api/endpoints/projections.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# frozen_string_literal: true
module PrizePicks
module Api
module Endpoints
module Projections
ENDPOINT = '/v1/projections'

def projections(options = {})
resp = request(:get, ENDPOINT, options)
Collection.from_response(resp, type: Projection)
end
end
end
end
end
7 changes: 6 additions & 1 deletion lib/prizepicks/api/endpoints/sign_in.rb
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
# frozen_string_literal: true

module PrizePicks
module Api
module Endpoints
module SignIn
ENDPOINT = '/users/sign_in'

def sign_in
request(:post, '/users/sign_in', {
resp = request(:post, ENDPOINT, {
user: {
email: @email,
password: @password,
},
})

User.new(resp)
end
end
end
Expand Down
23 changes: 23 additions & 0 deletions lib/prizepicks/api/responses/user.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# frozen_string_literal: true
module PrizePicks
module Api
module Responses
class User < OpenStruct
def initialize(attributes)
super to_obstruct(attributes)
end

def to_obstruct(_attributes)
case obj
when Hash
OpenStruct.new(obj.transform_values { |val| to_obstruct(val) })
when Array
obj.map { |o| to_obstruct(o) }
else
obj
end
end
end
end
end
end
6 changes: 4 additions & 2 deletions lib/prizepicks/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,16 @@ class Client
include Api::Endpoints

attr_accessor(*Config::ATTRIBUTES)
attr_reader :adapter

def initialize(options = {})
PrizePicks::Config::ATTRIBUTES.each do |key|
send("#{key}=", options.fetch(key, PrizePicks.config.send(key)))
end

raise ArgumentError, 'Missing :email' unless email
raise ArgumentError, 'Missing :password' unless password
# For testing
@stubs = options[:stubs] || nil
@adapter = options[:adapter] || nil
end
end
end
20 changes: 20 additions & 0 deletions lib/prizepicks/collection.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# frozen_string_literal: true
module PrizePicks
class Collection
attr_reader :data, :total_pages, :current_page

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

def initialize(data:, current_page:, total_pages:)
@data = data
@current_page = current_page
@total_pages = total_pages
end
end
end
4 changes: 4 additions & 0 deletions lib/prizepicks/error.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# frozen_string_literal: true
module PrizePicks
class Error < StandardError; end
end
2 changes: 1 addition & 1 deletion lib/prizepicks/faraday/connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def connection
faraday.response :json
faraday.use :cookie_jar
faraday.response :raise_error
faraday.adapter ::Faraday.default_adapter
faraday.adapter adapter, @stubs
end
end
end
Expand Down
26 changes: 26 additions & 0 deletions lib/prizepicks/objects/base.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# frozen_string_literal: true
require 'ostruct'

module PrizePicks
class Base < OpenStruct
def initialize(resp)
@resp = resp
super to_obstruct(resp.dig('data', 'attributes'))
end

def data
@resp['data']
end

def to_obstruct(obj)
case obj
when Hash
OpenStruct.new(obj.transform_values { |val| to_obstruct(val) })
when Array
obj.map { |o| to_obstruct(o) }
else
obj
end
end
end
end
5 changes: 5 additions & 0 deletions lib/prizepicks/objects/entry.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# frozen_string_literal: true
module PrizePicks
class Entry < Base
end
end
5 changes: 5 additions & 0 deletions lib/prizepicks/objects/league.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# frozen_string_literal: true
module PrizePicks
class League < Base
end
end
5 changes: 5 additions & 0 deletions lib/prizepicks/objects/projection.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# frozen_string_literal: true
module PrizePicks
class Projection < Base
end
end
5 changes: 5 additions & 0 deletions lib/prizepicks/objects/user.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# frozen_string_literal: true
module PrizePicks
class User < Base
end
end
2 changes: 1 addition & 1 deletion prizepicks.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Gem::Specification.new do |spec|

spec.metadata['homepage_uri'] = spec.homepage
spec.metadata['source_code_uri'] = spec.homepage
# spec.metadata["changelog_uri"] = "TODO: Put your gem's CHANGELOG.md URL here."
spec.metadata['changelog_uri'] = "#{spec.homepage}/blob/master/CHANGELOG.md"

# Specify which files should be added to the gem when it is released.
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
Expand Down
2 changes: 1 addition & 1 deletion sig/prizepicks.rbs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module Prizepicks
module PrizePicks
VERSION: String
# See the writing guide of rbs: https://github.com/ruby/rbs#guides
end

0 comments on commit 75cff34

Please sign in to comment.