Class: TicketNetwork::Base::Request

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

Instance Method Summary collapse

Methods included from FormatHelper

#format_key, #format_query_key, #format_query_value

Constructor Details

#initialize(action, parameters) ⇒ Request

Returns a new instance of Request.



32
33
34
35
# File 'lib/ticket_network/base.rb', line 32

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

Instance Method Details

#performObject

Executes the call to the web service.



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

def perform
  uri = URI.parse(url)

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

  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
      puts $!.inspect
      raise #NetworkError.new($!.to_s)
    end
  end

  case body = response.body
    when /^Authorization error: IP ([\d\.]+)/
      raise AuthorizationError, "Unauthorized IP (#{$1})"
    when /must be SSL/
      raise AuthorizationError, 'SSL Required'
    when /^Missing parameter: (.*)\./
      raise ParameterError, "Missing #{$1}"
    when /^Parameter error: (.*): (.*)\./
      raise ParameterError, "Invalid #{$1} (#{$2})"
    when /\A(?!<\?xml)/
      raise Error, body.split(/\./).first
    else
      @action.parser.parse(body) if body
  end
end