majesticseo 1.0.1 → 1.0.2

Sign up to get free protection for your applications and to get access to all the features.
File without changes
@@ -1,3 +1,9 @@
1
+ === 1.0.2 2012-01-15
2
+ * 2 minor enhancements
3
+ * allow symbol as enviroment variable
4
+ * Allow supression of stdout msgs if :debug flag is set to false
5
+ * cherrypicked needed active support extensions (AS v.3.0.0 and up)
6
+ * majestic escapes pipes in text data in response. Parsing correctly
1
7
  === 1.0.0 2011-04-09
2
8
  * 2 minor enhancements:
3
9
  * Added installation message
data/Rakefile CHANGED
@@ -7,9 +7,9 @@ require './lib/majesticseo'
7
7
  Hoe.plugin :newgem
8
8
  $hoe = Hoe.spec 'majesticseo' do
9
9
  self.developer 'Achilles Charmpilas', 'ac@humbuckercode.co.uk'
10
- self.post_install_message = 'PostInstall.txt' # TODO remove if post-install message not required
11
- self.rubyforge_name = self.name # TODO this is default value
12
- self.extra_deps = [['activesupport','>= 2.0.2'], ['nokogiri','>= 1.4.0']]
10
+ self.post_install_message = 'PostInstall.txt'
11
+ self.rubyforge_name = self.name
12
+ self.extra_deps = [['activesupport','>= 3.0.0'], ['nokogiri','>= 1.4.0']]
13
13
 
14
14
  end
15
15
 
@@ -4,13 +4,20 @@ $:.unshift(File.dirname(__FILE__)) unless
4
4
  =begin rdoc
5
5
  This module implements a ruby-friendly interface to the Majestic SEO developer API.
6
6
  =end
7
- require "active_support"
8
7
  require "nokogiri"
9
8
  require "open-uri"
10
9
  require "ostruct"
11
10
 
11
+ unless Hash.method_defined?(:to_param)
12
+ require "active_support/core_ext/object/to_query"
13
+ end
14
+
15
+ unless String.method_defined?(:underscore)
16
+ require "active_support/core_ext/string/inflections"
17
+ end
18
+
12
19
  module Majesticseo
13
- VERSION = '1.0.1'
20
+ VERSION = '1.0.2'
14
21
  class NoMethodError < ArgumentError; end
15
22
  class InvalidAPIKey < ArgumentError; end
16
23
  =begin rdoc
@@ -48,4 +55,4 @@ module Majesticseo
48
55
  =end
49
56
  class GlobalVars < Majesticseo::Structure; end
50
57
  end
51
- %W[data_table client].each { |r| require "#{File.dirname(__FILE__)}/majesticseo/#{r}" }
58
+ %W[data_table client].each { |r| require "#{File.dirname(__FILE__)}/majesticseo/#{r}" }
@@ -16,20 +16,29 @@ module Majesticseo
16
16
  end
17
17
  =end
18
18
  class Client
19
+ BASE_URI = "http://%s.majesticseo.com/api_command" unless
20
+ self.const_defined?(:BASE_URI)
21
+
19
22
  attr_reader :app_api_key, :env, :uri, :response
20
23
  attr_accessor :code, :error_message, :full_error, :data_tables, :global_vars
21
24
  =begin rdoc
22
25
  Initialize a Majestic SEO aPI client passing the following options:
23
26
  app_api_key: You Majesticseo API key, found at: https://www.Majesticseo.com/account/api
24
- enviroment (optional): Default to RAILS_ENV, RACK_ENV or default. Determines whether the client uses the sandbox or production API server
27
+ environment (optional): Default to RAILS_ENV, RACK_ENV or default. Determines whether the client uses the sandbox or production API server
25
28
  =end
26
29
  def initialize(opts = {})
27
30
  @app_api_key = opts.delete(:app_api_key)
28
-
29
- raise Majesticseo::InvalidAPIKey.new("API key needs to be a valid Majestic SEO API key. See: https://www.Majesticseo.com/account/api") if @app_api_key.blank?
30
-
31
- if opts[:enviroment]
32
- @env = opts.delete(:enviroment)
31
+ @debug = opts.fetch(:debug, true)
32
+
33
+ if !@app_api_key || @app_api_key.empty?
34
+ msg = "API key needs to be a valid Majestic SEO API key. See: "\
35
+ "https://www.Majesticseo.com/account/api"
36
+
37
+ raise Majesticseo::InvalidAPIKey.new(msg)
38
+ end
39
+
40
+ if opts[:environment]
41
+ @env = opts.delete(:environment)
33
42
  elsif defined?(RAILS_ENV)
34
43
  @env = RAILS_ENV
35
44
  elsif defined?(RACK_ENV)
@@ -40,20 +49,35 @@ module Majesticseo
40
49
  @response = nil
41
50
  @data_tables = []
42
51
  @global_vars = nil
43
- @uri = URI.parse("http://#{@env == "production" ? "enterprise" : "developer"}.Majesticseo.com/api_command")
52
+ @uri = URI.parse(build_url)
53
+
44
54
  puts "Started Majesticseo::Client in #{env} mode"
45
55
  end
46
56
 
47
57
  def call method, params
48
58
  params = {} unless params.is_a? Hash
49
59
  request_uri = uri.clone
50
- request_uri.query = params.merge({:app_api_key => app_api_key, :cmd => method}).to_param
60
+ request_uri.query = params.merge({
61
+ :app_api_key => app_api_key,
62
+ :cmd => method
63
+ }).to_param
64
+
51
65
  @response = Nokogiri::XML(open(request_uri))
52
66
 
53
67
  # Set response and global variable attributes
54
- response.at("Result").keys.each { |a| send("#{a.underscore}=".to_sym,response.at("Result").attr(a)) } if response.at("Result")
68
+ if response.at("Result")
69
+ response.at("Result").keys.each do |a|
70
+ send("#{a.underscore}=".to_sym, response.at("Result").attr(a))
71
+ end
72
+ end
73
+
55
74
  @global_vars = GlobalVars.new
56
- response.at("GlobalVars").keys.each { |a| @global_vars.send("#{a.underscore}=".to_sym,response.at("GlobalVars").attr(a)) } if response.at("GlobalVars")
75
+ if response.at("GlobalVars")
76
+ response.at("GlobalVars").keys.each do |a|
77
+ @global_vars.send("#{a.underscore}=".to_sym, response.at("GlobalVars").attr(a))
78
+ end
79
+ end
80
+
57
81
  parse_data if success?
58
82
  end
59
83
 
@@ -62,11 +86,29 @@ module Majesticseo
62
86
  end
63
87
 
64
88
  def parse_data
65
- @data_tables = @response.search("DataTable").collect { |table| DataTable.create_from_xml(table) }
89
+ @data_tables = @response.search("DataTable").map do |table|
90
+ DataTable.create_from_xml(table)
91
+ end
66
92
  end
67
93
 
68
94
  def success?
69
95
  code == "OK" and error_message == ""
70
96
  end
97
+
98
+ def build_url
99
+ subdomain = Hash.new("developer")
100
+ subdomain[:production] = "enterprise"
101
+ subdomain['production'] = "enterprise"
102
+
103
+ BASE_URI % subdomain[env]
104
+ end
105
+
106
+ def puts(msg)
107
+ Kernel.puts(msg) if debug?
108
+ end
109
+
110
+ def debug?
111
+ @debug
112
+ end
71
113
  end
72
- end
114
+ end
@@ -9,7 +9,7 @@ module Majesticseo
9
9
  table.rows_count = xml.attr("RowsCount").to_i
10
10
  table.rows = xml.search("Row").map do |r|
11
11
  row = Row.new
12
- data = r.content.split("|")
12
+ data = r.content.scan(/(?=<!\|)\|(?=!\|)/)
13
13
  table.headers.each_index { |i| row.send("#{table.headers[i].underscore}=",data[i])}
14
14
  row
15
15
  end
@@ -28,4 +28,5 @@ module Majesticseo
28
28
  @headers ? @headers : []
29
29
  end
30
30
  end
31
- end
31
+ end
32
+
@@ -1,3 +1,13 @@
1
+ require 'rubygems'
1
2
  require 'stringio'
2
3
  require 'test/unit'
3
- require File.dirname(__FILE__) + '/../lib/majesticseo'
4
+ require File.expand_path('../../lib/majesticseo', __FILE__)
5
+
6
+ def maj_client(key=nil, env=nil)
7
+ Majesticseo::Client.new(:app_api_key => key, :environment => env)
8
+ end
9
+
10
+ def fake_client(env=nil)
11
+ maj_client("fake", env)
12
+ end
13
+
@@ -1,15 +1,56 @@
1
- require File.dirname(__FILE__) + '/test_helper.rb'
2
-
3
- class TestMajesticseo < Test::Unit::TestCase
4
- def test_cannot_initialize_client_without_api_key
5
- assert_raise Majesticseo::InvalidAPIKey do
6
- Majesticseo::Client.new
7
- end
8
- end
9
-
10
- def test_cannot_initialize_client_with_empty_api_key
11
- assert_raise Majesticseo::InvalidAPIKey do
12
- Majesticseo::Client.new(:app_api_key => "")
13
- end
14
- end
15
- end
1
+ require File.expand_path('../test_helper', __FILE__)
2
+
3
+ class TestMajesticseo < Test::Unit::TestCase
4
+ def test_cannot_initialize_client_without_api_key
5
+ assert_raise Majesticseo::InvalidAPIKey do
6
+ Majesticseo::Client.new
7
+ end
8
+ end
9
+
10
+ def test_cannot_initialize_client_with_empty_api_key
11
+ assert_raise Majesticseo::InvalidAPIKey do
12
+ Majesticseo::Client.new(:app_api_key => "")
13
+ end
14
+ end
15
+ end
16
+
17
+ class TestMajesticURL < Test::Unit::TestCase
18
+ def test_builds_correct_url_for_development
19
+ assert_equal "http://developer.majesticseo.com/api_command",
20
+ fake_client("development").build_url
21
+ end
22
+
23
+ def test_builds_correct_url_for_production
24
+ [:production, "production"].each do |e|
25
+ assert_equal "http://enterprise.majesticseo.com/api_command",
26
+ fake_client(e).build_url
27
+ end
28
+ end
29
+
30
+ def test_builds_correct_url_for_default
31
+ assert_equal "http://developer.majesticseo.com/api_command",
32
+ fake_client.build_url
33
+ end
34
+ end
35
+
36
+ class TestMajesticDebug < Test::Unit::TestCase
37
+ def setup
38
+ @_stdout = $stdout
39
+ @stringio = StringIO.new
40
+ $stdout = @stringio
41
+ end
42
+
43
+ def teardown
44
+ $stdout = @_stdout
45
+ end
46
+
47
+ def test_client_in_debug_mode_by_default
48
+ assert fake_client.debug?
49
+ end
50
+
51
+ def test_suppress_output_when_not_in_debug_mode
52
+ client = Majesticseo::Client.new(:app_api_key => "test", :debug => false)
53
+
54
+ assert_equal "", @stringio.string
55
+ end
56
+ end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: majesticseo
3
3
  version: !ruby/object:Gem::Version
4
- hash: 21
4
+ hash: 19
5
5
  prerelease:
6
6
  segments:
7
7
  - 1
8
8
  - 0
9
- - 1
10
- version: 1.0.1
9
+ - 2
10
+ version: 1.0.2
11
11
  platform: ruby
12
12
  authors:
13
13
  - Achilles Charmpilas
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-04-09 00:00:00 +02:00
18
+ date: 2013-01-05 00:00:00 +01:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -26,12 +26,12 @@ dependencies:
26
26
  requirements:
27
27
  - - ">="
28
28
  - !ruby/object:Gem::Version
29
- hash: 11
29
+ hash: 7
30
30
  segments:
31
- - 2
31
+ - 3
32
32
  - 0
33
- - 2
34
- version: 2.0.2
33
+ - 0
34
+ version: 3.0.0
35
35
  type: :runtime
36
36
  version_requirements: *id001
37
37
  - !ruby/object:Gem::Dependency
@@ -51,37 +51,21 @@ dependencies:
51
51
  type: :runtime
52
52
  version_requirements: *id002
53
53
  - !ruby/object:Gem::Dependency
54
- name: rubyforge
54
+ name: hoe
55
55
  prerelease: false
56
56
  requirement: &id003 !ruby/object:Gem::Requirement
57
57
  none: false
58
58
  requirements:
59
59
  - - ">="
60
60
  - !ruby/object:Gem::Version
61
- hash: 7
61
+ hash: 35
62
62
  segments:
63
63
  - 2
64
- - 0
64
+ - 9
65
65
  - 4
66
- version: 2.0.4
66
+ version: 2.9.4
67
67
  type: :development
68
68
  version_requirements: *id003
69
- - !ruby/object:Gem::Dependency
70
- name: hoe
71
- prerelease: false
72
- requirement: &id004 !ruby/object:Gem::Requirement
73
- none: false
74
- requirements:
75
- - - ">="
76
- - !ruby/object:Gem::Version
77
- hash: 19
78
- segments:
79
- - 2
80
- - 6
81
- - 2
82
- version: 2.6.2
83
- type: :development
84
- version_requirements: *id004
85
69
  description: A ruby-friendly interface to the Majestic SEO API.
86
70
  email:
87
71
  - ac@humbuckercode.co.uk
@@ -107,6 +91,7 @@ files:
107
91
  - script/generate
108
92
  - test/test_helper.rb
109
93
  - test/test_majesticseo.rb
94
+ - .gemtest
110
95
  has_rdoc: true
111
96
  homepage: http://github.com/Achillefs/majesticseo
112
97
  licenses: []
@@ -138,7 +123,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
138
123
  requirements: []
139
124
 
140
125
  rubyforge_project: majesticseo
141
- rubygems_version: 1.4.2
126
+ rubygems_version: 1.6.1
142
127
  signing_key:
143
128
  specification_version: 3
144
129
  summary: A ruby-friendly interface to the Majestic SEO API.