Module: Yandex::API::Disk
- Defined in:
- lib/yandex-api/disk.rb
Defined Under Namespace
Classes: BaseStruct, Folder, Object, Storage
Constant Summary
collapse
- URL_API =
'https://cloud-api.yandex.net/v1/disk'
Class Method Summary
collapse
Class Method Details
.configuration ⇒ Object
7
8
9
10
11
12
13
14
|
# File 'lib/yandex-api/disk.rb', line 7
def self.configuration
if defined? @environment
raise RuntimeError.new("not configured Yandex.Disk for #{@environment} enviroment") unless @configuration
else
raise RuntimeError.new('not configured Yandex.Disk') unless @configuration
end
@configuration
end
|
.connection ⇒ Object
30
31
32
33
34
35
36
37
38
39
|
# File 'lib/yandex-api/disk.rb', line 30
def self.connection
return @connection if defined? @connection
uri = URI.parse(URL_API)
@connection = Net::HTTP.new(uri.host, uri.port)
if uri.scheme == "https"
@connection.use_ssl = true
@connection.verify_mode = OpenSSL::SSL::VERIFY_NONE
end
return @connection
end
|
.load(file, env = nil) ⇒ Object
16
17
18
19
20
|
# File 'lib/yandex-api/disk.rb', line 16
def self.load file, env = nil
@environment = env.to_s if env
config = YAML.load_file(file)
@configuration = defined?(@environment) ? config[@environment] : config
end
|
.parse_json(json) ⇒ Object
22
23
24
25
26
27
28
|
# File 'lib/yandex-api/disk.rb', line 22
def self.parse_json json
begin
return JSON.parse(json)
rescue => e
raise RuntimeError.new "#{e.message} in response"
end
end
|
.request(method, path, params = {}) ⇒ Object
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
|
# File 'lib/yandex-api/disk.rb', line 41
def self.request method, path, params = {}
= {
'Authorization' => "OAuth #{configuration['token']}",
'Content-Type' => 'application/json'
}
response = case method.to_sym
when :put, :post then connection.send(method.to_sym, path, nil, )
when :delete, :get then connection.send(method.to_sym, path, )
end
json = self.parse_json(response.body || "{}")
unless [Net::HTTPNoContent, Net::HTTPOK, Net::HTTPCreated].include? response.class
raise RuntimeError.new "#{json['description']}"
else
return json
end
end
|
.upload(stream, filename, url) ⇒ Object
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
|
# File 'lib/yandex-api/disk.rb', line 58
def self.upload(stream, filename, url)
uri = URI.parse(url)
connection = Net::HTTP.new(uri.host, uri.port)
if uri.scheme == "https"
connection.use_ssl = true
connection.verify_mode = OpenSSL::SSL::VERIFY_NONE
end
request = Net::HTTP::Post.new uri
boundary = "RubyClient#{rand(999999)}"
body = []
body << "------#{boundary}"
body << "Content-Disposition: form-data; name=\"file\"; filename=\"#{filename}\""
body << 'Content-Type: text/plain'
body << ''
body << stream.read
body << "------#{boundary}--"
request.body = body.join("\r\n")
request.content_type = "multipart/form-data; boundary=----#{boundary}"
response = connection.request(request)
unless response.kind_of? Net::HTTPCreated
raise RuntimeError.new "#{response.body}"
end
true
end
|