Class: MajesticSeo::Api::Response

Inherits:
Object
  • Object
show all
Defined in:
lib/majestic_seo/api/response.rb

Direct Known Subclasses

ItemInfoResponse, TopBackLinksResponse

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(response = nil) ⇒ Response

This method returns a new instance of the Response class. If one of the parameters are not provided, it will default to nil.



42
43
44
45
46
47
48
49
# File 'lib/majestic_seo/api/response.rb', line 42

def initialize(response = nil)
  @response           =   response
  @global_variables   =   {}
  @tables             =   {}
  @success            =   false

  parse_response
end

Instance Attribute Details

#codeObject

Returns the value of attribute code.



37
38
39
# File 'lib/majestic_seo/api/response.rb', line 37

def code
  @code
end

#error_messageObject

Returns the value of attribute error_message.



37
38
39
# File 'lib/majestic_seo/api/response.rb', line 37

def error_message
  @error_message
end

#full_errorObject

Returns the value of attribute full_error.



37
38
39
# File 'lib/majestic_seo/api/response.rb', line 37

def full_error
  @full_error
end

#global_variablesObject

Returns the value of attribute global_variables.



38
39
40
# File 'lib/majestic_seo/api/response.rb', line 38

def global_variables
  @global_variables
end

#itemsObject

Returns the value of attribute items.



38
39
40
# File 'lib/majestic_seo/api/response.rb', line 38

def items
  @items
end

#responseObject

Returns the value of attribute response.



36
37
38
# File 'lib/majestic_seo/api/response.rb', line 36

def response
  @response
end

#successObject

Returns the value of attribute success.



38
39
40
# File 'lib/majestic_seo/api/response.rb', line 38

def success
  @success
end

#table_keyObject

Returns the value of attribute table_key.



36
37
38
# File 'lib/majestic_seo/api/response.rb', line 36

def table_key
  @table_key
end

#tablesObject

Returns the value of attribute tables.



36
37
38
# File 'lib/majestic_seo/api/response.rb', line 36

def tables
  @tables
end

Instance Method Details

#parse_global_variablesObject



76
77
78
79
80
81
82
# File 'lib/majestic_seo/api/response.rb', line 76

def parse_global_variables
  vars = @response.at_xpath("//GlobalVars")

  vars.attributes.each do |key, attribute|
    @global_variables[key.underscore] = attribute.value
  end if (vars && vars.attributes.any?)
end

#parse_responseObject



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/majestic_seo/api/response.rb', line 51

def parse_response
  if (@response.is_a?(Nokogiri::XML::Document))
    @response = (@response && @response.root) ? @response.root : nil
  elsif (@response.is_a?(Faraday::Response))
    @response = (@response && @response.body) ? ::Nokogiri::XML(@response.body, nil, "utf-8").try(:root) : nil
  end

  if (@response)
    @response.attributes.each do |key, attribute|
      self.send("#{key.underscore}=", attribute.value)
    end

    @success  =   (@code && @code.downcase.eql?("ok"))

    if (success?)
      parse_global_variables
      parse_tables
    end
  end
end

#parse_table(table) ⇒ Object



104
105
106
107
# File 'lib/majestic_seo/api/response.rb', line 104

def parse_table(table)
  parsed_table                  =   MajesticSeo::Api::DataTable.new(table)
  @tables[parsed_table.name]    =   parsed_table
end

#parse_tablesObject



96
97
98
99
100
101
102
# File 'lib/majestic_seo/api/response.rb', line 96

def parse_tables
  tables = @response.xpath("//DataTables/DataTable")

  tables.each do |table|
    parse_table(table)
  end if (tables && tables.any?)
end

#stacktraceObject



84
85
86
# File 'lib/majestic_seo/api/response.rb', line 84

def stacktrace
  @full_error
end

#success?Boolean

Returns:

  • (Boolean)


72
73
74
# File 'lib/majestic_seo/api/response.rb', line 72

def success?
  @success
end