yandex-api 0.0.5 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a507df6506f1d68f303565a994fce28daa38bde6
4
- data.tar.gz: 811573b2767650e970b5dcb44f10f454c1ccafb4
3
+ metadata.gz: d3170cfe271a1c0ee6e7a8dd0d7fc5ee5d9db9cf
4
+ data.tar.gz: 9ef993dacfc37a3ff1a45c4734550d0e8b504569
5
5
  SHA512:
6
- metadata.gz: eb6dceefb263d50bfb5126be0d288b9a8e928fe1dd46bcdaf3fbf7934decf603b9007bdd2bf2efb2d6bfa18d1e894c4d007f55b03ee4f80c69feaad7f83250f0
7
- data.tar.gz: 76aac042480b9f89a53155d2e735a33847158a0520feea52c0978164a22732fc80937be74b003e8ae89ebab5baa7ecd14c596cfc2867ef7d6560622a8ec81fde
6
+ metadata.gz: 56b3119bd8862b4db32262da85fc81301a007473fdc9adc264e9c4b5f85b83fceacd467272a01d99341917c632a89a5dc39914b8916321c2b4f670c6b5b61ab5
7
+ data.tar.gz: 068b81459f4bac5e5ba21f697792f7e618cbe0c054e754006370f0a338b77685c3a44c443e6bda1947aad71cbdc788f9482fa92deae35889a45f7d9df6ec8d1d
data/.gitignore CHANGED
@@ -1,19 +1,7 @@
1
- *.gem
2
- *.rbc
3
- .bundle
4
- .config
5
- .yardoc
6
1
  Gemfile.lock
7
- InstalledFiles
8
- _yardoc
9
- coverage
10
- doc/
11
- lib/bundler/man
12
2
  pkg
13
- rdoc
14
- spec/reports
15
- test/tmp
16
- test/version_tmp
17
- tmp
18
3
  yandex_direct.yml
19
4
  yandex_translate.yml
5
+ .ruby-version
6
+ yandex-api.iml
7
+ spec/yandex_disk.yml
data/README.md CHANGED
@@ -2,31 +2,34 @@
2
2
 
3
3
  <a href="http://badge.fury.io/rb/yandex-api"><img src="https://badge.fury.io/rb/yandex-api@2x.png" alt="Gem Version" height="18"></a>
4
4
 
5
- Позволяет работать с сервисами Яндекс доступными через API
5
+ Allow you work with any Yandex.APIs
6
6
 
7
- Доступные модули:
8
- * Direct - Автоматизация рекламных кампаний в Яндекс.Директе (http://direct.yandex.ru/)
9
- * Translate - Позволяет получить доступ к онлайн-сервису машинного перевода Яндекса (http://translate.yandex.ru/)
7
+ Modules:
10
8
 
11
- ## Установка
9
+ * Direct - contain classes and methods for work with Yandex.Direct (http://direct.yandex.ru/)
10
+ * Translate - contain methods for work with Yandex.Translate (http://translate.yandex.ru/)
12
11
 
13
- Добавить в Gemfile эту строчку:
14
12
 
15
- gem 'yandex-api'
13
+ ## Installation
16
14
 
17
- Выполнить команду:
15
+ Add this line to your application's Gemfile:
16
+
17
+ ```
18
+ gem 'yandex-api'
19
+ ```
20
+
21
+ And then execute:
18
22
 
19
23
  $ bundle
20
24
 
21
- Или уставновить через gem:
25
+ Or install it yourself as:
22
26
 
23
27
  $ gem install yandex-api
24
28
 
25
-
26
29
  ## Direct
27
30
  ### в Ruby:
28
31
 
29
- Создать конфигурационный файл yandex_direct.yml
32
+ Create configuration file yandex_direct.yml
30
33
 
31
34
  token: token
32
35
  login: login
@@ -36,7 +39,7 @@
36
39
 
37
40
  ### в Ruby On Rails:
38
41
 
39
- Создать конфигурационный файл yandex_direct.yml
42
+ Create configuration file yandex_direct.yml
40
43
 
41
44
  development:
42
45
  token: token
@@ -45,11 +48,11 @@
45
48
  verbose: true
46
49
  sandbox: true
47
50
 
48
- Добавить в initializers файл yandex_direct.rb
51
+ Create yandex_direct.rb in config/initializers
49
52
 
50
53
  Yandex::API::Direct.load File.join(Rails.root,"config","yandex_direct.yml"), Rails.env
51
54
 
52
- ### Пример работы:
55
+ ### Simple example:
53
56
 
54
57
  require 'yandex-api'
55
58
  Yandex::API::Direct.load "yandex_direct.yml"
@@ -61,7 +64,7 @@
61
64
  ## Translate
62
65
  ### в Ruby:
63
66
 
64
- Создать конфигурационный файл yandex_direct.yml
67
+ Create configuration file yandex_translate.yml
65
68
 
66
69
  token: "token"
67
70
  ui: true
@@ -69,19 +72,19 @@
69
72
 
70
73
  ### в Ruby On Rails:
71
74
 
72
- Создать конфигурационный файл yandex_translate.yml
75
+ Create configuration file yandex_translate.yml
73
76
 
74
77
  development:
75
78
  token: "token"
76
79
  ui: "ru"
77
80
  verbose: true
78
81
 
79
- Добавить в initializers файл yandex_translate.rb
82
+ Create yandex_translate.rb in config/initializers
80
83
 
81
84
  Yandex::API::Translate.load File.join(Rails.root,"config","yandex_translate.yml"), Rails.env
82
85
 
83
86
 
84
- ### Пример работы
87
+ ### Simple example
85
88
 
86
89
  require 'yandex-api'
87
90
  Yandex::API::Translate.load "yandex.yml", "production"
@@ -3,8 +3,8 @@ require 'yandex-api/direct'
3
3
  require 'yandex-api/direct/base'
4
4
  require 'yandex-api/direct/banner_info'
5
5
  require 'yandex-api/direct/campaign_info'
6
-
7
6
  require 'yandex-api/translate'
7
+ require 'yandex-api/disk'
8
8
 
9
9
  module Yandex
10
10
  module API
@@ -7,6 +7,9 @@ require 'uri'
7
7
  module Yandex
8
8
  module API
9
9
  module Direct
10
+
11
+
12
+
10
13
  URL_API = 'https://api.direct.yandex.ru/v4/json/'
11
14
  URL_API_SANDBOX = 'https://api-sandbox.direct.yandex.ru/v4/json/'
12
15
 
@@ -0,0 +1,189 @@
1
+ require 'time'
2
+ module Yandex
3
+ module API
4
+ module Disk
5
+ URL_API = 'https://cloud-api.yandex.net/v1/disk'
6
+
7
+ def self.configuration
8
+ if defined? @environment
9
+ raise RuntimeError.new("not configured Yandex.Disk for #{@environment} enviroment") unless @configuration
10
+ else
11
+ raise RuntimeError.new('not configured Yandex.Disk') unless @configuration
12
+ end
13
+ @configuration
14
+ end
15
+
16
+ def self.load file, env = nil
17
+ @environment = env.to_s if env
18
+ config = YAML.load_file(file)
19
+ @configuration = defined?(@environment) ? config[@environment] : config
20
+ end
21
+
22
+ def self.parse_json json
23
+ begin
24
+ return JSON.parse(json)
25
+ rescue => e
26
+ raise RuntimeError.new "#{e.message} in response"
27
+ end
28
+ end
29
+
30
+ def self.connection
31
+ return @connection if defined? @connection
32
+ uri = URI.parse(URL_API)
33
+ @connection = Net::HTTP.new(uri.host, uri.port)
34
+ if uri.scheme == "https"
35
+ @connection.use_ssl = true
36
+ @connection.verify_mode = OpenSSL::SSL::VERIFY_NONE
37
+ end
38
+ return @connection
39
+ end
40
+
41
+ def self.request method, path, params = {}
42
+ headers = {
43
+ 'Authorization' => "OAuth #{configuration['token']}",
44
+ 'Content-Type' => 'application/json'
45
+ }
46
+ response = case method.to_sym
47
+ when :put, :post then connection.send(method.to_sym, path, nil, headers)
48
+ when :delete, :get then connection.send(method.to_sym, path, headers)
49
+ end
50
+ json = self.parse_json(response.body || "{}")
51
+ unless [Net::HTTPNoContent, Net::HTTPOK, Net::HTTPCreated].include? response.class
52
+ raise RuntimeError.new "#{json['description']}"
53
+ else
54
+ return json
55
+ end
56
+ end
57
+
58
+ def self.upload(stream, filename, url)
59
+ uri = URI.parse(url)
60
+ connection = Net::HTTP.new(uri.host, uri.port)
61
+ if uri.scheme == "https"
62
+ connection.use_ssl = true
63
+ connection.verify_mode = OpenSSL::SSL::VERIFY_NONE
64
+ end
65
+ request = Net::HTTP::Post.new uri
66
+ boundary = "RubyClient#{rand(999999)}"
67
+ body = []
68
+ body << "------#{boundary}"
69
+ body << "Content-Disposition: form-data; name=\"file\"; filename=\"#{filename}\""
70
+ body << 'Content-Type: text/plain'
71
+ body << ''
72
+ body << stream.read
73
+ body << "------#{boundary}--"
74
+ request.body = body.join("\r\n")
75
+ request.content_type = "multipart/form-data; boundary=----#{boundary}"
76
+ response = connection.request(request)
77
+ unless response.kind_of? Net::HTTPCreated
78
+ raise RuntimeError.new "#{response.body}"
79
+ end
80
+ true
81
+ end
82
+
83
+ class BaseStruct < Struct
84
+ def initialize(hash={})
85
+ hash.each do |key, value|
86
+ if self.respond_to?("#{key}=")
87
+ if value =~ /\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\+\d{2}:\d{2}/
88
+ self.send("#{key}=", Time.parse(value))
89
+ else
90
+ self.send("#{key}=", value)
91
+ end
92
+ end
93
+ end
94
+ end
95
+ end
96
+
97
+ class Object < BaseStruct.new(:name, :size, :created, :modified, :mime_type, :md5, :media_type, :path)
98
+ end
99
+
100
+ class Folder < BaseStruct.new(:name, :path, :modified, :created)
101
+ end
102
+
103
+ class Storage < BaseStruct.new(:trash_size, :total_space, :used_space)
104
+ def initialize
105
+ super Disk::request(:get, '/v1/disk/')
106
+ end
107
+
108
+ class << self
109
+ def ls(path)
110
+ response = Disk::request(:get, "/v1/disk/resources?path=#{path}")['_embedded'] || {}
111
+ response['items'] ||= []
112
+ response['items'].collect do |item|
113
+ case item['type']
114
+ when 'dir' then
115
+ Disk::Folder.new(item)
116
+ when 'file' then
117
+ Disk::Object.new(item)
118
+ end
119
+ end
120
+ end
121
+ def exists?(path)
122
+ Disk::request :get, "/v1/disk/resources?path=#{path}"
123
+ end
124
+
125
+ def mkdir!(path)
126
+ Disk::request :put, "/v1/disk/resources?path=#{path}"
127
+ true
128
+ end
129
+ def mkdir(path)
130
+ mkdir!(path) rescue false
131
+ end
132
+
133
+ def rm!(path)
134
+ Disk::request :delete, "/v1/disk/resources?path=#{path}"
135
+ true
136
+ end
137
+ def rm(path)
138
+ rm!(path) rescue false
139
+ end
140
+
141
+ def copy!(from, to)
142
+ Disk::request :post, "/v1/disk/resources/copy?from=#{from}&path=#{to}"
143
+ true
144
+ end
145
+ def copy(from, to)
146
+ copy!(from, to) rescue false
147
+ end
148
+
149
+ def move!(from, to)
150
+ Disk::request :post, "/v1/disk/resources/move?from=#{from}&path=#{to}"
151
+ true
152
+ end
153
+ def move(from, to)
154
+ move!(from,to) rescue false
155
+ end
156
+
157
+ def write!(file, to)
158
+ params = Disk::request :get, "/v1/disk/resources/upload?path=#{to}"
159
+ Disk::upload(file, to.split('/').last, params['href'])
160
+ true
161
+ end
162
+ def write(file, to)
163
+ write!(file, to) rescue false
164
+ end
165
+
166
+ def clean!(path = nil)
167
+ if path.nil?
168
+ Disk::request :delete, "/v1/disk/trash/resources"
169
+ else
170
+ Disk::request :delete, "/v1/disk/trash/resources?path=#{path}"
171
+ end
172
+ true
173
+ end
174
+ def clean(path = nil)
175
+ clean!(path) rescue false
176
+ end
177
+
178
+ def restore!(path, to = path)
179
+ Disk::request :put, "/v1/disk/trash/resources/restore?path=#{to}&name=#{path}"
180
+ true
181
+ end
182
+ def restore(path, to = path)
183
+ restore!(path, to) rescue false
184
+ end
185
+ end
186
+ end
187
+ end
188
+ end
189
+ end
@@ -1,5 +1,5 @@
1
1
  module Yandex
2
2
  module API
3
- VERSION = '0.0.5'
3
+ VERSION = '0.1.0'
4
4
  end
5
5
  end
@@ -0,0 +1,54 @@
1
+ require 'spec_helper'
2
+ include Yandex::API::Disk
3
+
4
+ describe Yandex::API::Disk do
5
+ before { Yandex::API::Disk.load File.join(File.dirname(__FILE__),'yandex_disk.yml'), :test }
6
+ it 'config' do
7
+ expect {Storage.new}.not_to raise_error
8
+ end
9
+ it 'ls' do
10
+ expect(Storage.ls('disk:/').any?).to be_truthy
11
+ end
12
+ it 'create/remove directory' do
13
+ expect(Storage.mkdir!('disk:/test')).to eql(true)
14
+ expect(Storage.rm!('disk:/test')).to eql(true)
15
+ expect(Storage.clean!('test')).to eql(true)
16
+ end
17
+ it 'write/remove file' do
18
+ file = File.open(__FILE__)
19
+ expect(Storage.write!(file, 'disk:/test.rb')).to eql(true)
20
+ expect(Storage.rm!('disk:/test.rb')).to eql(true)
21
+ expect(Storage.clean!('test.rb')).to eql(true)
22
+ end
23
+ it 'write/move file' do
24
+ file = File.open(__FILE__)
25
+ expect(Storage.write!(file, 'disk:/test.rb')).to eql(true)
26
+ expect(Storage.move!('disk:/test.rb', 'disk:/test33.rb')).to eql(true)
27
+ expect(Storage.rm!('disk:/test33.rb')).to eql(true)
28
+ expect(Storage.clean!('test33.rb')).to eql(true)
29
+ end
30
+ it 'write/copy file' do
31
+ file = File.open(__FILE__)
32
+ expect(Storage.write!(file, 'disk:/test.rb')).to eql(true)
33
+ expect(Storage.copy!('disk:/test.rb', 'disk:/test33.rb')).to eql(true)
34
+ expect(Storage.rm!('disk:/test33.rb')).to eql(true)
35
+ expect(Storage.rm!('disk:/test.rb')).to eql(true)
36
+ expect(Storage.clean!('test.rb')).to eql(true)
37
+ expect(Storage.clean!('test33.rb')).to eql(true)
38
+ end
39
+ it 'write/remove/clean' do
40
+ file = File.open(__FILE__)
41
+ expect(Storage.write!(file, 'disk:/test.rb')).to eql(true)
42
+ expect(Storage.rm!('disk:/test.rb')).to eql(true)
43
+ expect(Storage.clean!('test.rb')).to eql(true)
44
+ end
45
+
46
+ it 'write/remove/restore' do
47
+ file = File.open(__FILE__)
48
+ expect(Storage.write!(file, 'disk:/test.rb')).to eql(true)
49
+ expect(Storage.rm!('disk:/test.rb')).to eql(true)
50
+ expect(Storage.restore!('/test.rb')).to eql(true)
51
+ expect(Storage.rm!('disk:/test.rb')).to eql(true)
52
+ expect(Storage.clean!('test.rb')).to eql(true)
53
+ end
54
+ end
metadata CHANGED
@@ -1,55 +1,55 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: yandex-api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Evgeniy Shurmin
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-01-20 00:00:00.000000000 Z
11
+ date: 2015-03-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - "~>"
17
+ - - ~>
18
18
  - !ruby/object:Gem::Version
19
19
  version: '1.7'
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - "~>"
24
+ - - ~>
25
25
  - !ruby/object:Gem::Version
26
26
  version: '1.7'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: rake
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - "~>"
31
+ - - ~>
32
32
  - !ruby/object:Gem::Version
33
33
  version: '10.0'
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - "~>"
38
+ - - ~>
39
39
  - !ruby/object:Gem::Version
40
40
  version: '10.0'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: rspec
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - ">="
45
+ - - '>='
46
46
  - !ruby/object:Gem::Version
47
47
  version: '0'
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
- - - ">="
52
+ - - '>='
53
53
  - !ruby/object:Gem::Version
54
54
  version: '0'
55
55
  description: Yandex.API integration
@@ -59,7 +59,7 @@ executables: []
59
59
  extensions: []
60
60
  extra_rdoc_files: []
61
61
  files:
62
- - ".gitignore"
62
+ - .gitignore
63
63
  - Gemfile
64
64
  - LICENSE
65
65
  - README.md
@@ -71,14 +71,14 @@ files:
71
71
  - lib/yandex-api/direct/banner_info.rb
72
72
  - lib/yandex-api/direct/base.rb
73
73
  - lib/yandex-api/direct/campaign_info.rb
74
+ - lib/yandex-api/disk.rb
74
75
  - lib/yandex-api/translate.rb
75
76
  - lib/yandex-api/version.rb
76
77
  - spec/direct_spec.rb
78
+ - spec/disk_spec.rb
77
79
  - spec/spec_helper.rb
78
80
  - spec/translate_spec.rb
79
- - test.rb
80
81
  - yandex-api.gemspec
81
- - yandex.yml
82
82
  homepage: https://github.com/jpascal/yandex-api
83
83
  licenses: []
84
84
  metadata: {}
@@ -88,12 +88,12 @@ require_paths:
88
88
  - lib
89
89
  required_ruby_version: !ruby/object:Gem::Requirement
90
90
  requirements:
91
- - - ">="
91
+ - - '>='
92
92
  - !ruby/object:Gem::Version
93
93
  version: '0'
94
94
  required_rubygems_version: !ruby/object:Gem::Requirement
95
95
  requirements:
96
- - - ">="
96
+ - - '>='
97
97
  - !ruby/object:Gem::Version
98
98
  version: '0'
99
99
  requirements: []
@@ -104,5 +104,6 @@ specification_version: 4
104
104
  summary: Yandex.API integration
105
105
  test_files:
106
106
  - spec/direct_spec.rb
107
+ - spec/disk_spec.rb
107
108
  - spec/spec_helper.rb
108
109
  - spec/translate_spec.rb
data/test.rb DELETED
File without changes
data/yandex.yml DELETED
@@ -1,2 +0,0 @@
1
- production:
2
- token: b89f01551bb74bff8a49f26dd0f571f4