nvlope 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 8cd510beb0984f8e9ba22af3a1e68663d875669b
4
- data.tar.gz: 6ef841b1008c40533ed7dd2a1e9541c89648dca8
3
+ metadata.gz: 6f323a601352b22c4a9d1a1a663e32cce9e09c76
4
+ data.tar.gz: d0692731b041722896a76e37e2b19e670226b649
5
5
  SHA512:
6
- metadata.gz: 53da464862a6a87a2a01a1384cd11cdd650842b1876c10e88e2b29838400d5d9c16ce120c20c907e8c2bd8908ca9e9b14e7fa16a6728d0a85e2b55bf4a0d238d
7
- data.tar.gz: 4fb3d419f8d0d2578b7fa78a06e3f57ec9ac03eb6ad0747849529fb7d1f2716dc7611402126b38f5f34c52a2e02e849da4d82538b54816f7d7832f013ea62c3c
6
+ metadata.gz: ea35523c79dc384ca7a42965bd74a552077af3c02b79e5e17aae72b35ef76de1ecf58be1efc8f160d8fe1ffba5ebad8371b5fcd1bfcca8ffa22bf8bd8cf4d02f
7
+ data.tar.gz: 5d3f649ef2332556e91e6e6bff1291a57182909d6bf9147c070f9e48499bcf9cc9376d3e38ef4ec16a0c54d3936c2089d8909710691283a1abc14a1eb5a226e6
data/lib/nvlope.rb CHANGED
@@ -1,11 +1,13 @@
1
1
  require 'uri'
2
2
  require 'httparty'
3
3
  require 'nvlope/version'
4
+ require 'logger'
4
5
 
5
6
  class Nvlope
6
7
 
7
8
  autoload :Arguments, 'nvlope/arguments'
8
9
  autoload :Model, 'nvlope/model'
10
+ autoload :Request, 'nvlope/request'
9
11
  autoload :Session, 'nvlope/session'
10
12
  autoload :AccessToken, 'nvlope/access_token'
11
13
  autoload :Account, 'nvlope/account'
@@ -15,8 +17,15 @@ class Nvlope
15
17
  autoload :Message, 'nvlope/message'
16
18
  autoload :EmailAddress, 'nvlope/email_address'
17
19
 
18
- def self.Arguments hash
19
- Nvlope::Arguments.new hash
20
+ class << self
21
+ def Arguments hash
22
+ Nvlope::Arguments.new hash
23
+ end
24
+
25
+ def logger
26
+ @logger ||= Logger.new('/dev/null')
27
+ end
28
+ attr_writer :logger
20
29
  end
21
30
 
22
31
  def initialize arguments
@@ -29,12 +38,18 @@ class Nvlope
29
38
  @grant_type = arguments.optional(:grant_type ){ 'password' }.to_s
30
39
  @domain = arguments.optional(:domain ){ 'https://api.nvlope.com/' }.to_s
31
40
  @api_version = arguments.optional(:api_version){ 'v1' }.to_s
41
+ @logger = arguments.optional(:logger)
32
42
  end
33
43
 
34
44
  attr_accessor :grant_type, :username, :password, :client_id, :client_secret, :domain, :api_version
35
45
 
36
- def url path
37
- File.join(domain, api_version, path)
46
+ def logger
47
+ @logger || self.class.logger
48
+ end
49
+ attr_writer :logger
50
+
51
+ def access_token?
52
+ !@access_token.nil?
38
53
  end
39
54
 
40
55
  def access_token
@@ -49,25 +64,27 @@ class Nvlope
49
64
  @messages ||= Messages.new(self)
50
65
  end
51
66
 
52
- def request method, path, query={}, headers={}
53
- headers['Authorization'] ||= "Bearer #{access_token}"
54
- url = url(path)
55
- # puts "Nvlope: #{method.upcase} #{url}"
56
- HTTParty.send(method, url, query: query, headers: headers)
67
+ def unauthenticated_request method, path, options={}
68
+ Request.new(self, method, path, options).perform!
57
69
  end
58
70
 
71
+ def authenticated_request method, path, options={}
72
+ options[:headers] ||= {}
73
+ options[:headers]['Authorization'] ||= "Bearer #{access_token}"
74
+ Request.new(self, method, path, options).perform!
75
+ end
59
76
 
60
77
  def get_access_token
61
78
  @access_token = begin
62
- url = url('oauth2/token')
63
- # puts "Nvlope: POST #{url}"
64
- response = HTTParty.post(url, query:{
65
- grant_type: grant_type,
66
- username: username,
67
- password: password,
68
- client_id: client_id,
69
- client_secret: client_secret,
70
- })
79
+ response = unauthenticated_request(:post, 'oauth2/token',
80
+ query: {
81
+ grant_type: grant_type,
82
+ username: username,
83
+ password: password,
84
+ client_id: client_id,
85
+ client_secret: client_secret,
86
+ }
87
+ )
71
88
  response['access_token']
72
89
  end
73
90
  end
@@ -19,12 +19,15 @@ class Nvlope::Message < Nvlope::Model
19
19
  subject
20
20
  }
21
21
 
22
+ alias_method :html_part, :html
23
+ alias_method :text_part, :text
24
+
22
25
  def created_at
23
26
  @created_at ||= Time.at(created)
24
27
  end
25
28
 
26
29
  def header
27
- @header ||= Nvlope::Message::Header.new(nvlope, raw['header'])
30
+ @header ||= Nvlope::Message::Header.new(nvlope, raw['header'] || {})
28
31
  end
29
32
 
30
33
  def sender
@@ -48,6 +51,10 @@ class Nvlope::Message < Nvlope::Model
48
51
  define_method(key){ header.send(key) }
49
52
  end
50
53
 
54
+ def delete
55
+ nvlope.messages.delete [id]
56
+ end
57
+
51
58
  def mail_message
52
59
  mail_message = Mail::Message.new
53
60
  mail_message.date = header.date
@@ -70,4 +77,8 @@ class Nvlope::Message < Nvlope::Model
70
77
  mail_message
71
78
  end
72
79
 
80
+ def == other
81
+ (self.class === other || other.class === self) && self.id == other.id
82
+ end
83
+
73
84
  end
@@ -5,21 +5,22 @@ class Nvlope::Messages
5
5
  end
6
6
  attr_reader :nvlope
7
7
 
8
- def query params={}, headers={}
9
- params[:include] ||= 'all'
10
- raw = nvlope.request(:get, '/messages', params, headers)
8
+ def query query={}
9
+ query[:include] ||= 'all'
10
+ raw = nvlope.authenticated_request(:get, '/messages', query: query)
11
11
  Nvlope::MessageCollection.new(nvlope, raw)
12
12
  end
13
13
 
14
- def bulk_get message_ids, params={}, headers={}
15
- params["message_ids"] = message_ids
16
- raw = nvlope.request(:post, '/messages', params, headers)
14
+ def bulk_get message_ids, query={}
15
+ query["message_ids"] = message_ids
16
+ raw = nvlope.authenticated_request(:post, '/messages', query: query)
17
17
  Nvlope::MessageCollection.new(nvlope, raw)
18
18
  end
19
19
 
20
- def delete message_ids, params={}, headers={}
21
- params["message_ids"] = message_ids
22
- nvlope.request(:delete, '/messages', params, headers)
20
+ def delete message_ids, query={}
21
+ query["message_ids"] = message_ids
22
+ headers = {'Content-Type' => 'application/json'}
23
+ nvlope.authenticated_request(:delete, '/messages', body: query.to_json, headers: headers)
23
24
  self
24
25
  end
25
26
 
data/lib/nvlope/model.rb CHANGED
@@ -6,12 +6,12 @@ class Nvlope::Model
6
6
 
7
7
  def self.keys *keys
8
8
  @keys ||= Set[]
9
- return @keys if keys.empty?
10
9
  keys = keys.flatten.map(&:to_s).to_set
11
10
  keys.each do |key|
12
11
  define_method(key_to_method_name(key)){ raw[key] }
13
12
  end
14
13
  @keys += keys
14
+ @keys + (superclass.respond_to?(:keys) ? superclass.keys : Set[])
15
15
  end
16
16
 
17
17
  def initialize nvlope, raw={}
@@ -1,8 +1,39 @@
1
1
  class Nvlope::Request
2
2
 
3
- def initialize nvlope, raw
4
- @nvlope, @raw = nvlope, raw
3
+ Failed = Class.new(StandardError) do
4
+ def initialize request, response
5
+ @request, @response = request, response
6
+ super("code: #{response.code}\n#{response.body}")
7
+ end
8
+ attr_reader :request, :response
9
+ end
10
+
11
+ include HTTParty
12
+
13
+ def initialize nvlope, method, path, options={}
14
+ @nvlope, @method, @path, @options = nvlope, method, path, options
15
+ @url = File.join(nvlope.domain, nvlope.api_version, path)
16
+ end
17
+ attr_reader :nvlope, :method, :path, :url, :options
18
+
19
+ def response
20
+ @response ||= begin
21
+ nvlope.logger.info "Nvlope request: #{method.to_s.upcase} #{url} #{options.inspect}"
22
+ HTTParty.send(method, url, options)
23
+ end
24
+ end
25
+
26
+ def perform
27
+ response
28
+ end
29
+
30
+ def perform!
31
+ case response.code
32
+ when 200..299
33
+ return response
34
+ else
35
+ raise Failed.new(response.request, response)
36
+ end
5
37
  end
6
- attr_reader :nvlope, :raw
7
38
 
8
39
  end
@@ -1,3 +1,3 @@
1
1
  class Nvlope
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
data/spec/nvlope_spec.rb CHANGED
@@ -97,7 +97,7 @@ describe Nvlope do
97
97
  query: query,
98
98
  headers: headers.merge('Authorization' => 'Bearer FAKE_ACCESS_TOKEN'),
99
99
  )
100
- nvlope.request(:get, 'some/path', query, headers)
100
+ nvlope.request(:get, 'some/path', query: query, headers: headers)
101
101
  end
102
102
  end
103
103
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nvlope
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jared Grippe
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-03-19 00:00:00.000000000 Z
11
+ date: 2014-03-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: mail