Module: ChemSpider

Defined in:
lib/chem_spider.rb,
lib/chem_spider/version.rb,
lib/chem_spider/services/search.rb,
lib/chem_spider/services/in_ch_i.rb,
lib/chem_spider/services/spectra.rb,
lib/chem_spider/services/open_babel.rb,
lib/chem_spider/services/mass_spec_a_p_i.rb

Overview

ChemSpider.rb: ChemSpider wrapped up with a Ruby bow.

Defined Under Namespace

Modules: VERSION Classes: CompoundInfo, ExtRef, ExtendedCompoundInfo, SpectrumInfo

Class Method Summary collapse

Class Method Details

.get!(service_name, operation_name, params = {}, uri_options = {}, options = {}) ⇒ Object

Returns the result of calling the specified ChemSpider Web service using the HTTP/1.1 “GET” method.

Examples:

Convert an InChIKey into a ChemSpider ID.

##
# http://www.chemspider.com/InChI.asmx/InChIKeyToCSID?inchi_key=BSYNRYMUTXBXSQ-UHFFFAOYSA-N
# => <string xmlns="http://www.chemspider.com/">2157</string>
#
ChemSpider.get!(:InChI, :InChIKeyToCSID, { :inchi_key => 'BSYNRYMUTXBXSQ-UHFFFAOYSA-N' }, {}, {
  :selector => 'string', 
  :datatype => Integer, 
  :first_child => true,
})
#=> 2157

Convert an InChIKey into a ChemSpider ID using a proxy server.

##
# https://www.example.com:443/chemspider/InChI/InChIKeyToCSID?inchi_key=BSYNRYMUTXBXSQ-UHFFFAOYSA-N
# => <string xmlns="http://www.chemspider.com/">2157</string>
#
ChemSpider.get!(:InChI, :InChIKeyToCSID, { :inchi_key => 'BSYNRYMUTXBXSQ-UHFFFAOYSA-N' }, {
  :scheme => :https,
  :host => 'www.example.com',
  :port => 443,
  :path_format => '/chemspider/%s/%s',
}, {
  :selector => 'string', 
  :datatype => Integer, 
  :first_child => true,
})
#=> 2157

Parameters:

  • service_name (#to_sym)
  • operation_name (#to_sym)
  • params (Hash{Symbol => Object}) (defaults to: {})

    (Hash.new)

  • options (Hash{Symbol => Object}) (defaults to: {})

    (Hash.new)

  • uri_options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (uri_options):

  • :scheme (#to_s) — default: 'http'
  • :host (#to_s) — default: 'www.chemspider.com'
  • :port (#to_s) — default: 80
  • :path (#to_s) — default: nil
  • :path_format (#to_s) — default: '/%s.asmx/%s'
  • :query (#to_s) — default: nil
  • :fragment (#to_s) — default: nil

Options Hash (options):

  • :selector (#to_s) — default: nil
  • :datatype (Hash{Symbol => Object}, Class, #__attributes__) — default: Hash.new
  • :first_child (Boolean) — default: false

Returns:

  • (Object)

Since:

  • 0.0.1



61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/chem_spider.rb', line 61

def get!(service_name, operation_name, params = {}, uri_options = {}, options = {})
  # construct the URI...
  uri = uri_for(service_name, operation_name, params, uri_options)
  
  # dereference the URI...
  response = Net::HTTP.get_response(uri)
  
  # parse the response...
  doc = Nokogiri::XML(response.body)
  
  # process the result...
  css(doc, options)
end

.post!(service_name, operation_name, params = {}, uri_options = {}, options = {}) ⇒ Object

Returns the result of calling the specified ChemSpider Web service using the HTTP/1.1 “POST” method.

Examples:

Convert an InChIKey into a ChemSpider ID.

##
# http://www.chemspider.com/InChI.asmx/InChIKeyToCSID?inchi_key=BSYNRYMUTXBXSQ-UHFFFAOYSA-N
# => <string xmlns="http://www.chemspider.com/">2157</string>
#
ChemSpider.post!(:InChI, :InChIKeyToCSID, { :inchi_key => 'BSYNRYMUTXBXSQ-UHFFFAOYSA-N' }, {}, {
  :selector => 'string', 
  :datatype => Integer, 
  :first_child => true,
})
#=> 2157

Convert an InChIKey into a ChemSpider ID using a proxy server.

##
# https://www.example.com:443/chemspider/InChI/InChIKeyToCSID?inchi_key=BSYNRYMUTXBXSQ-UHFFFAOYSA-N
# => <string xmlns="http://www.chemspider.com/">2157</string>
#
ChemSpider.post!(:InChI, :InChIKeyToCSID, { :inchi_key => 'BSYNRYMUTXBXSQ-UHFFFAOYSA-N' }, {
  :scheme => :https,
  :host => 'www.example.com',
  :port => 443,
  :path_format => '/chemspider/%s/%s',
}, {
  :selector => 'string', 
  :datatype => Integer, 
  :first_child => true,
})
#=> 2157

Parameters:

  • service_name (#to_sym)
  • operation_name (#to_sym)
  • params (Hash{Symbol => Object}) (defaults to: {})

    (Hash.new)

  • options (Hash{Symbol => Object}) (defaults to: {})

    (Hash.new)

  • uri_options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (uri_options):

  • :scheme (#to_s) — default: 'http'
  • :host (#to_s) — default: 'www.chemspider.com'
  • :port (#to_s) — default: 80
  • :path (#to_s) — default: nil
  • :path_format (#to_s) — default: '/%s.asmx/%s'
  • :query (#to_s) — default: nil
  • :fragment (#to_s) — default: nil

Options Hash (options):

  • :selector (#to_s) — default: nil
  • :datatype (Hash{Symbol => Object}, Class, #__attributes__) — default: Hash.new
  • :first_child (Boolean) — default: false

Returns:

  • (Object)

Since:

  • 0.0.1



123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/chem_spider.rb', line 123

def post!(service_name, operation_name, params = {}, uri_options = {}, options = {})
  # construct the URI...
  uri = uri_for(service_name, operation_name, nil, uri_options)
  
  # dereference the URI...
  response = Net::HTTP.post_form(uri, params)
  
  # parse the response...
  doc = Nokogiri::XML(response.body)
  
  # process the result...
  css(doc, options)
end