digitsend 0.0.2 → 0.0.3

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