class AhrefsAPI::Base

Attributes

mode[RW]

Public Class Methods

new(api_token = ENV['AHREFS_TOKEN'], activate_debug = false) click to toggle source
# File lib/ahrefs_api/base.rb, line 11
def initialize(api_token = ENV['AHREFS_TOKEN'], activate_debug = false)
  @mode ||= :domain
  @token = api_token
  self.class.debug_output $stdout if activate_debug
  
  raise "No token: please provide a valid token for the AhrefsAPI" if @token.nil?
end

Public Instance Methods

get_json(target) click to toggle source

returns the endpoint data for the target in JSON format

# File lib/ahrefs_api/base.rb, line 21
def get_json(target)
  return get_data(target) if target.is_a? Hash
  get_data(target: target)
end

Protected Instance Methods

get_data(custom_query) click to toggle source

sets common & default options makes the actual call to the API endpoint

# File lib/ahrefs_api/base.rb, line 29
def get_data(custom_query)
  qopts = {
    token: token,
    from: from, 
    output: :json, 
    mode: mode,
    target: :'ahrefs.com'
  }.merge(custom_query)
  
  response = self.class.get('/', {query: qopts})
  
  raise "HTTP error: #{response.code}", response unless (200..299).include? response.code
  json = JSON.parse response.body
  unless json['error'].nil?
    error = json['error']
    raise "API error: #{error}" 
    
  end
  return json
end
token() click to toggle source

returns the API token

# File lib/ahrefs_api/base.rb, line 51
def token
  @token
end