Class: PubgRb::Api
- Inherits:
-
Object
- Object
- PubgRb::Api
- Defined in:
- lib/pubg_rb.rb
Instance Attribute Summary collapse
-
#api_key ⇒ Object
Returns the value of attribute api_key.
Instance Method Summary collapse
- #get(nickname = nil) ⇒ Object
- #get_json(nickname = nil) ⇒ Object
-
#initialize(api_key = nil) ⇒ Api
constructor
A new instance of Api.
Constructor Details
#initialize(api_key = nil) ⇒ Api
Returns a new instance of Api.
11 12 13 |
# File 'lib/pubg_rb.rb', line 11 def initialize (api_key = nil) @api_key = !api_key.nil? ? api_key : raise(ArgumentError, "Add an `api_key` from `pubgtracker.com`!") end |
Instance Attribute Details
#api_key ⇒ Object
Returns the value of attribute api_key.
10 11 12 |
# File 'lib/pubg_rb.rb', line 10 def api_key @api_key end |
Instance Method Details
#get(nickname = nil) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/pubg_rb.rb', line 25 def get (nickname = nil) nickname = !nickname.nil? ? nickname : raise(ArgumentError, " Nickname not defined: Add your pubg nickname") uri = URI.parse("https://pubgtracker.com/api/profile/pc/#{nickname}") http = Net::HTTP.new(uri.host, uri.port) http.use_ssl = true request = Net::HTTP::Get.new(uri.request_uri) request.add_field("TRN-Api-Key", @api_key) response = http.request(request) json = JSON.parse(response.body) Profile.new(json) end |
#get_json(nickname = nil) ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/pubg_rb.rb', line 14 def get_json(nickname = nil) nickname = !nickname.nil? ? nickname : raise(ArgumentError, " Nickname not defined: Add your pubg nickname") uri = URI.parse("https://pubgtracker.com/api/profile/pc/#{nickname}") http = Net::HTTP.new(uri.host, uri.port) http.use_ssl = true request = Net::HTTP::Get.new(uri.request_uri) request.add_field("TRN-Api-Key", @api_key) response = http.request(request) JSON.parse(response.body) end |