pdm 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -4,15 +4,16 @@ module Pdm
4
4
 
5
5
  require "cgi"
6
6
  require "uri"
7
- require "net/http/post/multipart"
7
+ require "rest_client"
8
8
  require "pdm/json"
9
9
 
10
10
 
11
- attr_accessor :endpoint, :api_key
11
+ attr_accessor :endpoint, :api_key, :accept
12
12
 
13
- def initialize(endpoint, api_key)
13
+ def initialize(endpoint, api_key, accept = :json)
14
14
  self.endpoint = endpoint
15
15
  self.api_key = api_key
16
+ self.accept = accept
16
17
  end
17
18
 
18
19
 
@@ -26,37 +27,27 @@ module Pdm
26
27
  URI.join(self.endpoint, path.to_s)
27
28
  end
28
29
 
29
- def make_query_string(path, params)
30
- params = params.collect { |k,v| "#{k}=#{CGI.escape(v.to_s)}" }
31
- params = params.join("&")
32
- [path, params].join("?")
33
- end
34
-
35
- def request(path, params = {})
30
+ def with_rest_client(path, params)
36
31
  uri = uri_from_path(path)
37
32
  params = decorate_params(params)
38
- Net::HTTP.start(uri.host, uri.port) do |http|
39
- req = yield(uri, params)
40
- http.request(req)
41
- end
33
+ yield(::RestClient, uri.to_s, params)
42
34
  end
43
35
 
44
- def get(path, params = {})
45
- request(path, params) do |uri, params|
46
- query_string = make_query_string(uri.path, params)
47
- Net::HTTP::Get.new(query_string)
36
+ def get(path, params)
37
+ with_rest_client(path, params) do |r, uri, params|
38
+ r.get(uri, params, :accept => accept)
48
39
  end
49
40
  end
50
-
51
- def multipart_post(path, params)
52
- request(path, params) do |uri, params|
53
- Net::HTTP::Post::Multipart.new(uri.path, params)
41
+
42
+ def post(path, params)
43
+ with_rest_client(path, params) do |r, uri, params|
44
+ r.post(uri, params, :accept => accept)
54
45
  end
55
46
  end
56
47
 
57
- def multipart_put(path, params)
58
- request(path, params) do |uri, params|
59
- Net::HTTP::Put::Multipart.new(uri.path, params)
48
+ def put(path, params)
49
+ with_rest_client(path, params) do |r, uri, params|
50
+ r.put(uri, params, :accept => accept)
60
51
  end
61
52
  end
62
53
  end
@@ -0,0 +1,30 @@
1
+ module Pdm
2
+ class Attachment
3
+
4
+ attr_accessor :mail_attachment
5
+ attr_accessor :eof
6
+
7
+ def initialize(mail_attachment)
8
+ self.mail_attachment = mail_attachment
9
+ self.eof = false
10
+ end
11
+
12
+
13
+ def eof?
14
+ self.eof == true
15
+ end
16
+
17
+ def read(bytes)
18
+ if eof?
19
+ nil
20
+ else
21
+ self.eof = true
22
+ mail_attachment.read
23
+ end
24
+ end
25
+
26
+ def path
27
+ mail_attachment.filename
28
+ end
29
+ end
30
+ end
@@ -3,7 +3,6 @@ require "mail/message"
3
3
  module Mail
4
4
  class Message
5
5
 
6
- attr_accessor :template_html
7
- attr_accessor :template_assigns
6
+ attr_accessor :personalization
8
7
  end
9
8
  end
@@ -4,6 +4,8 @@ module Pdm
4
4
 
5
5
  class Mail < Pdm::Resource
6
6
 
7
+ require "pdm/attachment"
8
+
7
9
  attr_accessor :guid, :mail
8
10
 
9
11
  def initialize(mail, options = {})
@@ -11,18 +13,22 @@ module Pdm
11
13
  super(options)
12
14
  end
13
15
 
16
+
17
+ def get_body_part(parts, content_type)
18
+ parts.detect do |p|
19
+ p.content_type.include?(content_type) &&
20
+ p.header[:content_disposition].nil?
21
+ end
22
+ end
14
23
 
15
24
  def get_body_parts(mail)
16
- parts = mail.body.parts
17
- plain_part = parts.detect { |p| p.content_type == "text/plain" }
18
- html_part = parts.detect { |p| p.content_type == "text/html" }
19
-
20
- if plain_part.blank? && html_part.blank?
21
- html_part = mail.body.to_s
25
+ [
26
+ "text/plain",
27
+ "text/html",
28
+ ].map do |content_type|
29
+ get_body_part(mail.parts, content_type)
22
30
  end
23
-
24
- [plain_part, html_part]
25
- end
31
+ end
26
32
 
27
33
  def basic_params(mail)
28
34
  {
@@ -34,39 +40,54 @@ module Pdm
34
40
  end
35
41
 
36
42
  def body_params(mail)
37
- plain_part, html_part = get_body_parts(mail)
43
+ plain_part, html_part = get_body_parts(mail).map do |p|
44
+ p && p.body.to_s
45
+ end
38
46
  {
39
47
  "job[plain]" => plain_part,
40
48
  "job[html]" => html_part,
41
49
  }
42
50
  end
43
51
 
44
- def template_params(mail)
45
- return {} if mail.template_html.nil?
52
+ def personalization_params(mail)
53
+ return {} if mail.personalization.nil?
46
54
  {
47
- "job[template_html]" => mail.template_html,
48
- "job[template_assigns]" => mail.template_assigns,
55
+ "job[personalization]" => mail.personalization,
49
56
  }
50
57
  end
51
-
58
+
59
+ def attachments_for_rest_client(attachments)
60
+ attachments.map do |attachment|
61
+ Pdm::Attachment.new(attachment)
62
+ end
63
+ end
64
+
65
+ def attachment_params(mail)
66
+ return {} if mail.attachments.empty?
67
+ {
68
+ "job[attachments]" => attachments_for_rest_client(mail.attachments),
69
+ }
70
+ end
71
+
52
72
  def create_and_send_job_params(mail)
53
73
  params = basic_params(mail)
54
74
  params.merge!(body_params(mail))
55
- params.merge!(template_params(mail))
75
+ params.merge!(personalization_params(mail))
76
+ params.merge!(attachment_params(mail))
56
77
  end
57
78
 
58
79
  def create_and_send_job(mail)
59
80
  params = create_and_send_job_params(mail)
60
81
  with_exceptions do
61
- api_request.multipart_post(:jobs, params)
82
+ api_request.post(:jobs, params)
62
83
  end
63
84
  end
64
85
 
65
86
  def deliver
66
87
  res = create_and_send_job(mail)
67
88
 
68
- case res
69
- when Net::HTTPSuccess
89
+ case res.code
90
+ when 200
70
91
  self.guid = res.body
71
92
  end
72
93
 
@@ -1,7 +1,7 @@
1
1
  module Pdm
2
2
 
3
3
  def version
4
- "0.0.2"
4
+ "0.0.3"
5
5
  end
6
6
  module_function :version
7
7
  end
@@ -18,5 +18,5 @@ Gem::Specification.new do |gem|
18
18
  gem.add_development_dependency("rake")
19
19
  gem.add_runtime_dependency("mail")
20
20
  gem.add_runtime_dependency("yajl-ruby")
21
- gem.add_runtime_dependency("multipart-post")
21
+ gem.add_runtime_dependency("rest-client")
22
22
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pdm
3
3
  version: !ruby/object:Gem::Version
4
- hash: 27
4
+ hash: 25
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 2
10
- version: 0.0.2
9
+ - 3
10
+ version: 0.0.3
11
11
  platform: ruby
12
12
  authors:
13
13
  - PipeDevMail
@@ -15,11 +15,10 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-11-20 00:00:00 +02:00
19
- default_executable:
18
+ date: 2011-11-26 00:00:00 Z
20
19
  dependencies:
21
20
  - !ruby/object:Gem::Dependency
22
- requirement: &id001 !ruby/object:Gem::Requirement
21
+ version_requirements: &id001 !ruby/object:Gem::Requirement
23
22
  none: false
24
23
  requirements:
25
24
  - - ">="
@@ -28,12 +27,12 @@ dependencies:
28
27
  segments:
29
28
  - 0
30
29
  version: "0"
31
- version_requirements: *id001
32
- name: rake
33
- type: :development
34
30
  prerelease: false
31
+ requirement: *id001
32
+ type: :development
33
+ name: rake
35
34
  - !ruby/object:Gem::Dependency
36
- requirement: &id002 !ruby/object:Gem::Requirement
35
+ version_requirements: &id002 !ruby/object:Gem::Requirement
37
36
  none: false
38
37
  requirements:
39
38
  - - ">="
@@ -42,12 +41,12 @@ dependencies:
42
41
  segments:
43
42
  - 0
44
43
  version: "0"
45
- version_requirements: *id002
46
- name: mail
47
- type: :runtime
48
44
  prerelease: false
45
+ requirement: *id002
46
+ type: :runtime
47
+ name: mail
49
48
  - !ruby/object:Gem::Dependency
50
- requirement: &id003 !ruby/object:Gem::Requirement
49
+ version_requirements: &id003 !ruby/object:Gem::Requirement
51
50
  none: false
52
51
  requirements:
53
52
  - - ">="
@@ -56,12 +55,12 @@ dependencies:
56
55
  segments:
57
56
  - 0
58
57
  version: "0"
59
- version_requirements: *id003
60
- name: yajl-ruby
61
- type: :runtime
62
58
  prerelease: false
59
+ requirement: *id003
60
+ type: :runtime
61
+ name: yajl-ruby
63
62
  - !ruby/object:Gem::Dependency
64
- requirement: &id004 !ruby/object:Gem::Requirement
63
+ version_requirements: &id004 !ruby/object:Gem::Requirement
65
64
  none: false
66
65
  requirements:
67
66
  - - ">="
@@ -70,10 +69,10 @@ dependencies:
70
69
  segments:
71
70
  - 0
72
71
  version: "0"
73
- version_requirements: *id004
74
- name: multipart-post
75
- type: :runtime
76
72
  prerelease: false
73
+ requirement: *id004
74
+ type: :runtime
75
+ name: rest-client
77
76
  description: Wrapper around the PipeDevMail.com API
78
77
  email:
79
78
  - support@pipedevmail.com
@@ -89,6 +88,7 @@ files:
89
88
  - Rakefile
90
89
  - lib/pdm.rb
91
90
  - lib/pdm/api_request.rb
91
+ - lib/pdm/attachment.rb
92
92
  - lib/pdm/error.rb
93
93
  - lib/pdm/ext/mail/message.rb
94
94
  - lib/pdm/history.rb
@@ -98,7 +98,6 @@ files:
98
98
  - lib/pdm/template.rb
99
99
  - lib/pdm/version.rb
100
100
  - pdm.gemspec
101
- has_rdoc: true
102
101
  homepage: ""
103
102
  licenses: []
104
103
 
@@ -128,7 +127,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
128
127
  requirements: []
129
128
 
130
129
  rubyforge_project:
131
- rubygems_version: 1.6.2
130
+ rubygems_version: 1.8.11
132
131
  signing_key:
133
132
  specification_version: 3
134
133
  summary: Interact with the PDM API