Class: Duse::Client::Session
- Inherits:
-
Object
- Object
- Duse::Client::Session
- Defined in:
- lib/duse/client/session.rb
Instance Attribute Summary collapse
-
#config ⇒ Object
Returns the value of attribute config.
Instance Method Summary collapse
- #connection ⇒ Object
- #create(entity, hash) ⇒ Object
- #delete(*args) ⇒ Object
- #delete_one(entity, id) ⇒ Object
- #error_msg(json) ⇒ Object
- #faraday_adapter ⇒ Object
- #find_many(entity, params = {}) ⇒ Object
- #find_one(entity, id) ⇒ Object
- #get(*args) ⇒ Object
- #patch(*args) ⇒ Object
- #post(*args) ⇒ Object
- #process_raw_http_response(response) ⇒ Object
- #raw(*args) ⇒ Object
- #raw_http_request(*args) ⇒ Object
- #update(entity, id, hash) ⇒ Object
Instance Attribute Details
#config ⇒ Object
Returns the value of attribute config.
15 16 17 |
# File 'lib/duse/client/session.rb', line 15 def config @config end |
Instance Method Details
#connection ⇒ Object
92 93 94 95 96 97 98 99 100 |
# File 'lib/duse/client/session.rb', line 92 def connection fail ArgumentError, 'Uri must be set' if config.uri.nil? @connection ||= Faraday.new url: config.uri do |faraday| faraday.request :json faraday.response :json, content_type: /\bjson$/ faraday.adapter *faraday_adapter end end |
#create(entity, hash) ⇒ Object
27 28 29 30 |
# File 'lib/duse/client/session.rb', line 27 def create(entity, hash) response_body = post("/#{entity.base_path}", hash) instance_from(entity, response_body) end |
#delete(*args) ⇒ Object
53 54 55 |
# File 'lib/duse/client/session.rb', line 53 def delete(*args) raw(:delete, *args) end |
#delete_one(entity, id) ⇒ Object
37 38 39 |
# File 'lib/duse/client/session.rb', line 37 def delete_one(entity, id) delete("/#{entity.base_path}/#{id}") end |
#error_msg(json) ⇒ Object
84 85 86 87 88 89 90 |
# File 'lib/duse/client/session.rb', line 84 def error_msg(json) result = JSON.parse(json)['message'] result = result.join "\n" if result.is_a? Array result rescue json end |
#faraday_adapter ⇒ Object
102 103 104 |
# File 'lib/duse/client/session.rb', line 102 def faraday_adapter Faraday.default_adapter end |
#find_many(entity, params = {}) ⇒ Object
22 23 24 25 |
# File 'lib/duse/client/session.rb', line 22 def find_many(entity, params = {}) response_body = get("/#{entity.base_path}") instances_from(entity, response_body) end |
#find_one(entity, id) ⇒ Object
17 18 19 20 |
# File 'lib/duse/client/session.rb', line 17 def find_one(entity, id) response_body = get("/#{entity.base_path}/#{id}") instance_from(entity, response_body) end |
#get(*args) ⇒ Object
41 42 43 |
# File 'lib/duse/client/session.rb', line 41 def get(*args) raw(:get, *args) end |
#patch(*args) ⇒ Object
49 50 51 |
# File 'lib/duse/client/session.rb', line 49 def patch(*args) raw(:patch, *args) end |
#post(*args) ⇒ Object
45 46 47 |
# File 'lib/duse/client/session.rb', line 45 def post(*args) raw(:post, *args) end |
#process_raw_http_response(response) ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/duse/client/session.rb', line 61 def process_raw_http_response(response) case response.status when 0 then raise SSLError, 'SSL error: could not verify peer' when 200..299 then JSON.parse(response.body) rescue response.body when 301, 303 then raw(:get, response.headers['Location']) when 302, 307, 308 then raw(verb, response.headers['Location']) when 401 then raise NotLoggedIn, error_msg(response.body) when 403 then raise NotAuthorized, error_msg(response.body) when 404 then raise NotFound, error_msg(response.body) when 422 then raise ValidationFailed, error_msg(response.body) when 400..499 then raise Error, error_msg(response.body) when 500..599 then raise Error, error_msg(response.body) else raise Error, "unhandled status code #{response.status}" end end |
#raw(*args) ⇒ Object
57 58 59 |
# File 'lib/duse/client/session.rb', line 57 def raw(*args) process_raw_http_response raw_http_request(*args) end |
#raw_http_request(*args) ⇒ Object
77 78 79 80 81 82 |
# File 'lib/duse/client/session.rb', line 77 def raw_http_request(*args) connection.public_send(*args) do |request| request.headers['Authorization'] = config.token unless config.token.nil? request.headers['Accept'] = 'application/vnd.duse.1+json' end end |
#update(entity, id, hash) ⇒ Object
32 33 34 35 |
# File 'lib/duse/client/session.rb', line 32 def update(entity, id, hash) response_body = patch("/#{entity.base_path}/#{id}", hash) instance_from(entity, response_body) end |