Module: IdigbioClient::Helper

Included in:
IdigbioClient, Search
Defined in:
lib/idigbio_client/helper.rb

Overview

Private internal methods

Instance Method Summary collapse

Instance Method Details

#get(url, params) ⇒ Object



27
28
29
30
# File 'lib/idigbio_client/helper.rb', line 27

def get(url, params)
  params[:query] = params.delete(:params)
  request(:get, url, params)
end

#normalize_type(type) ⇒ Object



38
39
40
41
42
43
# File 'lib/idigbio_client/helper.rb', line 38

def normalize_type(type)
  type = type.to_s
  return type if IdigbioClient.types.include?(type)
  sym_types = types.map { |t| ":#{t}" }.join(", ")
  fail "Unknown type :#{type}. Types: #{sym_types}"
end

#post(url, params) ⇒ Object



23
24
25
# File 'lib/idigbio_client/helper.rb', line 23

def post(url, params)
  request(:post, url, params)
end

#post?(method) ⇒ Boolean

Returns:

  • (Boolean)


19
20
21
# File 'lib/idigbio_client/helper.rb', line 19

def post?(method)
  !method.to_s.match(/get/i)
end

#query(opts) ⇒ Object



4
5
6
7
8
9
10
# File 'lib/idigbio_client/helper.rb', line 4

def query(opts)
  url, params = url_params(opts)
  resp = post?(opts[:method]) ? post(url, params) : get(url, params)
  resp = JSON.parse(resp.body, symbolize_names: true) if resp
  sleep(0.3)
  block_given? ? yield(resp) : resp
end

#request(method, url, params) ⇒ Object



32
33
34
35
36
# File 'lib/idigbio_client/helper.rb', line 32

def request(method, url, params)
  RestClient.send(method, url, params) do |resp, _req, _res|
    resp.code == 200 ? resp : nil
  end
end

#symbolize(h) ⇒ Object



45
46
47
48
49
50
51
# File 'lib/idigbio_client/helper.rb', line 45

def symbolize(h)
  h.keys.each do |k|
    sym = k.to_sym
    h[sym] = h.delete(k)
    symbolize(h[sym]) if h[sym].is_a? Hash
  end
end

#url_params(opts) ⇒ Object



12
13
14
15
16
17
# File 'lib/idigbio_client/helper.rb', line 12

def url_params(opts)
  opts = { method: "post" }.merge(opts)
  url = URL + opts[:path]
  params = HEADERS.merge(params: opts[:params].to_json)
  [url, params]
end