Class: Dagpi::HTTPClient

Inherits:
Object
  • Object
show all
Defined in:
lib/dagpirb/http.rb

Overview

Custom HTTP Client

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#tokenObject (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