Class: Ruspider::RestClient

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

Overview

ChemSpider REST Webservice API Client

Constant Summary collapse

HOST_NAME =
'www.chemspider.com'

Instance Method Summary collapse

Constructor Details

#initialize(token, header = {}) ⇒ RestClient

Returns a new instance of RestClient.



12
13
14
15
# File 'lib/ruspider/rest_client.rb', line 12

def initialize(token, header = {})
  @token = token
  @header = header
end

Instance Method Details

#post(api, op, options = {}) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/ruspider/rest_client.rb', line 27

def post(api, op, options = {})
  return -1 if op.empty?
  ops = { token: @token }.merge(options)
  res = HTTParty.post(
    "https://#{HOST_NAME}/#{api}/#{op}",
    header: { header: @header }, body: ops,
    query_string_normalizer: ->(h) { query_string_normalizer(h) }
  )

  raise "#{res.response.code}: #{res.parsed_response}" unless res.success?
  res.response.body
end

#query_string_normalizer(query) ⇒ Object



17
18
19
20
21
22
23
24
25
# File 'lib/ruspider/rest_client.rb', line 17

def query_string_normalizer(query)
  query.to_hash.map do |k, v|
    if v.class == Array
      v.map { |x| "#{k}=#{x}" }.join('&')
    else
      HTTParty::HashConversions.normalize_param(k, v)
    end
  end.join
end