Class: Dagpi::HTTPClient
- Inherits:
-
Object
- Object
- Dagpi::HTTPClient
- Defined in:
- lib/dagpirb/http.rb
Overview
Custom HTTP Client
Instance Attribute Summary collapse
-
#token ⇒ Object
readonly
Returns the value of attribute token.
Instance Method Summary collapse
-
#get_data(endpoint) ⇒ Object
Method to get data from Data API endpoints.
-
#get_image(endpoint, param) ⇒ Object
Method to get image from Image API endpoints Returns: bytes.
-
#initialize(token) ⇒ HTTPClient
constructor
A new instance of HTTPClient.
Constructor Details
#initialize(token) ⇒ HTTPClient
Returns a new instance of HTTPClient.
20 21 22 |
# File 'lib/dagpirb/http.rb', line 20 def initialize(token) @token = token end |
Instance Attribute Details
#token ⇒ Object (readonly)
Returns the value of attribute token.
19 20 21 |
# File 'lib/dagpirb/http.rb', line 19 def token @token end |
Instance Method Details
#get_data(endpoint) ⇒ Object
Method to get data from Data API endpoints. Returns: Hash
27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/dagpirb/http.rb', line 27 def get_data(endpoint) http = HTTP .headers("User-Agent" => "dagpi.rb v#{VERSION}", "Authorization" => @token) req = http.get("https://api.dagpi.xyz/data/#{endpoint}") if req.code == 200 JSON.parse(req.body) else parsed = JSON.parse(req.body) raise APIError.new(parsed["message"], req.code) end end |
#get_image(endpoint, param) ⇒ Object
Method to get image from Image API endpoints Returns: bytes
42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/dagpirb/http.rb', line 42 def get_image(endpoint, param) http = HTTP .headers("User-Agent" => "dagpi.rb v#{VERSION}", "Authorization" => @token) req = http.get("https://api.dagpi.xyz/image/#{endpoint}/?url=#{param}") if req.code == 200 req.body else parsed = JSON.parse(req.body) raise APIError.new(parsed["message"], req.code) end end |