pdga_api 0.1.1 → 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 4b5432a78e98dc550da5350ece5c4d2b451c971c3d1c3beb9f212b6873aee7a4
4
- data.tar.gz: '0935e3fee69e705e06a09e8ad0aad8ef99d2c9181e55b192f2fe251b3f0e4bb9'
3
+ metadata.gz: 35b7df8255cda304c73b1b758e8bfdae890255ccff47048e5064819588d39a66
4
+ data.tar.gz: 61ffb3de12198b1662e8af6c2acc91e92106b65ce8de717917db9589a55e3377
5
5
  SHA512:
6
- metadata.gz: fe7fb83b60d7209f1c8d51cdf257baad529536acee747d2d938d6a3356837715e78ba91fa04760929585e322d4906ab6043b629cee6f34c3c7d4280654b21ea5
7
- data.tar.gz: f1df1098a4fa046e02322a3f535efe16a0fe4717efa0b30a1478d8a8beb2f6f540c6aba3587a3c189165081e97078125e4d9345e2a402523bea542aabbbf7444
6
+ metadata.gz: 0d6e0500248d9fb1f00dab716e2b9932afe8d6118cecadffa2ba531b62eb7fa4acdd9b59e1243d3f4eb4eb56a8cc1706b4db912b7fb6cfe1ff23a7a8e2e82d8c
7
+ data.tar.gz: 273e1b051f53b6f86e33d7422e7baec267549a4f98f0ff4ad8b1ca05672d50204b0764673946170f6c061bed9a4b07811be39ea03925f9b30a561d585ce09832
data/.gitignore CHANGED
@@ -5,6 +5,4 @@
5
5
  /doc/
6
6
  /pkg/
7
7
  /spec/reports/
8
- /tmp/
9
- /secret.env
10
- /pdga_api.env
8
+ /tmp/
data/README.md CHANGED
@@ -18,9 +18,7 @@ Or install it yourself as:
18
18
  $ gem install pdga_api
19
19
  ## Starting steps
20
20
 
21
- Out of the box there are some ENV vars that are needed in order to make this gem work (also assumes you already have PDGA API access)
22
- First in the root directory there are 2 template .env files you can look at for setting up the ENV vars. You can run this with just a secret.env that contains your PDGA username and password
23
- If you run the login method it will write out a .env with additional information the API needs to make calls. (session id, session name, and token)
21
+ In order to use this you will need to initialize using your pdga username and password
24
22
 
25
23
  ## Usage
26
24
 
@@ -28,16 +26,16 @@ Expected responses can all be found here https://www.pdga.com/dev/api/rest/v1/au
28
26
 
29
27
  There are 4 types of actions that this gem allows you to perform.
30
28
  1. Authentication
31
- 1. login Pdga::Client.login({ username: ENV["USERNAME"], password: ENV["PASSWORD"] }) (Could pass the actual username/password)
32
- 2. connection status Pdga::Client.connect
33
- 3. logout Pdga::Client.logout
29
+ 1. Initialize `@client = Pdga.new(username: "username goes here", password: "password goes here")`
30
+ 2. connection status `@client.connect`
31
+ 3. logout `@client.logout`
34
32
  2. Player information which takes a hash of various search params that are listed on the PDGA site (default 10 returned with a max of 200)
35
- 1. player search Pdga::Client.players({ pdga_number: "15857" })
36
- 2. player statistics search Pdga::Client.player_statistics({ pdga_number: "15857", year: "2022" })
33
+ 1. player search `@client.players({ pdga_number: "15857" })`
34
+ 2. player statistics search `@client.player_statistics({ pdga_number: "15857", year: "2022" })`
37
35
  3. Event information which takes a hash of various search params that are listed on the PDGA site (default 10 returned with a max of 200)
38
- 1. event search Pdga::Client.events({ tournament_id: "47877" })
36
+ 1. event search `@client.events({ tournament_id: "47877" })`
39
37
  4. Course information which takes a hash of various search params that are listed on the PDGA site (default 10 returned with a max of 200)
40
- 1. course search Pdga::Client.courses({ course_id: "2146" })
38
+ 1. course search `@client.courses({ course_id: "2146" })`
41
39
 
42
40
  ## License
43
41
 
@@ -50,6 +48,4 @@ Everyone interacting in the PdgaApi project's codebases, issue trackers, chat ro
50
48
  ## Todos
51
49
 
52
50
  1. Mock the tests!
53
- 2. Figure out something better for the ENV vars
54
- 3. Add response error handling from the API
55
- 4. Refactor duplicated code in client.rb
51
+ 2. Make readme pretty and thorough
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Pdga
4
+ module Auth
5
+ def login(options)
6
+ response = build_request("post", "services/json/user/login", options)
7
+ save_session_information(response)
8
+ response
9
+ end
10
+
11
+ def connect
12
+ build_request("post", "services/json/system/connect")
13
+ end
14
+
15
+ def logout
16
+ build_request("post", "services/json/user/logout")
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Pdga
4
+ module Course
5
+ def courses(options)
6
+ build_request("get", "services/json/course?#{build_params(options)}")
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Pdga
4
+ module Event
5
+ def events(options)
6
+ build_request("get", "services/json/event?#{build_params(options)}")
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Pdga
4
+ module Player
5
+ def players(options)
6
+ build_request("get", "services/json/players?#{build_params(options)}")
7
+ end
8
+
9
+ def player_statistics(options)
10
+ build_request("get", "services/json/player-statistics?#{build_params(options)}")
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,46 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Pdga
4
+ module Request
5
+ API_ENDPOINT = "https://api.pdga.com"
6
+ attr_accessor :sessid, :session_name, :token
7
+
8
+ protected
9
+
10
+ def client
11
+ Faraday.new(API_ENDPOINT) do |client|
12
+ client.use Faraday::Response::RaiseError
13
+ client.request :url_encoded
14
+ client.adapter Faraday.default_adapter
15
+ client.headers["Cookie"] = "#{@session_name}=#{@sessid}"
16
+ client.headers["X-CSRF-Token"] = @token
17
+ end
18
+ end
19
+
20
+ def request(http_method:, endpoint:, body: {})
21
+ client.public_send(http_method, endpoint, body)
22
+ end
23
+
24
+ def build_params(options)
25
+ params = ""
26
+ options.each do |key, value|
27
+ params += "#{key}=#{value}&"
28
+ end
29
+ params.chomp!("&")
30
+ end
31
+
32
+ def build_request(http_method, endpoint, options = {})
33
+ JSON.parse(request(
34
+ http_method: http_method.to_sym,
35
+ endpoint: endpoint,
36
+ body: options
37
+ ).body)
38
+ end
39
+
40
+ def save_session_information(login_response)
41
+ @sessid = login_response["sessid"]
42
+ @session_name = login_response["session_name"]
43
+ @token = login_response["token"]
44
+ end
45
+ end
46
+ end
@@ -4,90 +4,29 @@ require "faraday"
4
4
  require "json"
5
5
  require "oj"
6
6
  require "dotenv"
7
+ require "pdga_api/api/auth"
8
+ require "pdga_api/api/request"
9
+ require "pdga_api/api/course"
10
+ require "pdga_api/api/player"
11
+ require "pdga_api/api/event"
7
12
 
8
13
  module Pdga
9
14
  # API Client for PDGA
10
15
  class Client
11
- API_ENDPOINT = "https://api.pdga.com"
16
+ attr_accessor :username, :password, :sessid, :session_name, :token
17
+ include Pdga::Auth
18
+ include Pdga::Request
19
+ include Pdga::Course
20
+ include Pdga::Player
21
+ include Pdga::Event
12
22
 
13
- def self.players(options)
14
- params = build_params(options)
15
- response = build_request("get", "services/json/players?#{params}")
16
- JSON.parse(response.body)
17
- end
18
-
19
- def self.player_statistics(options)
20
- params = build_params(options)
21
- response = build_request("get", "services/json/player-statistics?#{params}")
22
- JSON.parse(response.body)
23
- end
24
-
25
- def self.events(options)
26
- params = build_params(options)
27
- response = build_request("get", "services/json/event?#{params}")
28
- JSON.parse(response.body)
29
- end
30
-
31
- def self.courses(options)
32
- params = build_params(options)
33
- response = build_request("get", "services/json/course?#{params}")
34
- JSON.parse(response.body)
35
- end
36
-
37
- def self.login(options)
38
- response = build_request("post", "services/json/user/login", options)
39
- update_envs(JSON.parse(response.body))
40
- JSON.parse(response.body)
41
- end
42
-
43
- def self.connect
44
- response = build_request("post", "services/json/system/connect")
45
- JSON.parse(response.body)
46
- end
47
-
48
- def self.logout
49
- response = build_request("post", "services/json/user/logout")
50
- clear_env
51
- JSON.parse(response.body)
52
- end
53
-
54
- def self.client
55
- @client ||= Faraday.new(API_ENDPOINT) do |client|
56
- client.use Faraday::Response::RaiseError
57
- client.request :url_encoded
58
- client.adapter Faraday.default_adapter
59
- client.headers["Cookie"] = "#{ENV["SESSION_NAME"]}=#{ENV["SESSID"]}"
60
- client.headers["X-CSRF-Token"] = ENV["TOKEN"]
61
- end
62
- end
23
+ def initialize(options = {})
24
+ raise(ArgumentError, "username and password are required parameters") unless options.key?(:username) && options.key?(:password)
63
25
 
64
- def self.request(http_method:, endpoint:, body: {})
65
- client.public_send(http_method, endpoint, body)
66
- end
67
-
68
- def self.build_params(options)
69
- params = ""
70
- options.each do |key, value|
71
- params += "#{key}=#{value}&"
72
- end
73
- params.chomp!("&")
74
- end
75
-
76
- def self.build_request(http_method, endpoint, options = {})
77
- request(
78
- http_method: http_method.to_sym,
79
- endpoint: endpoint,
80
- body: options
81
- )
82
- end
26
+ @username = options[:username]
27
+ @password = options[:password]
83
28
 
84
- def self.update_envs(response)
85
- f = File.new("pdga_api.env", "w")
86
- f.write("SESSID=#{response["sessid"]}\nSESSION_NAME=#{response["session_name"]}\nTOKEN=#{response["token"]}")
87
- f.close
88
- ENV["SESSID"] = response["sessid"]
89
- ENV["SESSION_NAME"] = response["session_name"]
90
- ENV["TOKEN"] = response["token"]
29
+ login(username: @username, password: @password)
91
30
  end
92
31
  end
93
32
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module PdgaApi
4
- VERSION = "0.1.1"
4
+ VERSION = "0.2.1"
5
5
  end
data/lib/pdga_api.rb CHANGED
@@ -4,8 +4,30 @@ require_relative "pdga_api/version"
4
4
  require_relative "pdga_api/client"
5
5
  require "dotenv"
6
6
 
7
- module PdgaApi
8
- class Error < StandardError; end
9
- # Your code goes here...
10
- Dotenv.load("pdga_api.env", "secret.env")
7
+ module Pdga
8
+ class << self
9
+ attr_accessor :options
10
+ end
11
+ self.options = {}
12
+
13
+ def self.new(params = {})
14
+ Pdga::Client.new(params)
15
+ end
16
+
17
+ def self.configure(params = {})
18
+ raise(ArgumentError, "username and password are required parameters") unless params.key?(:username) && params.key?(:password)
19
+
20
+ options[:username] = params[:username]
21
+ options[:password] = params[:password]
22
+
23
+ login(username: @username, password: @password)
24
+ end
25
+
26
+ def self.config
27
+ options
28
+ end
29
+
30
+ def self.reset_config
31
+ self.options = {}
32
+ end
11
33
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pdga_api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Robert Elia
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-07-22 00:00:00.000000000 Z
11
+ date: 2022-07-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: minitest
@@ -125,11 +125,14 @@ files:
125
125
  - bin/console
126
126
  - bin/setup
127
127
  - lib/pdga_api.rb
128
+ - lib/pdga_api/api/auth.rb
129
+ - lib/pdga_api/api/course.rb
130
+ - lib/pdga_api/api/event.rb
131
+ - lib/pdga_api/api/player.rb
132
+ - lib/pdga_api/api/request.rb
128
133
  - lib/pdga_api/client.rb
129
134
  - lib/pdga_api/version.rb
130
- - pdga_api.env.template
131
135
  - pdga_api.gemspec
132
- - secret.env.template
133
136
  homepage: https://github.com/relia1/pdga_api
134
137
  licenses:
135
138
  - MIT
@@ -1,3 +0,0 @@
1
- SESSID=SESSID
2
- SESSION_NAME=SESSION_NAME
3
- TOKEN=TOKEN
data/secret.env.template DELETED
@@ -1,2 +0,0 @@
1
- USERNAME=USERNAME
2
- PASSWORD=PASSWORD