webmaster 0.1.0

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.
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/Gemfile ADDED
@@ -0,0 +1,24 @@
1
+ source "http://rubygems.org"
2
+
3
+ gem 'faraday'
4
+ gem 'oauth2'
5
+ gem 'libxml-ruby'
6
+ gem 'virtus'
7
+
8
+ group :development do
9
+ gem "shoulda"
10
+ gem "rdoc"
11
+ gem "bundler"
12
+ gem "jeweler"
13
+ end
14
+
15
+ group :documentation do
16
+ gem "yard"
17
+ end
18
+
19
+ group :test do
20
+ gem 'vcr'
21
+ gem 'webmock'
22
+ end
23
+
24
+ gemspec
@@ -0,0 +1,85 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ webmaster (0.0.1)
5
+ faraday
6
+ libxml-ruby
7
+ oauth2
8
+ virtus
9
+ webmaster
10
+
11
+ GEM
12
+ remote: http://rubygems.org/
13
+ specs:
14
+ activesupport (3.2.13)
15
+ i18n (= 0.6.1)
16
+ multi_json (~> 1.0)
17
+ addressable (2.3.4)
18
+ backports (2.6.7)
19
+ bourne (1.4.0)
20
+ mocha (~> 0.13.2)
21
+ crack (0.3.2)
22
+ descendants_tracker (0.0.1)
23
+ faraday (0.8.7)
24
+ multipart-post (~> 1.1)
25
+ git (1.2.5)
26
+ httpauth (0.2.0)
27
+ i18n (0.6.1)
28
+ jeweler (1.8.4)
29
+ bundler (~> 1.0)
30
+ git (>= 1.2.5)
31
+ rake
32
+ rdoc
33
+ json (1.7.7)
34
+ jwt (0.1.8)
35
+ multi_json (>= 1.5)
36
+ libxml-ruby (2.6.0)
37
+ metaclass (0.0.1)
38
+ mocha (0.13.3)
39
+ metaclass (~> 0.0.1)
40
+ multi_json (1.7.2)
41
+ multi_xml (0.5.3)
42
+ multipart-post (1.2.0)
43
+ oauth2 (0.9.1)
44
+ faraday (~> 0.8)
45
+ httpauth (~> 0.1)
46
+ jwt (~> 0.1.4)
47
+ multi_json (~> 1.0)
48
+ multi_xml (~> 0.5)
49
+ rack (~> 1.2)
50
+ rack (1.5.2)
51
+ rake (10.0.4)
52
+ rdoc (4.0.1)
53
+ json (~> 1.4)
54
+ shoulda (3.4.0)
55
+ shoulda-context (~> 1.0, >= 1.0.1)
56
+ shoulda-matchers (~> 1.0, >= 1.4.1)
57
+ shoulda-context (1.1.1)
58
+ shoulda-matchers (1.5.6)
59
+ activesupport (>= 3.0.0)
60
+ bourne (~> 1.3)
61
+ vcr (2.4.0)
62
+ virtus (0.5.4)
63
+ backports (~> 2.6.1)
64
+ descendants_tracker (~> 0.0.1)
65
+ webmock (1.11.0)
66
+ addressable (>= 2.2.7)
67
+ crack (>= 0.3.2)
68
+ yard (0.8.6.1)
69
+
70
+ PLATFORMS
71
+ ruby
72
+
73
+ DEPENDENCIES
74
+ bundler
75
+ faraday
76
+ jeweler
77
+ libxml-ruby
78
+ oauth2
79
+ rdoc
80
+ shoulda
81
+ vcr
82
+ virtus
83
+ webmaster!
84
+ webmock
85
+ yard
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2013 Igor Alexandrov
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,94 @@
1
+ [![Dependency Status](https://gemnasium.com/igor-alexandrov/webmaster.png)](http://gemnasium.com/igor-alexandrov/webmaster)
2
+ [![Code Climate](https://codeclimate.com/badge.png)](https://codeclimate.com/github/igor-alexandrov/webmaster)
3
+
4
+ # webmaster
5
+
6
+ Wrapper for Yandex.Webmaster API.
7
+
8
+ **About Yandex.Webmaster**
9
+
10
+ * [Russian](http://webmaster.yandex.ru/)
11
+ * [English](http://webmaster.yandex.com/)
12
+
13
+ **API Documentation**
14
+
15
+ * [Russian](http://api.yandex.ru/webmaster/)
16
+ * [English](http://api.yandex.com/webmaster/)
17
+
18
+ ## Installation
19
+
20
+ [sudo] gem install webmaster
21
+
22
+ ## Usage
23
+
24
+ ### Authentication
25
+
26
+ Yandex's API uses OAuth for authentication. Luckily, the Webmaster gem hides most of the details from you.
27
+
28
+ If you have never used Webmaster API or you want to change your authentication credentials or your authorization token expired, then you should create new one:
29
+
30
+ ```ruby
31
+ require 'rubygems'
32
+ require 'webmaster'
33
+
34
+ # Get your API credentials at https://oauth.yandex.ru/
35
+ client = Webmaster.new(:app_id => 'your_app_id', :app_password => 'your_app_password')
36
+ => #<Webmaster::Client>
37
+
38
+ # Follow the authorization url, you will be redirected to the callback url, specified in your application settings.
39
+ client.authorize_url
40
+ => "https://oauth.yandex.ru/authorize?response_type=code&client_id=your_app_id"
41
+
42
+ # Use authorization code from params to get authorization token
43
+ client.authenticate(params[:code])
44
+ => #<Webmaster::Client>
45
+
46
+ # If no error is raised then you are free to use any API method
47
+ # Too see what token is now used call for client configuration
48
+ token = client.configuration.oauth_token
49
+ => "82af4af2a42e4019bd59a325da0f31d8"
50
+ ```
51
+
52
+ If you want to restore previously used token, it can be easily done too:
53
+
54
+ ```ruby
55
+ require 'rubygems'
56
+ require 'webmaster'
57
+
58
+ # get your API credentials at https://oauth.yandex.ru/
59
+ client = Webmaster.new(:oauth_token => 'token')
60
+ ```
61
+
62
+ To check whether you client is authenticated or not, use `authenticated?` method.
63
+
64
+ ```ruby
65
+ # We have already initialized client before.
66
+ client.authenticated?
67
+ => true
68
+ ```
69
+
70
+
71
+ ## Note on Patches / Pull Requests
72
+
73
+ * Fork the project.
74
+ * Make your feature addition or bug fix.
75
+ * Add tests for it. This is important so I don't break it in a
76
+ future version unintentionally.
77
+ * Commit, do not mess with rakefile, version, or history.
78
+ (if you want to have your own version, that is fine but
79
+ bump version in a commit by itself I can ignore when I pull)
80
+ * Send me a pull request. Bonus points for topic branches.
81
+
82
+ ## Credits
83
+
84
+ ![JetRockets](http://www.jetrockets.ru/public/logo.png)
85
+
86
+ Webmaster is maintained by [JetRockets](http://www.jetrockets.ru/en).
87
+
88
+ Contributors:
89
+
90
+ * [Igor Alexandrov](http://igor-alexandrov.github.com/)
91
+
92
+ ## License
93
+
94
+ It is free software, and may be redistributed under the terms specified in the LICENSE file.
@@ -0,0 +1,46 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+ begin
6
+ Bundler.setup(:default, :development)
7
+ rescue Bundler::BundlerError => e
8
+ $stderr.puts e.message
9
+ $stderr.puts "Run `bundle install` to install missing gems"
10
+ exit e.status_code
11
+ end
12
+ require 'rake'
13
+
14
+ require 'jeweler'
15
+ require './lib/webmaster/version.rb'
16
+ Jeweler::Tasks.new do |gem|
17
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
18
+ gem.name = "webmaster"
19
+ gem.homepage = "http://github.com/igor-alexandrov/webmaster"
20
+ gem.license = "MIT"
21
+ gem.summary = %Q{Ruby wrapper for Yandex.Webmaster API}
22
+ gem.email = "igor.alexandrov@gmail.com"
23
+ gem.authors = ["Igor Alexandrov"]
24
+ gem.version = Webmaster::Version::STRING
25
+ # dependencies defined in Gemfile
26
+ end
27
+ Jeweler::RubygemsDotOrgTasks.new
28
+
29
+ require 'rake/testtask'
30
+ Rake::TestTask.new(:test) do |test|
31
+ test.libs << 'lib' << 'test'
32
+ test.pattern = 'test/**/test_*.rb'
33
+ test.verbose = true
34
+ end
35
+
36
+ task :default => :install
37
+
38
+ require 'rdoc/task'
39
+ Rake::RDocTask.new do |rdoc|
40
+ version = Webmaster::Version::STRING
41
+
42
+ rdoc.rdoc_dir = 'rdoc'
43
+ rdoc.title = "webmaster #{version}"
44
+ rdoc.rdoc_files.include('README*')
45
+ rdoc.rdoc_files.include('lib/**/*.rb')
46
+ end
@@ -0,0 +1,66 @@
1
+ require 'webmaster/version'
2
+ require 'webmaster/configuration'
3
+ require 'webmaster/errors'
4
+
5
+ require 'webmaster/core_ext/hash'
6
+ require 'webmaster/core_ext/object'
7
+ require 'webmaster/core_ext/string'
8
+
9
+ module Webmaster
10
+ # extend Configuration
11
+
12
+ class << self
13
+ # Alias for Webmaster::Client.new
14
+ #
15
+ # @return [Github::Client]
16
+ def new(options = {}, &block)
17
+ Webmaster::Client.new(:configuration => options, &block)
18
+ end
19
+
20
+ # Delegate to Webmaster::Client
21
+ #
22
+ def method_missing(method, *args, &block)
23
+ return super unless new.respond_to?(method)
24
+ new.send(method, *args, &block)
25
+ end
26
+
27
+ def respond_to?(method, include_private = false)
28
+ new.respond_to?(method, include_private) || super(method, include_private)
29
+ end
30
+
31
+ # config/initializers/webmaster.rb (for instance)
32
+ #
33
+ # Webmaster.configure do |config|
34
+ # config.application_id = 'application_id'
35
+ # config.application_password = 'application_password'
36
+ # end
37
+ #
38
+ # elsewhere
39
+ #
40
+ # client = Webmaster::Client.new
41
+
42
+ def configure
43
+ yield self
44
+ true
45
+ end
46
+ end
47
+
48
+ autoload :Base, 'webmaster/base'
49
+ autoload :ApiFactory, 'webmaster/api_factory'
50
+ autoload :Client, 'webmaster/client'
51
+
52
+ autoload :Host, 'webmaster/host'
53
+
54
+ module Hosts
55
+ autoload :Crawling, 'webmaster/hosts/crawling'
56
+ autoload :Verification, 'webmaster/hosts/verification'
57
+ end
58
+
59
+ module Request
60
+ autoload :OAuth2, 'webmaster/request/oauth2'
61
+ end
62
+
63
+ module Response
64
+ autoload :Hashify, 'webmaster/response/hashify'
65
+ end
66
+ end
@@ -0,0 +1,59 @@
1
+ # encoding: utf-8
2
+
3
+ module Webmaster
4
+ module Api
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 || 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
@@ -0,0 +1,87 @@
1
+ # encoding: utf-8
2
+
3
+ require 'faraday'
4
+
5
+ module Webmaster
6
+ module Api
7
+ module Connection
8
+ extend self
9
+
10
+ USER_AGENT = 'User-Agent'.freeze
11
+
12
+ ACCEPT = 'Accept'.freeze
13
+
14
+ ACCEPT_CHARSET = 'Accept-Charset'.freeze
15
+
16
+ CONTENT_TYPE = 'Content-Type'.freeze
17
+
18
+ ALLOWED_OPTIONS = [
19
+ :headers,
20
+ :url,
21
+ :params,
22
+ :request,
23
+ :ssl
24
+ ].freeze
25
+
26
+ # Returns a Fraday::Connection object
27
+ #
28
+ def connection(options = {})
29
+
30
+ options = self.connection_options(options)
31
+
32
+ if @connection_options != options
33
+ @connection = nil
34
+ @connection_options = options
35
+ end
36
+
37
+ @connection ||= Faraday.new(@connection_options.merge(:builder => self.stack))
38
+ end
39
+
40
+ protected
41
+
42
+ def connection_options(options = {})
43
+ options.slice!(*ALLOWED_OPTIONS)
44
+
45
+ {
46
+ :headers => {
47
+ ACCEPT_CHARSET => "utf-8",
48
+ USER_AGENT => self.configuration.user_agent,
49
+ # Due to error in Yandex.Webmaster API I had to change this header
50
+ # http://clubs.ya.ru/webmaster-api/replies.xml?item_no=150
51
+ # CONTENT_TYPE => 'application/xml'
52
+ CONTENT_TYPE => 'application/x-www-form-urlencoded'
53
+ },
54
+ :ssl => options.fetch(:ssl) { self.configuration.ssl },
55
+ :url => options.fetch(:endpoint) { self.configuration.endpoint }
56
+ }.merge(options)
57
+ end
58
+
59
+ # Exposes middleware builder to facilitate custom stacks and easy
60
+ # addition of new extensions such as cache adapter.
61
+ #
62
+ def stack(&block)
63
+ @stack ||= begin
64
+ block_given? ? Faraday::Builder.new(&block) : Faraday::Builder.new(&default_middleware)
65
+ end
66
+ end
67
+
68
+ # Default middleware stack that uses default adapter as specified at
69
+ # configuration stage.
70
+ #
71
+ def default_middleware
72
+ Proc.new do |builder|
73
+ builder.use Faraday::Request::Multipart
74
+ builder.use Faraday::Request::UrlEncoded
75
+ builder.use Webmaster::Request::OAuth2, self.configuration.oauth_token
76
+
77
+ builder.use Faraday::Response::Logger if ENV['DEBUG']
78
+ builder.use Webmaster::Response::Hashify
79
+ # builder.use Webmaster::Response::RaiseError
80
+
81
+ builder.adapter self.configuration.adapter
82
+ end
83
+ end
84
+
85
+ end
86
+ end
87
+ end