class Dagpi::HTTPClient

Custom HTTP Client

Attributes

token[R]

Public Class Methods

new(token) click to toggle source
# File lib/dagpirb/http.rb, line 20
def initialize(token)
  @token = token
end

Public Instance Methods

get_data(endpoint) click to toggle source

Method to get data from Data API endpoints. Returns: Hash

# 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) click to toggle source

Method to get image from Image API endpoints Returns: bytes

# 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