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

Instance Method Details

#create(url, body, handler) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/sequencescape-api/connection_factory/actions.rb', line 50

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



64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/sequencescape-api/connection_factory/actions.rb', line 64

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, &block) ⇒ Object



119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/sequencescape-api/connection_factory/actions.rb', line 119

def perform_for_file(http_verb, url, file, filename, content_type, &block)
  uri = URI.parse(url)
  Net::HTTP.start(uri.host, uri.port) 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



78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/sequencescape-api/connection_factory/actions.rb', line 78

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