Class: CommonChemistry::Client

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/commonchemistry.rb

Overview

Client class to interact with the Common Chemistry API

Instance Method Summary collapse

Constructor Details

#initializeClient

Returns a new instance of Client.



22
23
24
# File 'lib/commonchemistry.rb', line 22

def initialize
  # TODO
end

Instance Method Details

#detail(cas_rn: nil, uri: nil) ⇒ DetailResult

Retrieves detailed information about a substance

Parameters:

  • cas_rn (String, nil) (defaults to: nil)

    The CAS registry number

  • uri (String, nil) (defaults to: nil)

    The URI of the substance

Returns:

Raises:



54
55
56
57
58
59
60
61
62
63
# File 'lib/commonchemistry.rb', line 54

def detail(cas_rn: nil, uri: nil)
  params = {}
  params[:cas_rn] = cas_rn if cas_rn
  params[:uri] = uri if uri

  response = self.class.get('/detail', query: params)
  handle_response(response) do
    DetailResult.new(response.parsed_response)
  end
end

#export(uri:, return_as_attachment: false) ⇒ String

Exports substance data

Parameters:

  • uri (String)

    The URI of the substance

  • return_as_attachment (Boolean) (defaults to: false)

    Whether to return as attachment

Returns:

  • (String)

    The exported data

Raises:



73
74
75
76
77
78
79
# File 'lib/commonchemistry.rb', line 73

def export(uri:, return_as_attachment: false)
  params = { uri: uri, returnAsAttachment: return_as_attachment }
  response = self.class.get('/export', query: params)
  handle_response(response) do
    response.body  # Since the response is text/plain
  end
end

#search(q:, offset: nil, size: nil) ⇒ SearchResponse

Searches for substances matching the query

Parameters:

  • q (String)

    The search query

  • offset (Integer, nil) (defaults to: nil)

    The offset for pagination

  • size (Integer, nil) (defaults to: nil)

    The number of results to return

Returns:

Raises:



35
36
37
38
39
40
41
42
43
44
# File 'lib/commonchemistry.rb', line 35

def search(q:, offset: nil, size: nil)
  params = { q: q }
  params[:offset] = offset if offset
  params[:size] = size if size

  response = self.class.get('/search', query: params)
  handle_response(response) do
    SearchResponse.new(response.parsed_response)
  end
end