Class: Ruspider::RestClient
- Inherits:
-
Object
- Object
- Ruspider::RestClient
- 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
-
#initialize(token, header = {}) ⇒ RestClient
constructor
A new instance of RestClient.
- #post(api, op, options = {}) ⇒ Object
- #query_string_normalizer(query) ⇒ Object
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, = {}) return -1 if op.empty? ops = { token: @token }.merge() 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 |