Class: EventInventory::Base::Request

Inherits:
Object
  • Object
show all
Includes:
FormatHelper
Defined in:
lib/event_inventory/base.rb

Instance Method Summary collapse

Methods included from FormatHelper

#format_key, #format_query_value

Constructor Details

#initialize(action, parameters) ⇒ Request

Returns a new instance of Request.



33
34
35
36
# File 'lib/event_inventory/base.rb', line 33

def initialize(action, parameters)
  @action = action
  @parameters = HashWithIndifferentAccess.new(parameters)
end

Instance Method Details

#performObject

Executes the call to the web service.



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
# File 'lib/event_inventory/base.rb', line 39

def perform
  uri = URI.parse(url)

  session = Patron::Session.new
  session.timeout = 60
  session.base_url = "#{uri.scheme}://#{uri.host}:#{uri.port}/"
  session.proxy = EventInventory.proxy if EventInventory.proxy

  retries = 5

  begin
    response = case @action.method
      when :get
        session.get([uri.path, uri.query].compact.join('?'))
      when :post
        session.post(uri.path, uri.query)
    end
  rescue Patron::PartialFileError, Patron::ConnectionFailed, Patron::HostResolutionError, Patron::TimeoutError
    if (retries -= 1) > 0
      retry
    else
      raise NetworkError.new($!.to_s)
    end
  end

  case body = response.body
    when /Security Token: (\w+) \(APPCLIENT_ID\) is not registered/
      raise AuthorizationError, "Invalid security token (#{$1})"
    when /IP Address: ([\d\.]+) is not enabled/
      raise AuthorizationError, "Unauthorized IP (#{$1})"
    when /^Missing parameter: (.*)\./
      raise ParameterError, "Missing #{$1}"
    when /There was an error parsing (\w+) parameter \[(\w+)=(.*?)\]/
      raise ParameterError, "Invalid #{$2} (#{$1}, \"#{$3}\")"
    else
      @action.parser.parse(body) if body
  end
end