rentlinx 0.3.0 → 0.3.1

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: 6e3eba7e898a937ec5f358abcdb172ae207a1586
4
- data.tar.gz: 57cc34bb4131f160deac0b107fe24f8d870e3987
3
+ metadata.gz: 3819098a7b8edb089697d87f6302b298b3529f56
4
+ data.tar.gz: 5c392001f17b5c750ad0cd3c81cf336363bd070b
5
5
  SHA512:
6
- metadata.gz: 18bc2f16c329de3b9ad5c9462f623ebdee7a58b3e337a80eb6046238f98c45b6ff904c4c259a5cadcf434a5bcc027af40a0339d4dca8ba822cb75f0a08a3732e
7
- data.tar.gz: 9964b42866d070ac37ee9293ed34c057f90e904c0a0eb15c0886693d6cbc898ed66eafad92062972971a57bbc87859a8d0a8a7d167ebe506ddf1581e785671d4
6
+ metadata.gz: 529234a86df627dd0beea23cd00e49d921b1594bc160fcf362f3e98de4a84615c5592df1a948c8eeb02b01b1ac2b4ee4ec3d9ee761cf5333ff8f313ddaaa2126
7
+ data.tar.gz: a75cd39c0fa79d5fd8e52c98deb04c17b3c1442a62cc309b77da6f2d43925fa97cd7d230e82f7a20f05abd26ec640126e28da1c7d3b78517d132391210eeee78
data/lib/rentlinx.rb CHANGED
@@ -1,3 +1,4 @@
1
+ require 'logging'
1
2
  require 'rentlinx/client'
2
3
  require 'rentlinx/errors'
3
4
  require 'rentlinx/models/base'
@@ -38,8 +39,22 @@ module Rentlinx
38
39
  end
39
40
  end
40
41
 
42
+ def log_level(*args)
43
+ if args.empty?
44
+ @log_level
45
+ else
46
+ @log_level = args.first
47
+ end
48
+ end
49
+
41
50
  def client
42
51
  @client ||= Rentlinx::Client.new
43
52
  end
53
+
54
+ def logger
55
+ lgr = Logging.logger(STDOUT)
56
+ lgr.level = (@log_level || :error)
57
+ lgr
58
+ end
44
59
  end
45
60
  end
@@ -6,6 +6,10 @@ require 'rentlinx/default'
6
6
  module Rentlinx
7
7
  class Client
8
8
  def initialize
9
+ raise Rentlinx::NotConfigured if Rentlinx.username.nil? ||
10
+ Rentlinx.password.nil? ||
11
+ Rentlinx.api_url_prefix.nil?
12
+
9
13
  @url_prefix = (Rentlinx.api_url_prefix + '/').freeze # Extra slashes are fine
10
14
  @api_token ||= authenticate(Rentlinx.username, Rentlinx.password)
11
15
  end
@@ -13,11 +17,24 @@ module Rentlinx
13
17
  def post(object)
14
18
  case object
15
19
  when Rentlinx::Property
20
+ raise Rentlinx::InvalidObject, object unless object.valid?
16
21
  post_property(object)
17
22
  when Rentlinx::Unit
23
+ raise Rentlinx::InvalidObject, object unless object.valid?
18
24
  post_unit(object)
19
25
  else
20
- raise TypeError, "Invalid object: #{object.class}"
26
+ raise TypeError, "Type not permitted: #{object.class}"
27
+ end
28
+ end
29
+
30
+ def unpost(type, id)
31
+ case type
32
+ when :property
33
+ unpost_property(id)
34
+ when :unit
35
+ unpost_unit(id)
36
+ else
37
+ raise TypeError, "Invalid type: #{type}"
21
38
  end
22
39
  end
23
40
 
@@ -65,6 +82,14 @@ module Rentlinx
65
82
  request('PUT', "units/#{unit.unitID}", unit.to_hash)
66
83
  end
67
84
 
85
+ def unpost_property(id)
86
+ request('DELETE', "properties/#{id}")
87
+ end
88
+
89
+ def unpost_unit(id)
90
+ request('DELETE', "units/#{id}")
91
+ end
92
+
68
93
  def authenticate(username, password)
69
94
  data = { username: username, password: password }
70
95
  response = request('POST', 'authentication/login', data)
@@ -73,8 +98,29 @@ module Rentlinx
73
98
 
74
99
  def request(method, path, data = nil)
75
100
  options = { body: data.to_json, header: authenticated_headers }
101
+ Rentlinx.logger.debug "#{method} Request to #{path}\n#{options.inspect}"
76
102
  response = session.request(method, URI.join(@url_prefix, path), options)
77
- JSON.parse(response.body)
103
+ response_handler(response)
104
+ end
105
+
106
+ def response_handler(response)
107
+ case response.status
108
+ when 200, 201, 202
109
+ JSON.parse(response.body)
110
+ when 204
111
+ nil # don't attempt to JSON parse emptystring
112
+ when 400
113
+ body = JSON.parse(response.body)
114
+ raise Rentlinx::BadRequest, body['details']
115
+ when 403
116
+ raise Rentlinx::Forbidden, response
117
+ when 404
118
+ raise Rentlinx::NotFound, response
119
+ when 500, 501, 502, 503, 504, 505
120
+ raise Rentlinx::ServerError, response
121
+ else
122
+ raise Rentlinx::HTTPError, response
123
+ end
78
124
  end
79
125
 
80
126
  def authenticated_headers
@@ -7,4 +7,49 @@ module Rentlinx
7
7
 
8
8
  class InvalidTypeParam < RentlinxError
9
9
  end
10
+
11
+ class NotConfigured < RentlinxError
12
+ def initialize
13
+ super('Rentlinx is not configured.')
14
+ end
15
+ end
16
+
17
+ class InvalidObject < RentlinxError
18
+ def initialize(object)
19
+ super("#{object.class} is invalid: #{object.missing_attributes}")
20
+ end
21
+ end
22
+
23
+ class HTTPError < RentlinxError
24
+ attr_reader :response
25
+
26
+ def initialize(response, msg = nil)
27
+ @response = response
28
+ super(msg.nil? ? 'Received an unexpected response from the server' : msg)
29
+ end
30
+ end
31
+
32
+ class BadRequest < HTTPError
33
+ def initialize(response)
34
+ super(response, 'The request sent to the server was invalid.')
35
+ end
36
+ end
37
+
38
+ class NotFound < HTTPError
39
+ def initialize(response)
40
+ super(response, 'The item you requested could not be found on the remote server.')
41
+ end
42
+ end
43
+
44
+ class Forbidden < HTTPError
45
+ def initialize(response)
46
+ super(response, 'You are not permitted to access the item you requested.')
47
+ end
48
+ end
49
+
50
+ class ServerError < HTTPError
51
+ def initialize(response)
52
+ super(response, 'The remote server has experienced an error.')
53
+ end
54
+ end
10
55
  end
@@ -24,6 +24,14 @@ module Rentlinx
24
24
  Rentlinx.client.post(self)
25
25
  end
26
26
 
27
+ def unpost
28
+ Rentlinx.client.unpost(type, send(type.to_s + 'ID'))
29
+ end
30
+
31
+ def self.unpost(id)
32
+ Rentlinx.client.unpost(type, id)
33
+ end
34
+
27
35
  def self.get_from_id(type, id)
28
36
  Rentlinx.client.get(type.to_sym, id)
29
37
  end
@@ -51,5 +59,23 @@ module Rentlinx
51
59
  !send(at).nil? && send(at) != ''
52
60
  end
53
61
  end
62
+
63
+ def missing_attributes
64
+ missing = required_attributes.select do |at|
65
+ send(at).nil? || send(at) == ''
66
+ end
67
+
68
+ "Missing required attributes: #{missing.join(', ')}"
69
+ end
70
+
71
+ private
72
+
73
+ def type
74
+ self.class.type
75
+ end
76
+
77
+ def self.type
78
+ name.split('::').last.downcase.to_sym
79
+ end
54
80
  end
55
81
  end
@@ -1,3 +1,3 @@
1
1
  module Rentlinx
2
- VERSION = '0.3.0'
2
+ VERSION = '0.3.1'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rentlinx
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - AppFolio, Inc.
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-06-22 00:00:00.000000000 Z
11
+ date: 2015-06-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: httpclient
@@ -58,7 +58,7 @@ homepage: https://github.com/appfolio/rentlinx_client
58
58
  licenses:
59
59
  - Simplified BSD
60
60
  metadata: {}
61
- post_install_message: Thanks for installing!
61
+ post_install_message:
62
62
  rdoc_options: []
63
63
  require_paths:
64
64
  - lib
@@ -77,5 +77,5 @@ rubyforge_project:
77
77
  rubygems_version: 2.4.3
78
78
  signing_key:
79
79
  specification_version: 4
80
- summary: API Wrapper for
80
+ summary: API Wrapper for the Rentlinx API
81
81
  test_files: []