Module: Sequencescape::Api::ConnectionFactory::Actions
- Included in:
- Sequencescape::Api::ConnectionFactory
- Defined in:
- lib/sequencescape-api/connection_factory/actions.rb
Constant Summary collapse
- ServerError =
Class.new(Sequencescape::Api::ConnectionFactory::ConnectionError)
Instance Method Summary collapse
-
#create(url, body, handler) ⇒ Object
rubocop:todo Metrics/MethodLength.
-
#create_from_file(url, file, filename, content_type, handler) ⇒ Object
rubocop:todo Metrics/MethodLength.
- #perform_for_file(http_verb, url, file, filename, content_type) ⇒ Object
- #read(url, handler) ⇒ Object
- #retrieve(url, handler, content_type) ⇒ Object
- #root(handler) ⇒ Object
-
#update(url, body, handler) ⇒ Object
rubocop:enable Metrics/MethodLength.
Instance Method Details
#create(url, body, handler) ⇒ Object
rubocop:todo Metrics/MethodLength
51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/sequencescape-api/connection_factory/actions.rb', line 51 def create(url, body, handler) perform(:post, url, jsonify(body, action: :create)) do |response| case response when Net::HTTPCreated then handler.success(parse_json_from(response)) when Net::HTTPSuccess then handler.success(parse_json_from(response)) # TODO: should be error! when Net::HTTPUnauthorized then handler.unauthenticated(parse_json_from(response)) when Net::HTTPUnprocessableEntity then handler.failure(parse_json_from(response)) when Net::HTTPNotFound then handler.missing(parse_json_from(response)) when Net::HTTPRedirection then handle_redirect(response, handler) else raise ServerError, response.body end end end |
#create_from_file(url, file, filename, content_type, handler) ⇒ Object
rubocop:todo Metrics/MethodLength
67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/sequencescape-api/connection_factory/actions.rb', line 67 def create_from_file(url, file, filename, content_type, handler) perform_for_file(:post, url, file, filename, content_type) do |response| case response when Net::HTTPCreated then handler.success(parse_json_from(response)) when Net::HTTPSuccess then handler.success(parse_json_from(response)) # TODO: should be error! when Net::HTTPUnauthorized then handler.unauthenticated(parse_json_from(response)) when Net::HTTPUnprocessableEntity then handler.failure(parse_json_from(response)) when Net::HTTPNotFound then handler.missing(parse_json_from(response)) when Net::HTTPRedirection then handle_redirect(response, handler) else raise ServerError, response.body end end end |
#perform_for_file(http_verb, url, file, filename, content_type) ⇒ Object
123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/sequencescape-api/connection_factory/actions.rb', line 123 def perform_for_file(http_verb, url, file, filename, content_type) uri = URI.parse(url) Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == 'https') do |connection| connection.read_timeout = read_timeout file_headers = headers.merge!({ 'Content-Disposition' => "form-data; filename=\"#{filename}\"" }) request = Net::HTTP.const_get(http_verb.to_s.classify).new(uri.request_uri, file_headers) request.content_type = content_type # request.body = body.to_json request.body = file.read yield(connection.request(request)) end end |
#read(url, handler) ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/sequencescape-api/connection_factory/actions.rb', line 38 def read(url, handler) perform(:get, url) do |response| case response when Net::HTTPSuccess then handler.success(parse_json_from(response)) when Net::HTTPUnauthorized then handler.unauthenticated(parse_json_from(response)) when Net::HTTPNotFound then handler.missing(parse_json_from(response)) when Net::HTTPRedirection then handle_redirect(response, handler) else raise ServerError, response.body end end end |
#retrieve(url, handler, content_type) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/sequencescape-api/connection_factory/actions.rb', line 26 def retrieve(url, handler, content_type) perform(:get, url, nil, content_type) do |response| case response when Net::HTTPSuccess then response when Net::HTTPUnauthorized then handler.unauthenticated(parse_json_from(response)) when Net::HTTPNotFound then handler.missing(parse_json_from(response)) when Net::HTTPRedirection then handle_redirect(response, handler) else raise ServerError, response.body end end end |
#root(handler) ⇒ Object
22 23 24 |
# File 'lib/sequencescape-api/connection_factory/actions.rb', line 22 def root(handler) read(url, handler) end |
#update(url, body, handler) ⇒ Object
rubocop:enable Metrics/MethodLength
82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/sequencescape-api/connection_factory/actions.rb', line 82 def update(url, body, handler) perform(:put, url, jsonify(body, action: :update)) do |response| case response when Net::HTTPSuccess then handler.success(parse_json_from(response)) when Net::HTTPUnauthorized then handler.unauthenticated(parse_json_from(response)) when Net::HTTPUnprocessableEntity then handler.failure(parse_json_from(response)) when Net::HTTPNotFound then handler.missing(parse_json_from(response)) when Net::HTTPRedirection then handle_redirect(response, handler) else raise ServerError, response.body end end end |