Class: AssayDepot::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/assaydepot/client.rb

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Client

Returns a new instance of Client.



9
10
11
# File 'lib/assaydepot/client.rb', line 9

def initialize(options={})
  @endpoint = options[:endpoint]
end

Instance Method Details

#delete(params: {}) ⇒ Object



68
69
70
71
72
73
74
75
76
77
# File 'lib/assaydepot/client.rb', line 68

def delete(params: {})
  uri = get_uri( params )
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = uri.scheme === 'https'
  request = Net::HTTP::Delete.new(uri.request_uri)
  puts "CLIENT.DELETE HOST [#{uri.host}] URI [#{uri.request_uri}]}] PARAMS [#{params.inspect}]" if ENV["DEBUG"] == "true"
  request["Authorization"] = "Bearer #{AssayDepot.access_token}" unless params[:access_token]
  res = http.request(request)
  res.body
end

#get(params: {}) ⇒ Object



26
27
28
29
30
31
32
33
34
35
# File 'lib/assaydepot/client.rb', line 26

def get(params: {})
  uri = get_uri( params )
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = uri.scheme === 'https'
  request = Net::HTTP::Get.new(uri.request_uri)
  puts "CLIENT.GET HOST [#{uri.host}] URI [#{uri.request_uri}] PARAMS [#{params.inspect}]" if ENV["DEBUG"] == "true"
  request["Authorization"] = "Bearer #{AssayDepot.access_token}" unless params[:access_token]
  res = http.request(request)
  res.body
end

#post(body: {}, params: {}) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/assaydepot/client.rb', line 52

def post(body: {}, params: {})
  uri = get_uri( params )
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = uri.scheme === 'https'
  request = Net::HTTP::Post.new(uri.request_uri)
  puts "CLIENT.POST HOST [#{uri.host}] URI [#{uri.request_uri}] BODY [#{body.inspect}] PARAMS [#{params.inspect}]" if ENV["DEBUG"] == "true"
  request["Authorization"] = "Bearer #{AssayDepot.access_token}" unless params[:access_token]
  request["Accept"] = "application/json"
  request["Content-Type"] = "application/json"
  if (body && body.keys.length > 0)
    request.body = body.to_json
  end
  res = http.request(request)
  res.body
end

#put(body: {}, params: {}) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/assaydepot/client.rb', line 37

def put(body: {}, params: {})
  uri = get_uri( params )
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = uri.scheme === 'https'
  request = Net::HTTP::Put.new(uri.request_uri)
  puts "CLIENT.PUT HOST [#{uri.host}] URI [#{uri.request_uri}] BODY [#{body.inspect}] PARAMS [#{params.inspect}]" if ENV["DEBUG"] == "true"
  request["Authorization"] = "Bearer #{AssayDepot.access_token}" unless params[:access_token]
  request["Content-Type"] = "application/json"
  if (body.keys.length > 0)
    request.body = body.to_json
  end
  res = http.request(request)
  res.body
end

#request(url = nil, params = {}, headers = {}, auth = {}) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/assaydepot/client.rb', line 13

def request(url = nil, params={}, headers={}, auth={})
  uri = url.nil? ? get_uri(params) : URI( "#{url}" )
  puts "CLIENT.REQUEST HOST [#{uri.host}] URI [#{uri.request_uri}] PARAMS [#{params.inspect}]" if ENV["DEBUG"] == "true"
  uri.query = Rack::Utils.build_nested_query(params) unless params.keys.length == 0
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = uri.scheme === 'https'
  request = Net::HTTP::Get.new(uri.request_uri)
  request["Authorization"] = "Bearer #{AssayDepot.access_token}" unless params[:access_token] || auth[:username]
  request.basic_auth auth[:username], auth[:password] unless auth[:username].nil?
  res = http.request(request)
  JSON.parse(res.body)
end

#search(query: nil, facets: {}, params: {}) ⇒ Object



79
80
81
82
83
84
85
# File 'lib/assaydepot/client.rb', line 79

def search(query: nil, facets: {}, params: {})
  params["q"] = query if query != ""
  facets&.map do |name,value|
    params["facets[#{name}][]"] = value
  end
  get(params: params)
end