Module: Miniphonic::Helpers
Instance Method Summary collapse
-
#delete_from_server(url, payload = {}, &block) ⇒ Object
Deletes data from the server and executes a given block on success.
-
#from_server(url, payload = {}, &block) ⇒ Object
Gets data from the server and executes a given block on success.
- #handle_response(response, &block) ⇒ Object
- #path_to_payload(path, type) ⇒ Object
- #server_error(response) ⇒ Object
-
#to_server(url, payload = {}, &block) ⇒ Object
Posts data to the server and executes a given block on success.
Instance Method Details
#delete_from_server(url, payload = {}, &block) ⇒ Object
Deletes data from the server and executes a given block on success
20 21 22 23 24 25 26 27 28 29 |
# File 'lib/miniphonic/helpers.rb', line 20 def delete_from_server(url, payload = {}, &block) connection = Miniphonic.connect response = connection.delete url, payload if response.success? block.call(response) if block response else server_error(response) end end |
#from_server(url, payload = {}, &block) ⇒ Object
Gets data from the server and executes a given block on success
13 14 15 16 17 |
# File 'lib/miniphonic/helpers.rb', line 13 def from_server(url, payload = {}, &block) connection = Miniphonic.connect response = connection.get url, payload handle_response(response, &block) end |
#handle_response(response, &block) ⇒ Object
31 32 33 34 35 36 37 38 |
# File 'lib/miniphonic/helpers.rb', line 31 def handle_response(response, &block) if response.success? block.call(response) if block response else server_error(response) end end |
#path_to_payload(path, type) ⇒ Object
40 41 42 43 44 45 46 |
# File 'lib/miniphonic/helpers.rb', line 40 def path_to_payload(path, type) path = File.(path) filetype = MIME::Types.type_for(path) payload = {} payload[type] = Faraday::UploadIO.new(path, filetype) payload end |
#server_error(response) ⇒ Object
48 49 50 |
# File 'lib/miniphonic/helpers.rb', line 48 def server_error(response) raise Miniphonic::ServerSideError, "Error on server, responded #{ response.status } with message #{ response.body["error_message"]}." end |
#to_server(url, payload = {}, &block) ⇒ Object
Posts data to the server and executes a given block on success
6 7 8 9 10 |
# File 'lib/miniphonic/helpers.rb', line 6 def to_server(url, payload = {}, &block) connection = Miniphonic.connect response = connection.post url, payload handle_response(response, &block) end |