Module: CoverMyMeds::HostAndPath

Included in:
Consumers, Credentials, Drugs, Forms, Indicators, RequestPages, Requests, Tokens
Defined in:
lib/cover_my_meds/meta/host_and_path.rb

Class Method Summary collapse

Class Method Details

.included(base) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/cover_my_meds/meta/host_and_path.rb', line 27

def self.included(base)
  api_name = base.name.demodulize.underscore.downcase

  base.instance_exec do
    attr_writer "#{api_name}_path".to_sym
    define_method "#{api_name}_path" do
      instance_variable_get("@#{api_name}_path") || "/#{api_name.dasherize}/"
    end

    attr_writer "#{api_name}_host".to_sym
    define_method "#{api_name}_host" do
      instance_variable_get("@#{api_name}_host") || default_host
    end

    define_method "#{api_name}_request" do |method, options={}, &block|
      options    =  options.symbolize_keys
      path       =  options[:path] || ""
      params     =  options[:params]
      auth       =  options[:auth]
      host_name  =  public_send("#{api_name}_host".to_sym)
      full_path  =  public_send("#{api_name}_path".to_sym) + path

      proxy_args = [ method, host_name, full_path, params, auth ]
      fail ArgumentError, "method, host_name or full_path can not be nil" if proxy_args.take(3).any?(&:nil?)

      request(*proxy_args.compact, &block)
    end
  end

end