digitsend 0.0.1 → 0.0.2

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 +65 -15
  3. metadata +2 -2
@@ -1,3 +1,3 @@
1
1
  module Digitsend
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
data/lib/digitsend.rb CHANGED
@@ -1,16 +1,28 @@
1
1
  require "digitsend/version"
2
2
  require "net/http"
3
+ require "net/http/post/multipart"
3
4
  require "json"
4
5
 
5
6
  module DigitSend
6
7
  class Config
8
+ @host = 'digitsend.com'
9
+ @use_ssl = true
10
+
7
11
  class <<self
8
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
9
20
  end
10
21
  end
11
22
 
12
23
  class Message
13
24
  def initialize
25
+ @attachments = []
14
26
  yield self
15
27
  end
16
28
 
@@ -20,23 +32,61 @@ module DigitSend
20
32
 
21
33
  attr_accessor :to, :cc, :subject, :body
22
34
 
35
+ def add_file(path)
36
+ @attachments << path
37
+ end
38
+
23
39
  def send
24
- http = Net::HTTP.new('digitsend.com', 443)
25
- http.use_ssl = true
26
-
27
- payload = {
28
- message: {
29
- to: to,
30
- cc: cc,
31
- subject: subject,
32
- body: body
33
- }
40
+ api_call :post, '/api/messages', message: {
41
+ to: to,
42
+ cc: cc,
43
+ subject: subject,
44
+ body: body,
45
+ s3_file_uuids: @attachments.collect { |path| s3_file_uuid(path) }
34
46
  }
35
-
36
- http.post '/api/messages', payload.to_json,
37
- 'Content-Type' => 'application/json',
38
- 'Accept' => 'application/vnd.digitsend.v1',
39
- 'Authorization' => %Q[Token token="#{Config.api_token}"]
40
47
  end
48
+
49
+ private
50
+
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)
73
+ end
74
+ end
75
+
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
83
+
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
41
91
  end
42
92
  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.1
4
+ version: 0.0.2
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-26 00:00:00.000000000 Z
12
+ date: 2012-08-29 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: client library for DigitSend.
15
15
  email: