module DBots::API

Main API functions

Public Instance Methods

get(url, headers = {}) click to toggle source
# File lib/dbots/api.rb, line 12
def get(url, headers = {})
  headers['User-Agent'] = user_agent
  request(:get, url, headers)
end
parse_json(raw) click to toggle source

@!visibility private

# File lib/dbots/api.rb, line 37
def parse_json(raw)
  JSON.parse(raw)
rescue JSON::ParserError
  raw
end
post(url, body, headers = {}) click to toggle source
# File lib/dbots/api.rb, line 17
def post(url, body, headers = {})
  headers['User-Agent'] = user_agent
  request(:post, url, body, headers)
end
request(type, *attributes) click to toggle source

Request an endpoint easily

# File lib/dbots/api.rb, line 27
def request(type, *attributes)
  response = RestClient.send(type, *attributes)
  Response.new(response, parse_json(response.body))
rescue RestClient::Unauthorized
  raise DBots::Err::Unauthorized
rescue RestClient::InternalServerError
  raise DBots::Err::ServerFail
end
user_agent() click to toggle source
# File lib/dbots/api.rb, line 22
def user_agent
  "dbots (https://github.com/dbots-pkg/dbots.rb #{DBots::VERSION}) rest-client/#{RestClient::VERSION} #{RUBY_ENGINE}/#{RUBY_VERSION}p#{RUBY_PATCHLEVEL}"
end