Class: Majesticseo::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/majesticseo/client.rb

Overview

Please refer to the Majesticseo developer documentation for information regarding API methods: developer-support.Majesticseo.com/

=Example
  maj = Majesticseo::Client.new(:app_api_key => "BLAH")
  maj.get_subscription_info
  if maj.success?
    puts maj.global_vars.max_bulk_backlinks_check
    => 120
    puts maj.data_tables.first.rows.first.index_item_info_res_units
    => 99992
  else
    puts maj.response
    puts maj.error_message
  end

Constant Summary collapse

BASE_URI =
"http://%s.majesticseo.com/api_command"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Client

Initialize a Majestic SEO aPI client passing the following options:

app_api_key: You Majesticseo API key, found at: https://www.Majesticseo.com/account/api
environment (optional): Default to RAILS_ENV, RACK_ENV or default. Determines whether the client uses the sandbox or production API server


29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/majesticseo/client.rb', line 29

def initialize(opts = {})
  @app_api_key = opts.delete(:app_api_key)
  @debug = opts.fetch(:debug, true)

  if !@app_api_key || @app_api_key.empty?
    msg = "API key needs to be a valid Majestic SEO API key. See: "\
          "https://www.Majesticseo.com/account/api"

    raise Majesticseo::InvalidAPIKey.new(msg)
  end

  if opts[:environment]
    @env = opts.delete(:environment)
  elsif defined?(RAILS_ENV)
    @env = RAILS_ENV
  elsif defined?(RACK_ENV)
    @env = RACK_ENV
  else
    @env = "development"
  end
  @response = nil
  @data_tables = []
  @global_vars = nil
  @uri = URI.parse(build_url)

  puts "Started Majesticseo::Client in #{env} mode"
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, *args) ⇒ Object



84
85
86
# File 'lib/majesticseo/client.rb', line 84

def method_missing(m, *args)
  call(m.to_s.camelize, args[0])
end

Instance Attribute Details

#app_api_keyObject (readonly)

Returns the value of attribute app_api_key.



22
23
24
# File 'lib/majesticseo/client.rb', line 22

def app_api_key
  @app_api_key
end

#codeObject

Returns the value of attribute code.



23
24
25
# File 'lib/majesticseo/client.rb', line 23

def code
  @code
end

#data_tablesObject

Returns the value of attribute data_tables.



23
24
25
# File 'lib/majesticseo/client.rb', line 23

def data_tables
  @data_tables
end

#envObject (readonly)

Returns the value of attribute env.



22
23
24
# File 'lib/majesticseo/client.rb', line 22

def env
  @env
end

#error_messageObject

Returns the value of attribute error_message.



23
24
25
# File 'lib/majesticseo/client.rb', line 23

def error_message
  @error_message
end

#full_errorObject

Returns the value of attribute full_error.



23
24
25
# File 'lib/majesticseo/client.rb', line 23

def full_error
  @full_error
end

#global_varsObject

Returns the value of attribute global_vars.



23
24
25
# File 'lib/majesticseo/client.rb', line 23

def global_vars
  @global_vars
end

#responseObject (readonly)

Returns the value of attribute response.



22
23
24
# File 'lib/majesticseo/client.rb', line 22

def response
  @response
end

#uriObject (readonly)

Returns the value of attribute uri.



22
23
24
# File 'lib/majesticseo/client.rb', line 22

def uri
  @uri
end

Instance Method Details

#build_urlObject



98
99
100
101
102
103
104
# File 'lib/majesticseo/client.rb', line 98

def build_url
  subdomain = Hash.new("developer")
  subdomain[:production] = "enterprise"
  subdomain['production'] = "enterprise"

  BASE_URI % subdomain[env]
end

#call(method, params) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/majesticseo/client.rb', line 57

def call method, params
  params = {} unless params.is_a? Hash
  request_uri = uri.clone
  request_uri.query = params.merge({
    :app_api_key => app_api_key,
    :cmd => method
  }).to_param

  @response = Nokogiri::XML(open(request_uri))

  # Set response and global variable attributes
  if response.at("Result")
    response.at("Result").keys.each do |a|
      send("#{a.underscore}=".to_sym, response.at("Result").attr(a))
    end
  end

  @global_vars = GlobalVars.new
  if response.at("GlobalVars")
    response.at("GlobalVars").keys.each do |a|
      @global_vars.send("#{a.underscore}=".to_sym, response.at("GlobalVars").attr(a)) 
    end
  end

  parse_data if success?
end

#debug?Boolean

Returns:

  • (Boolean)


110
111
112
# File 'lib/majesticseo/client.rb', line 110

def debug?
  @debug
end

#parse_dataObject



88
89
90
91
92
# File 'lib/majesticseo/client.rb', line 88

def parse_data
  @data_tables = @response.search("DataTable").map do |table|
    DataTable.create_from_xml(table)
  end
end

#puts(msg) ⇒ Object



106
107
108
# File 'lib/majesticseo/client.rb', line 106

def puts(msg)
  Kernel.puts(msg) if debug?
end

#success?Boolean

Returns:

  • (Boolean)


94
95
96
# File 'lib/majesticseo/client.rb', line 94

def success?
  code == "OK" and error_message == ""
end