yandex-webmaster 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (49) hide show
  1. data/.document +5 -0
  2. data/Gemfile +23 -0
  3. data/Gemfile.lock +72 -0
  4. data/LICENSE.txt +20 -0
  5. data/README.md +259 -0
  6. data/Rakefile +46 -0
  7. data/lib/yandex-webmaster.rb +80 -0
  8. data/lib/yandex-webmaster/api/attributes.rb +23 -0
  9. data/lib/yandex-webmaster/api/attributes/accessor_builder.rb +59 -0
  10. data/lib/yandex-webmaster/api/attributes/reader_builder.rb +35 -0
  11. data/lib/yandex-webmaster/api/attributes/types/base.rb +27 -0
  12. data/lib/yandex-webmaster/api/attributes/types/boolean.rb +22 -0
  13. data/lib/yandex-webmaster/api/attributes/types/date.rb +17 -0
  14. data/lib/yandex-webmaster/api/attributes/types/date_time.rb +17 -0
  15. data/lib/yandex-webmaster/api/attributes/types/float.rb +17 -0
  16. data/lib/yandex-webmaster/api/attributes/types/integer.rb +17 -0
  17. data/lib/yandex-webmaster/api/attributes/types/symbol.rb +17 -0
  18. data/lib/yandex-webmaster/api/attributes/types/time.rb +17 -0
  19. data/lib/yandex-webmaster/api/attributes/writer_builder.rb +36 -0
  20. data/lib/yandex-webmaster/api/attributes_builder.rb +65 -0
  21. data/lib/yandex-webmaster/api_factory.rb +32 -0
  22. data/lib/yandex-webmaster/authorization.rb +59 -0
  23. data/lib/yandex-webmaster/base.rb +120 -0
  24. data/lib/yandex-webmaster/client.rb +16 -0
  25. data/lib/yandex-webmaster/configuration.rb +99 -0
  26. data/lib/yandex-webmaster/connection.rb +87 -0
  27. data/lib/yandex-webmaster/core_ext/array.rb +23 -0
  28. data/lib/yandex-webmaster/core_ext/class.rb +55 -0
  29. data/lib/yandex-webmaster/core_ext/date.rb +7 -0
  30. data/lib/yandex-webmaster/core_ext/date_time.rb +7 -0
  31. data/lib/yandex-webmaster/core_ext/hash.rb +92 -0
  32. data/lib/yandex-webmaster/core_ext/nil_class.rb +7 -0
  33. data/lib/yandex-webmaster/core_ext/object.rb +19 -0
  34. data/lib/yandex-webmaster/core_ext/string.rb +23 -0
  35. data/lib/yandex-webmaster/errors.rb +23 -0
  36. data/lib/yandex-webmaster/host.rb +220 -0
  37. data/lib/yandex-webmaster/hosts/crawling.rb +26 -0
  38. data/lib/yandex-webmaster/hosts/sitemap.rb +73 -0
  39. data/lib/yandex-webmaster/hosts/sitemap_info.rb +55 -0
  40. data/lib/yandex-webmaster/hosts/top_info.rb +19 -0
  41. data/lib/yandex-webmaster/hosts/verification.rb +81 -0
  42. data/lib/yandex-webmaster/request.rb +62 -0
  43. data/lib/yandex-webmaster/request/oauth2.rb +28 -0
  44. data/lib/yandex-webmaster/response/hashify.rb +59 -0
  45. data/lib/yandex-webmaster/version.rb +14 -0
  46. data/test/helper.rb +18 -0
  47. data/test/test_webmaster.rb +7 -0
  48. data/yandex-webmaster.gemspec +167 -0
  49. metadata +546 -0
@@ -0,0 +1,23 @@
1
+ # encoding: utf-8
2
+
3
+ module Yandex
4
+ module Webmaster
5
+ module Api
6
+ module Attributes
7
+ module Types
8
+ autoload :Base, 'yandex-webmaster/api/attributes/types/base'
9
+ autoload :Boolean, 'yandex-webmaster/api/attributes/types/boolean'
10
+ autoload :Date, 'yandex-webmaster/api/attributes/types/date'
11
+ autoload :DateTime, 'yandex-webmaster/api/attributes/types/date_time'
12
+ autoload :Float, 'yandex-webmaster/api/attributes/types/float'
13
+ autoload :Integer, 'yandex-webmaster/api/attributes/types/integer'
14
+ autoload :Symbol, 'yandex-webmaster/api/attributes/types/symbol'
15
+ autoload :Time, 'yandex-webmaster/api/attributes/types/time'
16
+ end
17
+
18
+ autoload :ReaderBuilder, 'yandex-webmaster/api/attributes/reader_builder'
19
+ autoload :WriterBuilder, 'yandex-webmaster/api/attributes/writer_builder'
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,59 @@
1
+ # encoding: utf-8
2
+
3
+ module Yandex
4
+ module Webmaster
5
+ module Api
6
+ module Attributes
7
+ class AccessorBuilder
8
+
9
+ attr_reader :object, :attribute_name, :type, :options
10
+
11
+ def initialize(object, attribute_name, type, options = {})
12
+ @object = object
13
+ @attribute_name = attribute_name
14
+ @type = type
15
+
16
+ @options = options.symbolize_keys
17
+ end
18
+
19
+ def accessor
20
+ raise NotImplementedError
21
+ end
22
+
23
+ def method_name
24
+ raise NotImplementedError
25
+ end
26
+
27
+ def define_method
28
+ raise NotImplementedError
29
+ end
30
+
31
+ def define_aliases
32
+ if self.type.respond_to?("default_#{self.accessor.to_s}_aliases")
33
+ self.type.send("default_#{self.accessor.to_s}_aliases", self.method_name).each do |alias_method_name|
34
+ self.object.send(:alias_method, alias_method_name, self.method_name)
35
+ end
36
+ end
37
+ end
38
+
39
+ def method_visibility_from_options
40
+ return self.options[self.accessor] if self.options[self.accessor].present? && [:public, :protected, :private].include?(self.options[self.accessor])
41
+ (self.options[self.accessor].is_a?(Hash) && self.options[self.accessor][:visibility]) || self.options["#{self.accessor.to_s}_visibility".to_sym]
42
+ end
43
+
44
+ def method_visibility
45
+ self.method_visibility_from_options || :public
46
+ end
47
+
48
+ def method_name_from_options
49
+ (self.options[self.accessor].is_a?(Hash) && self.options[self.accessor][:name]) || self.options["#{self.accessor.to_s}_name".to_sym]
50
+ end
51
+
52
+ def instance_variable_name
53
+ self.options[:instance_variable_name] || "@#{self.attribute_name}"
54
+ end
55
+ end
56
+ end
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,35 @@
1
+ # encoding: utf-8
2
+
3
+ require 'yandex-webmaster/api/attributes/accessor_builder'
4
+
5
+ module Yandex
6
+ module Webmaster
7
+ module Api
8
+ module Attributes
9
+ class ReaderBuilder < AccessorBuilder
10
+
11
+ def accessor
12
+ :reader
13
+ end
14
+
15
+ def method_name
16
+ self.method_name_from_options || self.attribute_name
17
+ end
18
+
19
+ def define_method
20
+ unless self.object.method_defined?(self.method_name)
21
+ self.object.class_eval(<<-EOS, __FILE__, __LINE__ + 1)
22
+ def #{method_name}
23
+ instance_variable_get(:#{instance_variable_name.to_s})
24
+ end
25
+ EOS
26
+
27
+ self.object.send self.method_visibility, self.method_name
28
+ self
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,27 @@
1
+ # encoding: utf-8
2
+
3
+ module Yandex
4
+ module Webmaster
5
+ module Api
6
+ module Attributes
7
+ module Types
8
+ class Base < Object
9
+ private_class_method :new
10
+
11
+ def self.typecast(value, options = {})
12
+ raise NotImplementedError
13
+ end
14
+
15
+ def self.default_reader_aliases(method_name)
16
+ []
17
+ end
18
+
19
+ def self.default_writer_aliases(method_name)
20
+ []
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,22 @@
1
+ # encoding: utf-8
2
+
3
+ module Yandex
4
+ module Webmaster
5
+ module Api
6
+ module Attributes
7
+ module Types
8
+ class Boolean < Base
9
+ def self.typecast(value, options = {})
10
+ return true if ['yes', '1', 1, 'true', true].include?(value)
11
+ return false
12
+ end
13
+
14
+ def self.default_reader_aliases(method_name)
15
+ super.push("#{method_name}?").flatten.uniq
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,17 @@
1
+ # encoding: utf-8
2
+
3
+ module Yandex
4
+ module Webmaster
5
+ module Api
6
+ module Attributes
7
+ module Types
8
+ class Date < Base
9
+ def self.typecast(value, options = {})
10
+ options[:format].present? ? ::Date.strftime(value, options[:format]) : ::Date.parse(value)
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,17 @@
1
+ # encoding: utf-8
2
+
3
+ module Yandex
4
+ module Webmaster
5
+ module Api
6
+ module Attributes
7
+ module Types
8
+ class DateTime < Base
9
+ def self.typecast(value, options = {})
10
+ options[:format].present? ? ::DateTime.strftime(value, options[:format]) : ::DateTime.parse(value)
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,17 @@
1
+ # encoding: utf-8
2
+
3
+ module Yandex
4
+ module Webmaster
5
+ module Api
6
+ module Attributes
7
+ module Types
8
+ class Float < Base
9
+ def self.typecast(value, options = {})
10
+ value.to_f
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,17 @@
1
+ # encoding: utf-8
2
+
3
+ module Yandex
4
+ module Webmaster
5
+ module Api
6
+ module Attributes
7
+ module Types
8
+ class Integer < Base
9
+ def self.typecast(value, options = {})
10
+ value.to_i
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,17 @@
1
+ # encoding: utf-8
2
+
3
+ module Yandex
4
+ module Webmaster
5
+ module Api
6
+ module Attributes
7
+ module Types
8
+ class Symbol < Base
9
+ def self.typecast(value, options = {})
10
+ value.underscore.to_sym
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,17 @@
1
+ # encoding: utf-8
2
+
3
+ module Yandex
4
+ module Webmaster
5
+ module Api
6
+ module Attributes
7
+ module Types
8
+ class Time < Base
9
+ def self.typecast(value, options = {})
10
+ options[:format].present? ? ::Time.strftime(value, options[:format]) : ::Time.parse(value)
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,36 @@
1
+ # encoding: utf-8
2
+
3
+ require 'yandex-webmaster/api/attributes/accessor_builder'
4
+
5
+ module Yandex
6
+ module Webmaster
7
+ module Api
8
+ module Attributes
9
+ class WriterBuilder < AccessorBuilder
10
+
11
+ def accessor
12
+ :writer
13
+ end
14
+
15
+ def method_name
16
+ self.method_name_from_options || "#{self.attribute_name}="
17
+ end
18
+
19
+ def define_method
20
+ unless self.object.method_defined?(self.method_name)
21
+ self.object.class_eval(<<-EOS, __FILE__, __LINE__ + 1)
22
+ def #{self.method_name}(value)
23
+ value = #{type}.respond_to?(:typecast) ? #{type}.typecast(value) : #{type}.new(value)
24
+ instance_variable_set(:#{instance_variable_name.to_s}, value)
25
+ end
26
+ EOS
27
+
28
+ self.object.send self.method_visibility, self.method_name
29
+ self
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,65 @@
1
+ # encoding: utf-8
2
+
3
+ module Yandex
4
+ module Webmaster
5
+ module Api
6
+ class AttributesBuilder
7
+ attr_reader :klass, :options
8
+
9
+ def initialize(object, options, &block)
10
+ @object = object
11
+ @options = options
12
+
13
+ raise ArgumentError.new('Missing options[:as] value' ) if @options[:as].blank?
14
+
15
+ self.instance_eval(&block)
16
+ end
17
+
18
+ def attr(*args)
19
+ options = args.extract_options!
20
+ type = options.delete(:type) || args[1]
21
+ attribute_name = args[0].to_s
22
+
23
+ casted_type = self.class.cast_type(type)
24
+ self.class.const_missing(type.to_s) if casted_type.blank?
25
+
26
+ reader_builder = Yandex::Webmaster::Api::Attributes::ReaderBuilder.new(@object, attribute_name, casted_type, options)
27
+ writer_builder = Yandex::Webmaster::Api::Attributes::WriterBuilder.new(@object, attribute_name, casted_type, options)
28
+ self.add_attribute(attribute_name, reader_builder, writer_builder)
29
+
30
+ reader_builder.define_method.define_aliases
31
+ writer_builder.define_method.define_aliases
32
+ end
33
+
34
+ def self.cast_type(constant)
35
+ return constant if constant.is_a?(Class) && constant < Yandex::Webmaster::Api::Attributes::Types::Base
36
+
37
+ string = constant.to_s
38
+ string = string.camelize if (string =~ /\w_\w/ || string[0].downcase == string[0])
39
+
40
+ begin
41
+ if Yandex::Webmaster::Api::Attributes::Types.const_defined?(string)
42
+ return Yandex::Webmaster::Api::Attributes::Types.const_get(string)
43
+ elsif Module.const_defined?(string)
44
+ return Module.const_get(string)
45
+ else
46
+ return nil
47
+ end
48
+ rescue
49
+ return constant
50
+ end
51
+ end
52
+
53
+ protected
54
+
55
+ def add_attribute(attribute_name, reader_builder, writer_builder)
56
+ @object.send(self.options[:as])[attribute_name.to_sym] = {
57
+ :reader_name => reader_builder.method_name,
58
+ :writer_name => writer_builder.method_name,
59
+ :instance_variable_name => writer_builder.instance_variable_name
60
+ }
61
+ end
62
+ end
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,32 @@
1
+ module Yandex
2
+ module Webmaster
3
+ class ApiFactory < Base
4
+ extend Forwardable
5
+
6
+ def_delegators :to_a, :to_xml, :to_yaml, :length, :collect, :map, :each, :all?, :include?, :to_ary
7
+
8
+ attr_accessor :klass, :url
9
+
10
+ def respond_to?(method, include_private = false)
11
+ super || Array.method_defined?(method) || @klass.respond_to?(method, include_private)
12
+ end
13
+
14
+ def method_missing(method, *args, &block)
15
+ if @klass.respond_to?(method)
16
+ args = args.push(self)
17
+ @klass.send(method, *args, &block)
18
+ elsif Array.method_defined?(method)
19
+ self.class.def_delegator :to_a, method
20
+ self.to_a.send(method, *args, &block)
21
+ else
22
+ super
23
+ end
24
+ end
25
+
26
+ def to_a
27
+ @objects ||= self.objects_from_response(self.klass, self.request(:get, self.url), :host)
28
+ end
29
+ alias :all :to_a
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,59 @@
1
+ # encoding: utf-8
2
+
3
+ module Yandex
4
+ module Webmaster
5
+ module Authorization
6
+
7
+ attr_accessor :scopes
8
+
9
+ # Setup OAuth2 instance
10
+ def oauth
11
+ @client ||= ::OAuth2::Client.new(self.configuration.app_id, self.configuration.app_password,
12
+ {
13
+ :site => self.configuration.site || Yandex::Webmaster::Configuration.size,
14
+ :authorize_url => '/authorize',
15
+ :token_url => '/token',
16
+ :ssl => { :verify => false }
17
+ }
18
+ )
19
+ end
20
+
21
+ # Strategy token
22
+ #
23
+ def auth_code
24
+ self.verify_oauth!
25
+ self.oauth.auth_code
26
+ end
27
+
28
+ # Sends authorization request to Yandex.Webmaster.
29
+ #
30
+ def authorize_url(params = {})
31
+ self.verify_oauth!
32
+ self.oauth.auth_code.authorize_url(params)
33
+ end
34
+
35
+ # Makes request to token endpoint and retrieves access token value
36
+ def token(authorization_code, params = {})
37
+ self.verify_oauth!
38
+ self.oauth.auth_code.get_token(authorization_code, params)
39
+ end
40
+
41
+ def authenticate(authorization_code, params = {})
42
+ self.configuration.oauth_token = (self.token(authorization_code, params).token)
43
+ self
44
+ end
45
+
46
+ # Check whether authentication credentials are present
47
+ def authenticated?
48
+ self.configuration.oauth_token?
49
+ end
50
+
51
+ protected
52
+
53
+ def verify_oauth! # :nodoc:
54
+ raise ArgumentError, 'Need to provide app_id and app_password' unless self.configuration.app_id? && self.configuration.app_password?
55
+ end
56
+
57
+ end
58
+ end
59
+ end