Method: Epom::EpomElement.generic_method

Defined in:
lib/epom/epom_element.rb

.generic_method(method_name, url_params, body_params) ⇒ Object



24
25
26
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/epom/epom_element.rb', line 24

def self.generic_method(method_name, url_params, body_params)
  signature = extended_methods[method_name]
  url_params_signature = signature[:url_parameters]
  body_params_signature = signature[:body_parameters]
  url_signature = signature[:url]   

  url_params.symbolize_keys! if url_params.is_a? Hash
  body_params.symbolize_keys! if body_params.is_a? Hash   
  
  method = signature[:method]

  if signature[:headers]
    headers signature[:headers]
  else
    default_options[:headers] = {}
  end

  if signature.has_key?(:format)
    format signature[:format]
  else
    format :json
  end

  timestamp = Time.now.to_i * 1000
  if body_params_signature.include?(:hash) and not body_params[:hash]
    body_params[:hash] = Epom.create_hash(Epom.create_hash(Epom.config.password), timestamp)
  end
  if body_params_signature.include?(:timestamp) and not body_params[:timestamp]
    body_params[:timestamp] = timestamp
  end
  if body_params_signature.include?(:username) and not body_params[:username]
    body_params[:username] = Epom.config.username
  end
  
  if url_params_signature and url_params_signature.include?(:login) and not url_params[:login]
    url_params[:login] = Epom.config.username
  end
  if url_params_signature and url_params_signature.include?(:hash) and not url_params[:hash]
    url_params[:hash] = Epom.create_hash(Epom.create_hash(Epom.config.password), timestamp)
  end
  if url_params_signature and url_params_signature.include?(:timestamp) and not url_params[:timestamp]
    url_params[:timestamp] = timestamp
  end

  if params_validation(url_params, url_params_signature) and params_validation(body_params, body_params_signature)
    http_proxy Epom.config.proxy_address, Epom.config.proxy_port, Epom.config.proxy_user, Epom.config.proxy_password
    base_uri Epom.config.epom_server
    url = replace_params_in_url(url_signature, url_params)

    puts method_name
    response = send(method, url, :query => body_params)
    if response.success?
      return response.parsed_response
    else
      response.code
    end
  else
    raise ArgumentError, 'Error in params_validation method'
  end
end