Class: Kooaba::UpdateRequest
- Inherits:
-
Object
- Object
- Kooaba::UpdateRequest
- Defined in:
- lib/kooaba/update_request.rb
Constant Summary collapse
- CONTENT_TYPE =
'application/json'
- ALLOWED_PARAMS =
["title", "enabled", "reference_id", "metadata"]
Instance Attribute Summary collapse
-
#item_uuid ⇒ Object
Returns the value of attribute item_uuid.
-
#params ⇒ Object
Returns the value of attribute params.
Instance Method Summary collapse
-
#cleanedParams(params) ⇒ Object
Clean params and only use those allowed.
-
#initialize(item_uuid, params) ⇒ UpdateRequest
constructor
A new instance of UpdateRequest.
- #make_request(url) ⇒ Object
- #notAllowedParams(params) ⇒ Object
- #parse_2xx(http_resp) ⇒ Object
- #parse_4xx(http_resp) ⇒ Object
- #parse_5xx(http_resp) ⇒ Object
- #parse_request(http_resp) ⇒ Object
-
#start ⇒ Object
Returns the http response from the Kooaba servers.
- #unknown_response(http_resp) ⇒ Object
Constructor Details
#initialize(item_uuid, params) ⇒ UpdateRequest
Returns a new instance of UpdateRequest.
19 20 21 22 23 |
# File 'lib/kooaba/update_request.rb', line 19 def initialize(item_uuid, params) @item_uuid = item_uuid puts "Warning! The following params are not allowed and will be ignored: #{notAllowedParams(params)}" if notAllowedParams(params) != "" @params = cleanedParams(params) end |
Instance Attribute Details
#item_uuid ⇒ Object
Returns the value of attribute item_uuid.
12 13 14 |
# File 'lib/kooaba/update_request.rb', line 12 def item_uuid @item_uuid end |
#params ⇒ Object
Returns the value of attribute params.
13 14 15 |
# File 'lib/kooaba/update_request.rb', line 13 def params @params end |
Instance Method Details
#cleanedParams(params) ⇒ Object
Clean params and only use those allowed.
28 29 30 |
# File 'lib/kooaba/update_request.rb', line 28 def cleanedParams(params) params.select {|k, v| ALLOWED_PARAMS.include? k} end |
#make_request(url) ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/kooaba/update_request.rb', line 48 def make_request(url) http = Net::HTTP.new(url.host, url.port) http.use_ssl = true http.verify_mode = OpenSSL::SSL::VERIFY_NONE http.read_timeout = 500 req = Net::HTTP::Put.new(url.path) req.body = params.to_json req['date'] = Time.new.httpdate req['content-type'] = CONTENT_TYPE req['authorization'] = "Token #{Kooaba.data_key}" http.request(req) end |
#notAllowedParams(params) ⇒ Object
32 33 34 |
# File 'lib/kooaba/update_request.rb', line 32 def notAllowedParams(params) params.select {|k, v| !ALLOWED_PARAMS.include? k}.keys.join(", ") end |
#parse_2xx(http_resp) ⇒ Object
77 78 79 |
# File 'lib/kooaba/update_request.rb', line 77 def parse_2xx(http_resp) # "Item has been updated.\n" end |
#parse_4xx(http_resp) ⇒ Object
81 82 83 84 85 86 87 88 89 90 |
# File 'lib/kooaba/update_request.rb', line 81 def parse_4xx(http_resp) case http_resp.code when 404 raise ArgumentError.new("Kooaba Response: #{http_resp.code} #{http_resp.body}. Perhaps your key is not allowed to access this item?") when 422 raise ArgumentError.new("Kooaba Response: #{http_resp.code} #{http_resp.body}. Please check your params.") else raise ArgumentError.new("Kooaba Response: #{http_resp.code} #{http_resp.body}.") end end |
#parse_5xx(http_resp) ⇒ Object
92 93 94 |
# File 'lib/kooaba/update_request.rb', line 92 def parse_5xx(http_resp) raise StandardError.new("Internal Server Error: #{http_resp.code} #{http_resp.body}") end |
#parse_request(http_resp) ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/kooaba/update_request.rb', line 64 def parse_request(http_resp) case http_resp when Net::HTTPSuccess parse_2xx(http_resp) when Net::HTTPClientError parse_4xx(http_resp) when Net::HTTPServerError parse_5xx(http_resp) else unknown_response(http_resp) end end |
#start ⇒ Object
Returns the http response from the Kooaba servers.
39 40 41 42 43 44 45 46 |
# File 'lib/kooaba/update_request.rb', line 39 def start url = URI.parse(Kooaba::UPLOAD_URL + "items/" + item_uuid) resp = make_request(url) parse_request(resp) return resp end |
#unknown_response(http_resp) ⇒ Object
96 97 98 |
# File 'lib/kooaba/update_request.rb', line 96 def unknown_response(http_resp) raise StandardError.new("Unknown response: #{http_resp.code} #{http_resp.body} ") end |