Class: MajesticSeo::Api::Client

Inherits:
Object
  • Object
show all
Includes:
Logger
Defined in:
lib/majestic_seo/api/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Logger

#log

Constructor Details

#initialize(options = {}) ⇒ Client

Returns a new instance of Client.



45
46
47
48
49
50
51
52
53
54
# File 'lib/majestic_seo/api/client.rb', line 45

def initialize(options = {})
	set_config

	@api_key            =   options.fetch(:api_key,     self.config.fetch("api_key", nil))
	@environment        =   options.fetch(:environment, self.config.fetch("environment", :sandbox)).to_sym
	@verbose            =   options.fetch(:verbose,     false)

	set_api_url
	set_connection
end

Instance Attribute Details

#api_keyObject

Returns the value of attribute api_key.



42
43
44
# File 'lib/majestic_seo/api/client.rb', line 42

def api_key
  @api_key
end

#api_urlObject

Returns the value of attribute api_url.



42
43
44
# File 'lib/majestic_seo/api/client.rb', line 42

def api_url
  @api_url
end

#configObject

Returns the value of attribute config.



42
43
44
# File 'lib/majestic_seo/api/client.rb', line 42

def config
  @config
end

#connectionObject

Returns the value of attribute connection.



42
43
44
# File 'lib/majestic_seo/api/client.rb', line 42

def connection
  @connection
end

#environmentObject

Returns the value of attribute environment.



42
43
44
# File 'lib/majestic_seo/api/client.rb', line 42

def environment
  @environment
end

#verboseObject

Returns the value of attribute verbose.



42
43
44
# File 'lib/majestic_seo/api/client.rb', line 42

def verbose
  @verbose
end

Instance Method Details

#execute_command(name, parameters = {}, options = {}) ⇒ Object

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.



120
121
122
123
# File 'lib/majestic_seo/api/client.rb', line 120

def execute_command(name, parameters = {}, options = {})
	request_parameters = parameters.merge({"app_api_key" => @api_key, "cmd" => name})
	self.execute_request(request_parameters, options)
end

#execute_open_app_request(command_name, access_token, parameters = {}, options = {}) ⇒ Object

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.



130
131
132
133
# File 'lib/majestic_seo/api/client.rb', line 130

def execute_open_app_request(command_name, access_token, parameters = {}, options = {})
	request_parameters = parameters.merge({"accesstoken" => access_token, "cmd" => command_name, "privatekey" => @api_key})
	self.execute_request(request_parameters, options)
end

#execute_request(parameters = {}, options = {}) ⇒ Object

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



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/majestic_seo/api/client.rb', line 137

def execute_request(parameters = {}, options = {})
   response = nil

   begin
     log(:info, "[MajesticSeo::Api::Client] - Sending API Request to Majestic SEO. Parameters: #{parameters.inspect}. Options: #{options.inspect}")
     response = @connection.get do |request|
       request.params    =   parameters  if (!parameters.empty?)
       request.options   =   options     if (!options.empty?)
     end
   rescue StandardError => e
     log(:error, "[MajesticSeo::Api::Client] - Error occurred while trying to perform API-call with parameters: #{parameters.inspect}. Error Class: #{e.class.name}. Error Message: #{e.message}. Stacktrace: #{e.backtrace.join("\n")}")
     response    =   nil
   end

	return response
end

#get_index_item_info(urls, parameters = {}, options = {}) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/majestic_seo/api/client.rb', line 83

def get_index_item_info(urls, parameters = {}, options = {})
  request_parameters                    =   {}
  request_parameters['datasource']      =   parameters.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", request_parameters, options)
   response    =   MajesticSeo::Api::ItemInfoResponse.new(response)

   return response
end


98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/majestic_seo/api/client.rb', line 98

def get_top_back_links(url, parameters = {}, options = {})
   request_parameters                                  =     {}
   request_parameters['datasource']                    =     parameters.fetch(:data_source, "historic")
   request_parameters['URL']                           =     url
   request_parameters["MaxSourceURLs"]                 =     parameters.fetch(:max_source_urls, 100)
   request_parameters["ShowDomainInfo"]                =     parameters.fetch(:show_domain_info, 0)
   request_parameters["GetUrlData"]                    =     parameters.fetch(:get_url_data, 1)
   request_parameters["GetSubDomainData"]              =     parameters.fetch(:get_sub_domain_data, 0)
   request_parameters["GetRootDomainData"]             =     parameters.fetch(:get_root_domain_data, 0)
   request_parameters["MaxSourceURLsPerRefDomain"]     =     parameters.fetch(:max_source_urls_per_ref_domain, -1)
   request_parameters["DebugForceQueue"]               =     parameters.fetch(:debug_force_queue, 0)

   response    =   self.execute_command("GetTopBackLinks", request_parameters, options)
   response    =   MajesticSeo::Api::TopBackLinksResponse.new(response)

   return response
end

#set_api_urlObject



65
66
67
68
69
70
71
72
# File 'lib/majestic_seo/api/client.rb', line 65

def set_api_url
  @api_url = case @environment.to_sym
    when :sandbox     then "http://developer.majesticseo.com/api_command"
    when :production  then "http://enterprise.majesticseo.com/api_command"
    else
      "http://developer.majesticseo.com/api_command"
  end
end

#set_configObject



56
57
58
59
60
61
62
63
# File 'lib/majestic_seo/api/client.rb', line 56

def set_config
  env           = (defined?(Rails) && Rails.respond_to?(:env)) ? Rails.env : (ENV["RACK_ENV"] || 'development')

  self.config   = YAML.load_file(File.join(Rails.root, "config/majestic_seo.yml"))[env] rescue nil
   self.config ||= YAML.load_file(File.join(File.dirname(__FILE__), "../../generators/templates/majestic_seo.yml"))[env] rescue nil
   self.config ||= YAML.load_file(File.join(File.dirname(__FILE__), "../../generators/templates/majestic_seo.template.yml"))[env] rescue nil
   self.config ||= {}
end

#set_connectionObject



74
75
76
77
78
79
80
81
# File 'lib/majestic_seo/api/client.rb', line 74

def set_connection
  @connection = Faraday.new(:url => @api_url, :ssl => {:verify => false}) do |builder|
     builder.request   :url_encoded
     builder.request   :retry
     builder.response  :logger if (@verbose)
     builder.adapter   :net_http
   end
end