Class: EPC::Client::BaseClient

Inherits:
Object
  • Object
show all
Defined in:
lib/epc/client/base_client.rb

Direct Known Subclasses

HttpClient

Defined Under Namespace

Classes: BadResponse, HTTPException

Instance Method Summary collapse

Constructor Details

#initialize(target_url = EPC::Config::DEFAULT_TARGET, caller_id = nil, auth_token = nil, debug = false) ⇒ BaseClient

Returns a new instance of BaseClient.



12
13
14
15
16
17
18
19
20
21
# File 'lib/epc/client/base_client.rb', line 12

def initialize(target_url=EPC::Config::DEFAULT_TARGET, caller_id = nil, auth_token = nil, debug = false)
  target_url = "http://#{target_url}" unless target_url =~ /^https?/
  target_url = target_url.gsub(/\/+$/, '')
  @target =  target_url

  @caller_id = caller_id || EPC::Config.caller_id
  @auth_token = auth_token || EPC::Config.auth_token
  @digest = OpenSSL::Digest::Digest.new('sha256')
  @debug = debug
end

Instance Method Details

#request(method, path, content_type = nil, payload = nil, headers = {}, skip_signature = false) ⇒ Object

returns an array of status, body, response_headers



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
# File 'lib/epc/client/base_client.rb', line 24

def request(method, path, content_type = nil, payload = nil, headers = {}, skip_signature = false)
  salt = generate_salt
  now = Time.now
          
  headers = headers.dup
  unless skip_signature
    headers['X-AGILEMETHODS-SALT'] = salt
    headers['X-AGILEMETHODS-TIMESTAMP'] = now.to_f
    headers['X-AGILEMETHODS-CALLER-ID'] = @caller_id if @caller_id
    headers['X-AGILEMETHODS-SIGNATURE'] = generate_signature(salt, now.to_f, path, payload)
  end

  if content_type
    headers['Content-Type'] = content_type
    headers['Accept'] = content_type
  end
  
  if path =~ /^https?/
    url = path
  else
    url = "#{@target}#{path}"
  end

  req = {
    :method => method,
    :url => url,
    :payload => payload,
    :headers => headers
  }

  begin
    result = nil
    RestClient::Request.execute(req) { |response, request|
      result = [response.code, response.body, response.headers]
    }
    result
  rescue Net::HTTPBadResponse => e
    raise BadTarget "Received bad HTTP response from target: #{e}"
  rescue SystemCallError, RestClient::Exception => e
    raise HTTPException, "HTTP exception: #{e}"
  end
end