Class: Majesticseo::Client
- Inherits:
-
Object
- Object
- Majesticseo::Client
- 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
-
#app_api_key ⇒ Object
readonly
Returns the value of attribute app_api_key.
-
#code ⇒ Object
Returns the value of attribute code.
-
#data_tables ⇒ Object
Returns the value of attribute data_tables.
-
#env ⇒ Object
readonly
Returns the value of attribute env.
-
#error_message ⇒ Object
Returns the value of attribute error_message.
-
#full_error ⇒ Object
Returns the value of attribute full_error.
-
#global_vars ⇒ Object
Returns the value of attribute global_vars.
-
#response ⇒ Object
readonly
Returns the value of attribute response.
-
#uri ⇒ Object
readonly
Returns the value of attribute uri.
Instance Method Summary collapse
- #build_url ⇒ Object
- #call(method, params) ⇒ Object
- #debug? ⇒ Boolean
-
#initialize(opts = {}) ⇒ Client
constructor
Initialize a Majestic SEO aPI client passing the following options: app_api_key: You Majesticseo API key, found at: www.Majesticseo.com/account/api environment (optional): Default to RAILS_ENV, RACK_ENV or default.
- #method_missing(m, *args) ⇒ Object
- #parse_data ⇒ Object
- #puts(msg) ⇒ Object
- #success? ⇒ Boolean
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_key ⇒ Object (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 |
#code ⇒ Object
Returns the value of attribute code.
23 24 25 |
# File 'lib/majesticseo/client.rb', line 23 def code @code end |
#data_tables ⇒ Object
Returns the value of attribute data_tables.
23 24 25 |
# File 'lib/majesticseo/client.rb', line 23 def data_tables @data_tables end |
#env ⇒ Object (readonly)
Returns the value of attribute env.
22 23 24 |
# File 'lib/majesticseo/client.rb', line 22 def env @env end |
#error_message ⇒ Object
Returns the value of attribute error_message.
23 24 25 |
# File 'lib/majesticseo/client.rb', line 23 def @error_message end |
#full_error ⇒ Object
Returns the value of attribute full_error.
23 24 25 |
# File 'lib/majesticseo/client.rb', line 23 def full_error @full_error end |
#global_vars ⇒ Object
Returns the value of attribute global_vars.
23 24 25 |
# File 'lib/majesticseo/client.rb', line 23 def global_vars @global_vars end |
#response ⇒ Object (readonly)
Returns the value of attribute response.
22 23 24 |
# File 'lib/majesticseo/client.rb', line 22 def response @response end |
#uri ⇒ Object (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_url ⇒ Object
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
110 111 112 |
# File 'lib/majesticseo/client.rb', line 110 def debug? @debug end |
#parse_data ⇒ Object
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
94 95 96 |
# File 'lib/majesticseo/client.rb', line 94 def success? code == "OK" and == "" end |