majestic_seo_api 0.9.3
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +13 -0
- data/LICENSE.txt +50 -0
- data/README.markdown +69 -0
- data/Rakefile +44 -0
- data/VERSION +1 -0
- data/lib/generators/majestic_seo/majestic_seo_generator.rb +18 -0
- data/lib/generators/templates/majestic_seo.template.yml +13 -0
- data/lib/majestic_seo/api/client.rb +151 -0
- data/lib/majestic_seo/api/data_table.rb +90 -0
- data/lib/majestic_seo/api/item_info.rb +126 -0
- data/lib/majestic_seo/api/item_info_response.rb +58 -0
- data/lib/majestic_seo/api/logger.rb +11 -0
- data/lib/majestic_seo/api/response.rb +113 -0
- data/lib/majestic_seo/api/top_back_links_response.rb +44 -0
- data/lib/majestic_seo/extensions/string.rb +10 -0
- data/lib/majestic_seo/railtie.rb +12 -0
- data/lib/majestic_seo_api.rb +17 -0
- data/majestic_seo_api.gemspec +84 -0
- data/script/get_index_item_info.rb +137 -0
- data/script/get_top_backlinks.rb +140 -0
- data/script/open_app_get_index_item_info.rb +156 -0
- data/spec/majestic_seo/client_spec.rb +68 -0
- data/spec/majestic_seo/item_info_response_spec.rb +136 -0
- data/spec/majestic_seo/top_back_links_response_spec.rb +31 -0
- data/spec/spec_helper.rb +26 -0
- metadata +160 -0
data/Gemfile
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
source "http://rubygems.org"
|
2
|
+
|
3
|
+
gem 'nokogiri'
|
4
|
+
gem "faraday", "~> 0.7.6"
|
5
|
+
gem "agiley-faraday_middleware", "~> 0.8.3"
|
6
|
+
gem 'jruby-openssl', '~> 0.7', :platforms => :jruby
|
7
|
+
|
8
|
+
group :development, :test do
|
9
|
+
gem 'bundler'
|
10
|
+
gem 'jeweler'
|
11
|
+
gem 'rspec'
|
12
|
+
gem "mocha"
|
13
|
+
end
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
|
2
|
+
Version 0.9.3
|
3
|
+
|
4
|
+
Copyright (c) 2011, Majestic-12 Ltd
|
5
|
+
|
6
|
+
All rights reserved.
|
7
|
+
|
8
|
+
|
9
|
+
|
10
|
+
Redistribution and use in source and binary forms, with or without
|
11
|
+
|
12
|
+
modification, are permitted provided that the following conditions are met:
|
13
|
+
|
14
|
+
* Redistributions of source code must retain the above copyright
|
15
|
+
|
16
|
+
notice, this list of conditions and the following disclaimer.
|
17
|
+
|
18
|
+
* Redistributions in binary form must reproduce the above copyright
|
19
|
+
|
20
|
+
notice, this list of conditions and the following disclaimer in the
|
21
|
+
|
22
|
+
documentation and/or other materials provided with the distribution.
|
23
|
+
|
24
|
+
* Neither the name of the Majestic-12 Ltd nor the
|
25
|
+
|
26
|
+
names of its contributors may be used to endorse or promote products
|
27
|
+
|
28
|
+
derived from this software without specific prior written permission.
|
29
|
+
|
30
|
+
|
31
|
+
|
32
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
33
|
+
|
34
|
+
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
35
|
+
|
36
|
+
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
37
|
+
|
38
|
+
DISCLAIMED. IN NO EVENT SHALL Majestic-12 Ltd BE LIABLE FOR ANY
|
39
|
+
|
40
|
+
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
41
|
+
|
42
|
+
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
43
|
+
|
44
|
+
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
45
|
+
|
46
|
+
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
47
|
+
|
48
|
+
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
49
|
+
|
50
|
+
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
data/README.markdown
ADDED
@@ -0,0 +1,69 @@
|
|
1
|
+
# Majestic SEO Ruby API Client #
|
2
|
+
|
3
|
+
**This is currently under heavy development (first released 2012-01-27). Get back to me if you run into any bugs.**
|
4
|
+
|
5
|
+
This is a Ruby Api client/wrapper/connector intended to be used with [Majestic SEO's awesome API](http://developer-support.majesticseo.com/).
|
6
|
+
|
7
|
+
Majestic SEO currently offers a client/connector over at [Connector Downloads](http://developer-support.majesticseo.com/connectors/downloads/) but this connector wasn't suitable for me because:
|
8
|
+
|
9
|
+
* It wasn't gemified (thus requiring a bunch of hacking to incorporate it into every respective Rails-app)
|
10
|
+
* Didn't have a config file with environment specific client settings (api key, api environment). I need to be able to use different api environments depending on the app environment
|
11
|
+
* **Didn't work with JRuby** - this gem does (or well - on 1.6.6-head and 1.7-head).
|
12
|
+
* Lacked test/spec coverage. The current test coverage isn't the best, but it's still better than nothing.
|
13
|
+
|
14
|
+
It is based on the Majestic SEO connector but has pretty much been rewritten from scratch.
|
15
|
+
|
16
|
+
The original script/test-files included now reside in script/. All of them haven't been completely upgraded yet, but they will.
|
17
|
+
|
18
|
+
## Installation ##
|
19
|
+
Add to your Gemfile:
|
20
|
+
```
|
21
|
+
gem 'majestic_seo_api', :git => 'git://github.com/Agiley/Majestic-SEO-Api.git'
|
22
|
+
```
|
23
|
+
|
24
|
+
Generate config file:
|
25
|
+
```
|
26
|
+
rails generate majestic_seo
|
27
|
+
```
|
28
|
+
|
29
|
+
## Tested on ##
|
30
|
+
The specs pass on:
|
31
|
+
|
32
|
+
* Ruby 1.9.2
|
33
|
+
* Ruby 1.9.3
|
34
|
+
* JRuby 1.6.6-head
|
35
|
+
* JRuby 1.7-head
|
36
|
+
|
37
|
+
*JRuby 1.6.5/1.6.5.1 does not work :(*
|
38
|
+
|
39
|
+
## License ##
|
40
|
+
Original Majestic SEO License:
|
41
|
+
|
42
|
+
---------
|
43
|
+
|
44
|
+
Version 0.9.3
|
45
|
+
|
46
|
+
Copyright (c) 2011, Majestic-12 Ltd
|
47
|
+
|
48
|
+
All rights reserved.
|
49
|
+
|
50
|
+
Redistribution and use in source and binary forms, with or without
|
51
|
+
modification, are permitted provided that the following conditions are met:
|
52
|
+
|
53
|
+
* Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
|
54
|
+
* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
|
55
|
+
* Neither the name of the Majestic-12 Ltd nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
|
56
|
+
|
57
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
58
|
+
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
59
|
+
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
60
|
+
DISCLAIMED. IN NO EVENT SHALL Majestic-12 Ltd BE LIABLE FOR ANY
|
61
|
+
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
62
|
+
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
63
|
+
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
64
|
+
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
65
|
+
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
66
|
+
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
67
|
+
|
68
|
+
---------
|
69
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
begin
|
2
|
+
require 'bundler'
|
3
|
+
Bundler::GemHelper.install_tasks
|
4
|
+
|
5
|
+
rescue LoadError
|
6
|
+
puts "Bundler not available. Install it with: gem install bundler"
|
7
|
+
end
|
8
|
+
|
9
|
+
begin
|
10
|
+
# Rspec 2.0
|
11
|
+
require 'rspec/core/rake_task'
|
12
|
+
|
13
|
+
desc 'Default: run specs'
|
14
|
+
task :default => :spec
|
15
|
+
RSpec::Core::RakeTask.new do |t|
|
16
|
+
t.pattern = "spec/**/*_spec.rb"
|
17
|
+
end
|
18
|
+
|
19
|
+
RSpec::Core::RakeTask.new('rcov') do |t|
|
20
|
+
t.pattern = "spec/**/*_spec.rb"
|
21
|
+
t.rcov = true
|
22
|
+
t.rcov_opts = ['--exclude', 'spec']
|
23
|
+
end
|
24
|
+
|
25
|
+
rescue LoadError
|
26
|
+
puts "Rspec not available. Install it with: gem install rspec"
|
27
|
+
end
|
28
|
+
|
29
|
+
begin
|
30
|
+
require 'jeweler'
|
31
|
+
|
32
|
+
Jeweler::Tasks.new do |gemspec|
|
33
|
+
gemspec.name = "majestic_seo_api"
|
34
|
+
gemspec.summary = "Interface for communicating with Majestic SEO's API"
|
35
|
+
gemspec.description = "Interface for communicating with Majestic SEO's API"
|
36
|
+
gemspec.email = "sebastian@agiley.se"
|
37
|
+
gemspec.homepage = "http://developer-support.majesticseo.com/connectors/"
|
38
|
+
gemspec.authors = ["Majestic-12 Ltd", "Sebastian Johnsson"]
|
39
|
+
end
|
40
|
+
|
41
|
+
Jeweler::GemcutterTasks.new
|
42
|
+
rescue LoadError
|
43
|
+
puts "Jeweler not available. Install it with: gem install jeweler"
|
44
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.9.3
|
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'generators/helpers/file_helper'
|
2
|
+
|
3
|
+
module MajesticSeo
|
4
|
+
module Generators
|
5
|
+
class MajesticSeoGenerator < Rails::Generators::Base
|
6
|
+
namespace "majestic_seo"
|
7
|
+
source_root File.expand_path("../../templates", __FILE__)
|
8
|
+
|
9
|
+
desc "Copies majestic_seo.yml to the Rails app's config directory."
|
10
|
+
|
11
|
+
def copy_config
|
12
|
+
template "majestic_seo.template.yml", "config/majestic_seo.yml"
|
13
|
+
end
|
14
|
+
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
@@ -0,0 +1,151 @@
|
|
1
|
+
|
2
|
+
=begin
|
3
|
+
|
4
|
+
Version 0.9.3
|
5
|
+
|
6
|
+
Copyright (c) 2011, Majestic-12 Ltd
|
7
|
+
All rights reserved.
|
8
|
+
|
9
|
+
Redistribution and use in source and binary forms, with or without
|
10
|
+
modification, are permitted provided that the following conditions are met:
|
11
|
+
1. Redistributions of source code must retain the above copyright
|
12
|
+
notice, this list of conditions and the following disclaimer.
|
13
|
+
2. Redistributions in binary form must reproduce the above copyright
|
14
|
+
notice, this list of conditions and the following disclaimer in the
|
15
|
+
documentation and/or other materials provided with the distribution.
|
16
|
+
3. Neither the name of the Majestic-12 Ltd nor the
|
17
|
+
names of its contributors may be used to endorse or promote products
|
18
|
+
derived from this software without specific prior written permission.
|
19
|
+
|
20
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
21
|
+
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
22
|
+
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
23
|
+
DISCLAIMED. IN NO EVENT SHALL Majestic-12 Ltd BE LIABLE FOR ANY
|
24
|
+
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
25
|
+
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
26
|
+
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
27
|
+
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
28
|
+
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
29
|
+
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
30
|
+
|
31
|
+
=end
|
32
|
+
|
33
|
+
require 'uri'
|
34
|
+
require 'cgi'
|
35
|
+
require 'rubygems'
|
36
|
+
require 'faraday_middleware'
|
37
|
+
|
38
|
+
module MajesticSeo
|
39
|
+
module Api
|
40
|
+
|
41
|
+
class Client
|
42
|
+
attr_accessor :connection, :api_url, :config, :api_key, :environment
|
43
|
+
include MajesticSeo::Api::Logger
|
44
|
+
|
45
|
+
def initialize(api_key = nil, environment = nil)
|
46
|
+
set_config
|
47
|
+
|
48
|
+
@api_key = api_key || self.config.fetch("api_key", nil)
|
49
|
+
@environment = environment || self.config.fetch("environment", :sandbox)
|
50
|
+
@environment = @environment.to_sym
|
51
|
+
|
52
|
+
set_api_url
|
53
|
+
set_connection
|
54
|
+
end
|
55
|
+
|
56
|
+
def set_config
|
57
|
+
rails_env = defined?(Rails) ? Rails.env : "development"
|
58
|
+
self.config = YAML.load_file(File.join(Rails.root, "config/majestic_seo.yml"))[rails_env] rescue nil
|
59
|
+
self.config ||= YAML.load_file(File.join(File.dirname(__FILE__), "../../generators/templates/majestic_seo.yml"))[rails_env] rescue nil
|
60
|
+
self.config ||= YAML.load_file(File.join(File.dirname(__FILE__), "../../generators/templates/majestic_seo.template.yml"))[rails_env] rescue nil
|
61
|
+
self.config ||= {}
|
62
|
+
end
|
63
|
+
|
64
|
+
def set_api_url
|
65
|
+
@api_url = case @environment.to_sym
|
66
|
+
when :sandbox then "http://developer.majesticseo.com/api_command"
|
67
|
+
when :production then "http://enterprise.majesticseo.com/api_command"
|
68
|
+
else
|
69
|
+
"http://developer.majesticseo.com/api_command"
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
def set_connection
|
74
|
+
@connection = Faraday.new(:url => @api_url) do |builder|
|
75
|
+
builder.request :url_encoded
|
76
|
+
#builder.response :logger
|
77
|
+
builder.use FaradayMiddleware::ParseNokogiriXml
|
78
|
+
builder.adapter :net_http
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
def get_index_item_info(urls, parameters = {})
|
83
|
+
request_parameters = {}
|
84
|
+
request_parameters['datasource'] = parameters.fetch(:data_source, "historic")
|
85
|
+
request_parameters["items"] = urls.size
|
86
|
+
|
87
|
+
urls.each_with_index do |url, index|
|
88
|
+
request_parameters["item#{index}"] = url
|
89
|
+
end
|
90
|
+
|
91
|
+
timeout = parameters.fetch(:timeout, 5)
|
92
|
+
response = self.execute_command("GetIndexItemInfo", request_parameters, timeout)
|
93
|
+
response = MajesticSeo::Api::ItemInfoResponse.new(response)
|
94
|
+
|
95
|
+
return response
|
96
|
+
end
|
97
|
+
|
98
|
+
def get_top_back_links(url, parameters = {})
|
99
|
+
request_parameters = {}
|
100
|
+
request_parameters['datasource'] = parameters.fetch(:data_source, "historic")
|
101
|
+
request_parameters['URL'] = url
|
102
|
+
request_parameters["MaxSourceURLs"] = parameters.fetch(:max_source_urls, 100)
|
103
|
+
request_parameters["ShowDomainInfo"] = parameters.fetch(:show_domain_info, 0)
|
104
|
+
request_parameters["GetUrlData"] = parameters.fetch(:get_url_data, 1)
|
105
|
+
request_parameters["GetSubDomainData"] = parameters.fetch(:get_sub_domain_data, 0)
|
106
|
+
request_parameters["GetRootDomainData"] = parameters.fetch(:get_root_domain_data, 0)
|
107
|
+
request_parameters["MaxSourceURLsPerRefDomain"] = parameters.fetch(:max_source_urls_per_ref_domain, -1)
|
108
|
+
request_parameters["DebugForceQueue"] = parameters.fetch(:debug_force_queue, 0)
|
109
|
+
|
110
|
+
timeout = parameters.fetch(:timeout, 5)
|
111
|
+
response = self.execute_command("GetTopBackLinks", request_parameters, timeout)
|
112
|
+
response = MajesticSeo::Api::TopBackLinksResponse.new(response)
|
113
|
+
|
114
|
+
return response
|
115
|
+
end
|
116
|
+
|
117
|
+
# This method will execute the specified command as an api request.
|
118
|
+
# 'name' is the name of the command you wish to execute, e.g. GetIndexItemInfo
|
119
|
+
# 'parameters' a hash containing the command parameters.
|
120
|
+
# 'timeout' specifies the amount of time to wait before aborting the transaction. This defaults to 5 seconds.
|
121
|
+
def execute_command(name, parameters, timeout = 5)
|
122
|
+
query_parameters = parameters.merge({"app_api_key" => @api_key, "cmd" => name})
|
123
|
+
self.execute_request(query_parameters, timeout)
|
124
|
+
end
|
125
|
+
|
126
|
+
# This will execute the specified command as an OpenApp request.
|
127
|
+
# 'command_name' is the name of the command you wish to execute, e.g. GetIndexItemInfo
|
128
|
+
# 'parameters' a hash containing the command parameters.
|
129
|
+
# 'access_token' the token provided by the user to access their resources.
|
130
|
+
# 'timeout' specifies the amount of time to wait before aborting the transaction. This defaults to 5 seconds.
|
131
|
+
def execute_open_app_request(command_name, parameters, access_token, timeout = 5)
|
132
|
+
query_parameters = parameters.merge({"accesstoken" => access_token, "cmd" => command_name, "privatekey" => @api_key})
|
133
|
+
self.execute_request(query_parameters, timeout)
|
134
|
+
end
|
135
|
+
|
136
|
+
# 'parameters' a hash containing the command parameters.
|
137
|
+
# 'timeout' specifies the amount of time to wait before aborting the transaction. This defaults to 5 seconds.
|
138
|
+
def execute_request(parameters, timeout = 5)
|
139
|
+
response = @connection.get do |request|
|
140
|
+
request.params = parameters
|
141
|
+
request.options = {:timeout => timeout}
|
142
|
+
end
|
143
|
+
|
144
|
+
return response
|
145
|
+
end
|
146
|
+
|
147
|
+
end
|
148
|
+
|
149
|
+
end
|
150
|
+
end
|
151
|
+
|
@@ -0,0 +1,90 @@
|
|
1
|
+
|
2
|
+
=begin
|
3
|
+
|
4
|
+
Version 0.9.3
|
5
|
+
|
6
|
+
Copyright (c) 2011, Majestic-12 Ltd
|
7
|
+
All rights reserved.
|
8
|
+
|
9
|
+
Redistribution and use in source and binary forms, with or without
|
10
|
+
modification, are permitted provided that the following conditions are met:
|
11
|
+
1. Redistributions of source code must retain the above copyright
|
12
|
+
notice, this list of conditions and the following disclaimer.
|
13
|
+
2. Redistributions in binary form must reproduce the above copyright
|
14
|
+
notice, this list of conditions and the following disclaimer in the
|
15
|
+
documentation and/or other materials provided with the distribution.
|
16
|
+
3. Neither the name of the Majestic-12 Ltd nor the
|
17
|
+
names of its contributors may be used to endorse or promote products
|
18
|
+
derived from this software without specific prior written permission.
|
19
|
+
|
20
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
21
|
+
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
22
|
+
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
23
|
+
DISCLAIMED. IN NO EVENT SHALL Majestic-12 Ltd BE LIABLE FOR ANY
|
24
|
+
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
25
|
+
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
26
|
+
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
27
|
+
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
28
|
+
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
29
|
+
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
30
|
+
|
31
|
+
=end
|
32
|
+
|
33
|
+
module MajesticSeo
|
34
|
+
module Api
|
35
|
+
class DataTable
|
36
|
+
attr_accessor :node, :name, :row_count, :headers, :rows
|
37
|
+
|
38
|
+
def initialize(node)
|
39
|
+
@node = node
|
40
|
+
|
41
|
+
if (@node)
|
42
|
+
@name = @node["Name"] || nil
|
43
|
+
@row_count = @node["RowsCount"] || 0
|
44
|
+
@headers = @node["Headers"] || []
|
45
|
+
|
46
|
+
@rows = []
|
47
|
+
parse
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
def parse
|
52
|
+
rows = @node.xpath("Row")
|
53
|
+
|
54
|
+
if (@headers && rows && rows.any?)
|
55
|
+
@headers = split(@headers)
|
56
|
+
|
57
|
+
rows.each do |row|
|
58
|
+
parse_row(row)
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
def parse_row(row)
|
64
|
+
if (row && row.content)
|
65
|
+
row = row.content
|
66
|
+
row_hash = {}
|
67
|
+
splitted_row = split(row)
|
68
|
+
|
69
|
+
@headers.each_with_index do |header, index|
|
70
|
+
value = splitted_row[index].strip
|
71
|
+
value = (value && value != "") ? value : nil
|
72
|
+
row_hash[header] = value
|
73
|
+
end
|
74
|
+
|
75
|
+
@rows << row_hash if (row_hash && !row_hash.empty?)
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
def split(text)
|
80
|
+
splitted = text.split(/\|(?!\|)/)
|
81
|
+
end
|
82
|
+
|
83
|
+
def row_count
|
84
|
+
@rows.length
|
85
|
+
end
|
86
|
+
|
87
|
+
end
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
@@ -0,0 +1,126 @@
|
|
1
|
+
|
2
|
+
=begin
|
3
|
+
|
4
|
+
Version 0.9.3
|
5
|
+
|
6
|
+
Copyright (c) 2011, Majestic-12 Ltd
|
7
|
+
All rights reserved.
|
8
|
+
|
9
|
+
Redistribution and use in source and binary forms, with or without
|
10
|
+
modification, are permitted provided that the following conditions are met:
|
11
|
+
1. Redistributions of source code must retain the above copyright
|
12
|
+
notice, this list of conditions and the following disclaimer.
|
13
|
+
2. Redistributions in binary form must reproduce the above copyright
|
14
|
+
notice, this list of conditions and the following disclaimer in the
|
15
|
+
documentation and/or other materials provided with the distribution.
|
16
|
+
3. Neither the name of the Majestic-12 Ltd nor the
|
17
|
+
names of its contributors may be used to endorse or promote products
|
18
|
+
derived from this software without specific prior written permission.
|
19
|
+
|
20
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
21
|
+
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
22
|
+
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
23
|
+
DISCLAIMED. IN NO EVENT SHALL Majestic-12 Ltd BE LIABLE FOR ANY
|
24
|
+
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
25
|
+
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
26
|
+
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
27
|
+
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
28
|
+
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
29
|
+
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
30
|
+
|
31
|
+
=end
|
32
|
+
|
33
|
+
module MajesticSeo
|
34
|
+
module Api
|
35
|
+
class ItemInfo
|
36
|
+
attr_accessor :response, :mappings
|
37
|
+
attr_accessor :index, :type, :url, :result_code, :success, :error_message, :status
|
38
|
+
attr_accessor :external_backlinks, :referring_domains, :indexed_urls, :analysis_results_unit_cost, :ac_rank
|
39
|
+
attr_accessor :get_top_backlinks_analysis_results_unit_cost, :referring_ip_addresses, :referring_subnets
|
40
|
+
attr_accessor :referring_edu_domains, :external_edu_backlinks, :referring_gov_domains, :external_gov_backlinks
|
41
|
+
attr_accessor :exact_referring_edu_domains, :exact_external_edu_backlinks, :exact_referring_gov_domains, :exact_external_gov_backlinks
|
42
|
+
attr_accessor :crawled, :last_crawl_data, :last_crawl_result, :redirecting, :final_redirect_result
|
43
|
+
attr_accessor :outbound_domain_links, :outbound_external_backliks, :outbound_internal_backlinks
|
44
|
+
attr_accessor :title, :redirecting_to
|
45
|
+
|
46
|
+
# This method returns a new instance of the Response class.
|
47
|
+
# If one of the parameters are not provided, it will default to nil.
|
48
|
+
def initialize(response = nil)
|
49
|
+
self.response = response
|
50
|
+
|
51
|
+
self.mappings = {
|
52
|
+
"ItemNum" => {:index => :integer},
|
53
|
+
"ItemType" => {:type => :integer},
|
54
|
+
"Item" => {:url => :string},
|
55
|
+
"ResultCode" => {:result_code => :string},
|
56
|
+
"Status" => {:status => :string},
|
57
|
+
"ExtBackLinks" => {:external_backlinks => :integer},
|
58
|
+
"RefDomains" => {:referring_domains => :integer},
|
59
|
+
"IndexedURLs" => {:indexed_urls => :integer},
|
60
|
+
"AnalysisResUnitsCost" => {:analysis_results_unit_cost => :integer},
|
61
|
+
"ACRank" => {:ac_rank => :integer},
|
62
|
+
"GetTopBackLinksAnalysisResUnitsCost" => {:get_top_backlinks_analysis_results_unit_cost => :integer},
|
63
|
+
"RefIPs" => {:referring_ip_addresses => :integer},
|
64
|
+
"RefSubNets" => {:referring_subnets => :integer},
|
65
|
+
"RefDomainsEDU" => {:referring_edu_domains => :integer},
|
66
|
+
"ExtBackLinksEDU" => {:external_edu_backlinks => :integer},
|
67
|
+
"RefDomainsGOV" => {:referring_gov_domains => :integer},
|
68
|
+
"ExtBackLinksGOV" => {:external_gov_backlinks => :integer},
|
69
|
+
"RefDomainsEDU_Exact" => {:exact_referring_edu_domains => :integer},
|
70
|
+
"ExtBackLinksEDU_Exact" => {:exact_external_edu_backlinks => :integer},
|
71
|
+
"RefDomainsGOV_Exact" => {:exact_referring_gov_domains => :integer},
|
72
|
+
"ExtBackLinksGOV_Exact" => {:exact_external_gov_backlinks => :integer},
|
73
|
+
"CrawledFlag" => {:crawled => :boolean},
|
74
|
+
"LastCrawlDate" => {:last_crawl_date => :date},
|
75
|
+
"LastCrawlResult" => {:last_crawl_result => :string},
|
76
|
+
"RedirectFlag" => {:redirecting => :boolean},
|
77
|
+
"FinalRedirectResult" => {:final_redirect_result => :string},
|
78
|
+
"OutDomainsExternal" => {:outbound_domain_links => :integer},
|
79
|
+
"OutLinksExternal" => {:outbound_external_backliks => :integer},
|
80
|
+
"OutLinksInternal" => {:outbound_internal_backlinks => :integer},
|
81
|
+
"Title" => {:title => :string},
|
82
|
+
"RedirectTo" => {:redirecting_to => :string},
|
83
|
+
}
|
84
|
+
|
85
|
+
parse_item_info
|
86
|
+
end
|
87
|
+
|
88
|
+
def parse_item_info
|
89
|
+
self.response.each do |api_column, api_value|
|
90
|
+
mapping = self.mappings[api_column]
|
91
|
+
value = (!api_value.nil? && !api_value.to_s.eql?("")) ? api_value.to_s : nil
|
92
|
+
|
93
|
+
mapping.each do |column, data_type|
|
94
|
+
converted_value = case data_type
|
95
|
+
when :string then value
|
96
|
+
when :integer then value.to_i
|
97
|
+
when :date then value #Return it as a string for now - need to be able to retrieve a result with a date set to determine date parsing format
|
98
|
+
when :boolean then (value.downcase.eql?("true"))
|
99
|
+
end
|
100
|
+
|
101
|
+
self.send("#{column}=", converted_value)
|
102
|
+
end if (value)
|
103
|
+
end
|
104
|
+
|
105
|
+
set_result_status
|
106
|
+
set_item_type
|
107
|
+
end
|
108
|
+
|
109
|
+
def set_result_status
|
110
|
+
self.success = (self.result_code && self.result_code.downcase.eql?("ok"))
|
111
|
+
self.error_message = (self.success) ? "" : self.result_code
|
112
|
+
end
|
113
|
+
|
114
|
+
def set_item_type
|
115
|
+
self.type = case self.type
|
116
|
+
when 1 then :root_domain
|
117
|
+
when 2 then :sub_domain
|
118
|
+
when 3 then :url
|
119
|
+
end
|
120
|
+
end
|
121
|
+
|
122
|
+
end
|
123
|
+
|
124
|
+
end
|
125
|
+
end
|
126
|
+
|
@@ -0,0 +1,58 @@
|
|
1
|
+
|
2
|
+
=begin
|
3
|
+
|
4
|
+
Version 0.9.3
|
5
|
+
|
6
|
+
Copyright (c) 2011, Majestic-12 Ltd
|
7
|
+
All rights reserved.
|
8
|
+
|
9
|
+
Redistribution and use in source and binary forms, with or without
|
10
|
+
modification, are permitted provided that the following conditions are met:
|
11
|
+
1. Redistributions of source code must retain the above copyright
|
12
|
+
notice, this list of conditions and the following disclaimer.
|
13
|
+
2. Redistributions in binary form must reproduce the above copyright
|
14
|
+
notice, this list of conditions and the following disclaimer in the
|
15
|
+
documentation and/or other materials provided with the distribution.
|
16
|
+
3. Neither the name of the Majestic-12 Ltd nor the
|
17
|
+
names of its contributors may be used to endorse or promote products
|
18
|
+
derived from this software without specific prior written permission.
|
19
|
+
|
20
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
21
|
+
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
22
|
+
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
23
|
+
DISCLAIMED. IN NO EVENT SHALL Majestic-12 Ltd BE LIABLE FOR ANY
|
24
|
+
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
25
|
+
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
26
|
+
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
27
|
+
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
28
|
+
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
29
|
+
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
30
|
+
|
31
|
+
=end
|
32
|
+
|
33
|
+
module MajesticSeo
|
34
|
+
module Api
|
35
|
+
class ItemInfoResponse < Response
|
36
|
+
|
37
|
+
def initialize(response, table_key = "Results")
|
38
|
+
self.table_key = table_key
|
39
|
+
super(response)
|
40
|
+
parse_item_info_objects
|
41
|
+
end
|
42
|
+
|
43
|
+
def parse_item_info_objects
|
44
|
+
parsed = []
|
45
|
+
item_infos = self.items
|
46
|
+
|
47
|
+
if (item_infos && item_infos.any?)
|
48
|
+
item_infos.each do |item_info|
|
49
|
+
parsed << MajesticSeo::Api::ItemInfo.new(item_info)
|
50
|
+
end
|
51
|
+
|
52
|
+
@items = parsed if (parsed.any?)
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|