mailru_target 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 85a6716f3c1fa1e7b99659497cfd6c26bcd5048e
4
+ data.tar.gz: d226c4a4d7684fcddd4faa29ef1916f713eea09c
5
+ SHA512:
6
+ metadata.gz: d6c16aa41301f8061837c7189b924c2f9b3d2f45e359f1c2769f122b9278a0213db26f9ab232e5ad490f2de60190fd5b736a2651860a02c2f3c4c1fb86bf2a34
7
+ data.tar.gz: 7effe0ba7117f40712117b629dad5aed7d1c2a5435ae808c862e39232adbb94be095483b0fb00388c0464ec091f5e9e8941958d3870981aa6097b41d979289ab
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ Gemfile.lock
2
+ *.sublime-*
3
+ .DS_Store
4
+ pkg
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in mailru_target.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Eugeniy Belyaev
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,47 @@
1
+ # MailruTarget
2
+
3
+ ## Installation
4
+
5
+ Add this line to your application's Gemfile:
6
+
7
+ gem 'mailru_target'
8
+
9
+ And then execute:
10
+
11
+ $ bundle
12
+
13
+ Or install it yourself as:
14
+
15
+ $ gem install mailru_target
16
+
17
+ ## Usage
18
+
19
+ MailruTarget.client_id = YOUR_CLIENT_ID
20
+ MailruTarget.client_secret = YOUR_CLIENT_SECRET_KEY
21
+
22
+ Get authorize url and redirect user to it.
23
+
24
+ MailruTarget::Auth.authorize_url
25
+
26
+ Recieve authentication code and request token:
27
+
28
+ MailruTarget::Auth.get_token code
29
+ => {"access_token" => "xxx", "token_type" => "Bearer", "expires_in" => 86400, "refresh_token" => "xxx"}
30
+
31
+ Use refresh_token to update current token after it expires
32
+
33
+ MailruTarget::Auth.refresh_token code
34
+ => {"access_token" => "xxx", "token_type" => "Bearer", "expires_in" => 86400, "refresh_token" => "xxx"}
35
+
36
+ Initialize new session and request restful resources:
37
+
38
+ session = MailruTarget::Session.new(token)
39
+ session.request :get, "/campaigns", status: "active"
40
+
41
+ ## Contributing
42
+
43
+ 1. Fork it ( https://github.com/[my-github-username]/mailru_target/fork )
44
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
45
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
46
+ 4. Push to the branch (`git push origin my-new-feature`)
47
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,25 @@
1
+ # see https://target.mail.ru/doc/api/oauth2
2
+
3
+ module MailruTarget
4
+ class Auth
5
+ class << self
6
+ include MailruTarget::Request
7
+
8
+ def authorize_url
9
+ state = (0...32).map { (65 + rand(26)).chr }.join.downcase
10
+ "https://target.mail.ru/oauth2/authorize?response_type=code" <<
11
+ "&client_id=#{MailruTarget.client_id}&state=#{state}&scope=#{MailruTarget.scopes}"
12
+ end
13
+
14
+ def get_token(code)
15
+ params = { grant_type: "authorization_code", code: code, v: 2 }
16
+ request :post, "/oauth2/token", params
17
+ end
18
+
19
+ def refresh_token(code)
20
+ params = { grant_type: "refresh_token", refresh_token: code, v: 2 }
21
+ request :post, "/oauth2/token", params
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,11 @@
1
+ module MailRu
2
+ class ConnectionError < Exception
3
+ def initialize(e)
4
+ @exception = e
5
+ end
6
+
7
+ def message
8
+ @exception.message
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,16 @@
1
+ module MailruTarget
2
+ class RequestError < Exception
3
+ def initialize(e)
4
+ super build_message e
5
+ end
6
+
7
+ private
8
+
9
+ def build_message(e)
10
+ body = JSON.parse e.response
11
+ "#{body['error']} : #{body['error_description']}" if body['error']
12
+ rescue
13
+ e.response
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,42 @@
1
+ module MailruTarget
2
+ module Request
3
+ API_URI = "https://target.mail.ru/api"
4
+
5
+ def request(method, path, params = {}, headers = {})
6
+ JSON.parse make_request(method, path, params, headers).to_s
7
+ end
8
+
9
+ def make_request(method, path, params = {}, headers = {})
10
+ begin
11
+ RestClient.send *build(method, path, params, headers)
12
+
13
+ rescue RestClient::Unauthorized,
14
+ RestClient::Forbidden,
15
+ RestClient::BadRequest,
16
+ RestClient::ResourceNotFound => e
17
+ raise MailruTarget::RequestError.new e
18
+
19
+ rescue SocketError => e
20
+ raise MailruTarget::ConnectionError.new e
21
+ end
22
+ end
23
+
24
+ private
25
+
26
+ def build(method, path, params, headers)
27
+ path = API_URI + "/v#{params.delete(:v) || 1}" + path
28
+ path << ".json" unless path.split("/").last["."]
29
+
30
+ if params[:token]
31
+ headers[:Authorization] = "Bearer #{params.delete :token}"
32
+ else
33
+ params.merge! client_id: MailruTarget.client_id, client_secret: MailruTarget.client_secret
34
+ end
35
+
36
+ case method
37
+ when :get then [:get, path, { params: params }.merge(headers)]
38
+ when :post then [:post, path, params, headers]
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,17 @@
1
+ # see https://target.mail.ru/doc/api/detailed/
2
+
3
+ module MailruTarget
4
+ class Session
5
+ include MailruTarget::Request
6
+
7
+ attr_accessor :token
8
+
9
+ def initialize(token)
10
+ @token = token
11
+ end
12
+
13
+ def request(method, path, params = {})
14
+ super method, path, params.merge({ token: token })
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,3 @@
1
+ module MailruTarget
2
+ VERSION = "0.0.2"
3
+ end
@@ -0,0 +1,20 @@
1
+ require 'restclient'
2
+ require 'json'
3
+
4
+ module MailruTarget
5
+
6
+ autoload :Auth, 'mailru_target/auth'
7
+ autoload :Request, 'mailru_target/request'
8
+ autoload :Session, 'mailru_target/session'
9
+
10
+ autoload :ConnectionError, 'mailru_target/errors/connection_error'
11
+ autoload :RequestError, 'mailru_target/errors/request_error'
12
+
13
+ class << self
14
+ attr_accessor :client_id, :client_secret, :scopes
15
+
16
+ def scopes
17
+ @scopes || 'read_ads,read_payments,create_ads'
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'mailru_target/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "mailru_target"
8
+ spec.version = MailruTarget::VERSION
9
+ spec.authors = ["Eugeniy Belyaev"]
10
+ spec.email = ["eugeniy.b@garin-studio.ru"]
11
+ spec.summary = %q{Target.Mail.ru api via oauth2}
12
+ spec.description = %q{Target.Mail.ru api via oauth2}
13
+ spec.homepage = "https://github.com/zhekanax/mailru_target"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency "rest-client"
22
+ spec.add_dependency "json"
23
+
24
+ spec.add_development_dependency "bundler", "~> 1.6"
25
+ spec.add_development_dependency "rake"
26
+ end
metadata ADDED
@@ -0,0 +1,113 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mailru_target
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Eugeniy Belyaev
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-10-05 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rest-client
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: json
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: '1.6'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '1.6'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: Target.Mail.ru api via oauth2
70
+ email:
71
+ - eugeniy.b@garin-studio.ru
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - .gitignore
77
+ - Gemfile
78
+ - LICENSE.txt
79
+ - README.md
80
+ - Rakefile
81
+ - lib/mailru_target.rb
82
+ - lib/mailru_target/auth.rb
83
+ - lib/mailru_target/errors/connection_error.rb
84
+ - lib/mailru_target/errors/request_error.rb
85
+ - lib/mailru_target/request.rb
86
+ - lib/mailru_target/session.rb
87
+ - lib/mailru_target/version.rb
88
+ - mailru_target.gemspec
89
+ homepage: https://github.com/zhekanax/mailru_target
90
+ licenses:
91
+ - MIT
92
+ metadata: {}
93
+ post_install_message:
94
+ rdoc_options: []
95
+ require_paths:
96
+ - lib
97
+ required_ruby_version: !ruby/object:Gem::Requirement
98
+ requirements:
99
+ - - '>='
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ required_rubygems_version: !ruby/object:Gem::Requirement
103
+ requirements:
104
+ - - '>='
105
+ - !ruby/object:Gem::Version
106
+ version: '0'
107
+ requirements: []
108
+ rubyforge_project:
109
+ rubygems_version: 2.2.2
110
+ signing_key:
111
+ specification_version: 4
112
+ summary: Target.Mail.ru api via oauth2
113
+ test_files: []