digitsend 0.0.4 → 0.0.5
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/digitsend/client.rb +62 -0
- data/lib/digitsend/config.rb +17 -0
- data/lib/digitsend/message.rb +35 -0
- data/lib/digitsend/repository.rb +12 -0
- data/lib/digitsend/version.rb +1 -1
- data/lib/digitsend.rb +5 -124
- metadata +6 -2
@@ -0,0 +1,62 @@
|
|
1
|
+
require "net/http"
|
2
|
+
require "net/http/post/multipart"
|
3
|
+
require "json"
|
4
|
+
|
5
|
+
module DigitSend
|
6
|
+
class Client
|
7
|
+
class <<self
|
8
|
+
def call(path, params)
|
9
|
+
http = Net::HTTP.new(Config.host, Config.port)
|
10
|
+
http.use_ssl = Config.use_ssl
|
11
|
+
|
12
|
+
response = http.post path, params && params.to_json,
|
13
|
+
'Content-Type' => 'application/json',
|
14
|
+
'Accept' => 'application/vnd.digitsend.v1',
|
15
|
+
'Authorization' => %Q[Token token="#{Config.api_token}"]
|
16
|
+
|
17
|
+
response.body.empty? ? nil : JSON.parse(response.body)
|
18
|
+
end
|
19
|
+
|
20
|
+
def upload_s3_file(path, data)
|
21
|
+
response = create_s3_file(File.basename(path))
|
22
|
+
|
23
|
+
upload_to_s3 \
|
24
|
+
URI.parse(response["url"]),
|
25
|
+
response["fields"],
|
26
|
+
stream_for_data(path, data)
|
27
|
+
|
28
|
+
response["uuid"]
|
29
|
+
end
|
30
|
+
|
31
|
+
private
|
32
|
+
|
33
|
+
def create_s3_file(filename)
|
34
|
+
Client.call '/api/s3_files', s3_file: { name: filename }
|
35
|
+
end
|
36
|
+
|
37
|
+
def upload_to_s3(url, fields, stream)
|
38
|
+
req = Net::HTTP::Post::Multipart.new \
|
39
|
+
url.to_s,
|
40
|
+
fields.merge("file" => UploadIO.new(stream, "binary/octet-stream"))
|
41
|
+
|
42
|
+
n = Net::HTTP.new(url.host, url.port)
|
43
|
+
n.use_ssl = true
|
44
|
+
n.verify_mode = OpenSSL::SSL::VERIFY_NONE
|
45
|
+
|
46
|
+
n.start do |http|
|
47
|
+
http.request(req)
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
def stream_for_data(path, data)
|
52
|
+
if data.nil?
|
53
|
+
File.open(path, "r")
|
54
|
+
elsif data.is_a?(String)
|
55
|
+
StringIO.new(data)
|
56
|
+
else
|
57
|
+
data
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module DigitSend
|
2
|
+
class Config
|
3
|
+
@host = 'api.digitsend.com'
|
4
|
+
@use_ssl = true
|
5
|
+
|
6
|
+
class <<self
|
7
|
+
attr_accessor :api_token
|
8
|
+
attr_accessor :host
|
9
|
+
attr_accessor :use_ssl
|
10
|
+
attr_writer :port
|
11
|
+
|
12
|
+
def port
|
13
|
+
@port || ( @use_ssl ? 443 : 80 )
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
module DigitSend
|
2
|
+
class Message
|
3
|
+
def initialize
|
4
|
+
@attachments = []
|
5
|
+
end
|
6
|
+
|
7
|
+
def self.send(&block)
|
8
|
+
new.tap { |m| yield(m) }.send
|
9
|
+
end
|
10
|
+
|
11
|
+
attr_accessor :to, :cc, :subject, :body
|
12
|
+
|
13
|
+
def add_file(filename, data = nil)
|
14
|
+
@attachments << [ filename, data ]
|
15
|
+
end
|
16
|
+
|
17
|
+
def send
|
18
|
+
Client.call '/api/messages', message: {
|
19
|
+
to: to,
|
20
|
+
cc: cc,
|
21
|
+
subject: subject,
|
22
|
+
body: body,
|
23
|
+
s3_file_uuids: s3_file_uuids
|
24
|
+
}
|
25
|
+
end
|
26
|
+
|
27
|
+
private
|
28
|
+
|
29
|
+
def s3_file_uuids
|
30
|
+
@attachments.collect do |filename, data|
|
31
|
+
Client.upload_s3_file(filename, data)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
module DigitSend
|
2
|
+
class Repository
|
3
|
+
def self.upload(repo_name, path, filename, data)
|
4
|
+
uuid = Client.upload_s3_file(filename, data)
|
5
|
+
|
6
|
+
Client.call '/api/files/versions',
|
7
|
+
repo_name: repo_name,
|
8
|
+
path: path,
|
9
|
+
repo_file_version: { s3_file_uuid: uuid }
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
data/lib/digitsend/version.rb
CHANGED
data/lib/digitsend.rb
CHANGED
@@ -1,124 +1,5 @@
|
|
1
|
-
require
|
2
|
-
require
|
3
|
-
require
|
4
|
-
require
|
5
|
-
|
6
|
-
module DigitSend
|
7
|
-
class Config
|
8
|
-
@host = 'digitsend.com'
|
9
|
-
@use_ssl = true
|
10
|
-
|
11
|
-
class <<self
|
12
|
-
attr_accessor :api_token
|
13
|
-
attr_accessor :host
|
14
|
-
attr_accessor :use_ssl
|
15
|
-
attr_writer :port
|
16
|
-
|
17
|
-
def port
|
18
|
-
@port || ( @use_ssl ? 443 : 80 )
|
19
|
-
end
|
20
|
-
end
|
21
|
-
end
|
22
|
-
|
23
|
-
class Client
|
24
|
-
class <<self
|
25
|
-
def call(path, params)
|
26
|
-
http = Net::HTTP.new(Config.host, Config.port)
|
27
|
-
http.use_ssl = Config.use_ssl
|
28
|
-
|
29
|
-
response = http.post path, params && params.to_json,
|
30
|
-
'Content-Type' => 'application/json',
|
31
|
-
'Accept' => 'application/vnd.digitsend.v1',
|
32
|
-
'Authorization' => %Q[Token token="#{Config.api_token}"]
|
33
|
-
|
34
|
-
response.body.empty? ? nil : JSON.parse(response.body)
|
35
|
-
end
|
36
|
-
|
37
|
-
def upload_s3_file(filename, data)
|
38
|
-
response = create_s3_file(filename)
|
39
|
-
|
40
|
-
upload_to_s3 \
|
41
|
-
URI.parse(response["url"]),
|
42
|
-
response["fields"],
|
43
|
-
stream_for_data(filename, data)
|
44
|
-
|
45
|
-
response["uuid"]
|
46
|
-
end
|
47
|
-
|
48
|
-
private
|
49
|
-
|
50
|
-
def create_s3_file(name)
|
51
|
-
Client.call '/api/s3_files', s3_file: { name: File.basename(name) }
|
52
|
-
end
|
53
|
-
|
54
|
-
def upload_to_s3(url, fields, stream)
|
55
|
-
req = Net::HTTP::Post::Multipart.new \
|
56
|
-
url.to_s,
|
57
|
-
fields.merge("file" => UploadIO.new(stream, "binary/octet-stream"))
|
58
|
-
|
59
|
-
n = Net::HTTP.new(url.host, url.port)
|
60
|
-
n.use_ssl = true
|
61
|
-
n.verify_mode = OpenSSL::SSL::VERIFY_NONE
|
62
|
-
|
63
|
-
n.start do |http|
|
64
|
-
http.request(req)
|
65
|
-
end
|
66
|
-
end
|
67
|
-
|
68
|
-
def stream_for_data(filename, data)
|
69
|
-
if data.nil?
|
70
|
-
File.open(filename, "r")
|
71
|
-
elsif data.is_a?(String)
|
72
|
-
StringIO.new(data)
|
73
|
-
else
|
74
|
-
data
|
75
|
-
end
|
76
|
-
end
|
77
|
-
end
|
78
|
-
end
|
79
|
-
|
80
|
-
class Message
|
81
|
-
def initialize
|
82
|
-
@attachments = []
|
83
|
-
end
|
84
|
-
|
85
|
-
def self.send(&block)
|
86
|
-
new.tap { |m| yield(m) }.send
|
87
|
-
end
|
88
|
-
|
89
|
-
attr_accessor :to, :cc, :subject, :body
|
90
|
-
|
91
|
-
def add_file(filename, data = nil)
|
92
|
-
@attachments << [ filename, data ]
|
93
|
-
end
|
94
|
-
|
95
|
-
def send
|
96
|
-
Client.call '/api/messages', message: {
|
97
|
-
to: to,
|
98
|
-
cc: cc,
|
99
|
-
subject: subject,
|
100
|
-
body: body,
|
101
|
-
s3_file_uuids: s3_file_uuids
|
102
|
-
}
|
103
|
-
end
|
104
|
-
|
105
|
-
private
|
106
|
-
|
107
|
-
def s3_file_uuids
|
108
|
-
@attachments.collect do |filename, data|
|
109
|
-
Client.upload_s3_file(filename, data)
|
110
|
-
end
|
111
|
-
end
|
112
|
-
end
|
113
|
-
|
114
|
-
class Repository
|
115
|
-
def self.upload(repo_name, path, filename, data)
|
116
|
-
uuid = Client.upload_s3_file(filename, data)
|
117
|
-
|
118
|
-
Client.call '/api/files/versions',
|
119
|
-
repo_name: repo_name,
|
120
|
-
path: path,
|
121
|
-
repo_file_version: { s3_file_uuid: uuid }
|
122
|
-
end
|
123
|
-
end
|
124
|
-
end
|
1
|
+
require 'digitsend/version'
|
2
|
+
require 'digitsend/config'
|
3
|
+
require 'digitsend/client'
|
4
|
+
require 'digitsend/message'
|
5
|
+
require 'digitsend/repository'
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: digitsend
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.5
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-09-
|
12
|
+
date: 2012-09-02 00:00:00.000000000 Z
|
13
13
|
dependencies: []
|
14
14
|
description: client library for DigitSend.
|
15
15
|
email:
|
@@ -25,6 +25,10 @@ files:
|
|
25
25
|
- Rakefile
|
26
26
|
- digitsend.gemspec
|
27
27
|
- lib/digitsend.rb
|
28
|
+
- lib/digitsend/client.rb
|
29
|
+
- lib/digitsend/config.rb
|
30
|
+
- lib/digitsend/message.rb
|
31
|
+
- lib/digitsend/repository.rb
|
28
32
|
- lib/digitsend/version.rb
|
29
33
|
homepage: ''
|
30
34
|
licenses: []
|