class Majestic::Api::Client

Attributes

api_url[RW]
configuration[RW]
connection[RW]

Public Class Methods

new(configuration: ::Majestic::Api.configuration) click to toggle source
# File lib/majestic/api/client.rb, line 8
def initialize(configuration: ::Majestic::Api.configuration)
self.configuration    =   configuration
        
set_api_url
        set_connection
end

Public Instance Methods

execute_command(name, params: {}, options: {}) click to toggle source

This method will execute the specified command as an api request. 'name' is the name of the command you wish to execute, e.g. GetIndexItemInfo 'parameters' a hash containing the command parameters. 'timeout' specifies the amount of time to wait before aborting the transaction. This defaults to 5 seconds.

# File lib/majestic/api/client.rb, line 60
def execute_command(name, params: {}, options: {})
        params.merge!({
  app_api_key: self.configuration.api_key,
  cmd:         name
})

        self.execute_request(params: params, options: options)
end
execute_open_app_request(command_name, access_token, params: {}, options: {}) click to toggle source

This will execute the specified command as an OpenApp request. 'command_name' is the name of the command you wish to execute, e.g. GetIndexItemInfo 'parameters' a hash containing the command parameters. 'access_token' the token provided by the user to access their resources. 'timeout' specifies the amount of time to wait before aborting the transaction. This defaults to 5 seconds.

# File lib/majestic/api/client.rb, line 74
def execute_open_app_request(command_name, access_token, params: {}, options: {})
        params.merge!({
  accesstoken: access_token,
  cmd:         command_name,
  privatekey:  self.configuration.api_key
})

        self.execute_request(params: params, options: options)
end
execute_request(params: {}, options: {}) click to toggle source

'parameters' a hash containing the command parameters. 'options' a hash containing command/call options (timeout, proxy settings etc)

# File lib/majestic/api/client.rb, line 86
def execute_request(params: {}, options: {})
response        =   nil

log(:info, "[Majestic::Api::Client] - Sending API Request to Majestic SEO. Parameters: #{params.inspect}. Options: #{options.inspect}")

response        =   self.connection.get do |request|
  request.params                    =   params  if params && !params.empty?
  request.options                   =   options if options && !options.empty?
end

        return response
end
get_index_item_info(urls:, params: {}, options: {}) click to toggle source
# File lib/majestic/api/client.rb, line 41
def get_index_item_info(urls:, params: {}, options: {})
  request_parameters                    =   {}
  request_parameters[:datasource]       =   params.fetch(:data_source, "historic")
request_parameters[:items]            =   urls.size

urls.each_with_index do |url, index|
  request_parameters["item#{index}"]  =   url
end

response    =   self.execute_command("GetIndexItemInfo", params: request_parameters, options: options)
response    =   Majestic::Api::ItemInfoResponse.new(response)

return response
end
set_api_url(format = :json) click to toggle source
# File lib/majestic/api/client.rb, line 15
  def set_api_url(format = :json)
  self.api_url          =   case self.configuration.environment.to_sym
    when :sandbox     then "https://developer.majestic.com/api/#{format}"
    when :production  then "https://api.majestic.com/api/#{format}"
    else
      "https://developer.majestic.com/api/#{format}"
  end
end
set_connection(headers: {}) click to toggle source
# File lib/majestic/api/client.rb, line 24
def set_connection(headers: {})        
headers         =   {
  "User-Agent"    =>  self.configuration.user_agent,
  "Content-Type"  =>  "application/json"
}.merge(headers)

  self.connection = Faraday.new(url: self.api_url, ssl: {verify: false}) do |builder|
  builder.headers     =   headers
  
  builder.request  :url_encoded
  builder.request  :retry
  builder.response :json
  builder.response :logger if self.configuration.verbose
  builder.adapter  :net_http
end
end
verbose() click to toggle source
# File lib/majestic/api/client.rb, line 99
def verbose
  self.configuration.verbose
end