Class: MasterCard::Core::Controller::APIController

Inherits:
Object
  • Object
show all
Includes:
MasterCard::Core, Exceptions, Security
Defined in:
lib/mastercard/core/controller.rb

Constant Summary collapse

ACTION_CREATE =
"CREATE"
ACTION_DELETE =
"DELETE"
ACTION_UPDATE =
"UPDATE"
ACTION_READ =
"READ"
ACTION_LIST =
"LIST"
ACTION_QUERY =
"QUERY"
KEY_ID =
"id"
KEY_FORMAT =
"Format"
KEY_ACCEPT =
"Accept"
KEY_USER_AGENT =
"User-Agent"
KEY_CONTENT_TYPE =
"Content-Type"
APPLICATION_JSON =
"application/json"
RUBY_SDK =
"Ruby_SDK"
JSON_STR =
"JSON"

Instance Method Summary collapse

Constructor Details

#initialize(version = nil) ⇒ APIController

Returns a new instance of APIController.



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/mastercard/core/controller.rb', line 59

def initialize(version=nil)
  #Set the parameters
  @baseURL = Config.getAPIBaseURL()

  @baseURL = removeForwardSlashFromTail(@baseURL)

  #Verify if the URL is correct
  unless Util.validateURL(@baseURL)
    raise APIException.new "URL: '" + @baseURL + "' is not a valid url"
  end

  #Set the version
  unless version.nil?
    @version = version
  else
    @version = Constants::VERSION
  end

end

Instance Method Details

#execute(action, resourcePath, headerKey, queryKey, input) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/mastercard/core/controller.rb', line 79

def execute(action,resourcePath,headerKey,queryKey,input)

  #Check preconditions for execute
  preCheck()

  #Separate the headers from the inputMap
  headers = Util.subMap(input,headerKey)

  #Separate the query from the inputMap
  queryParams = Util.subMap(input,queryKey)

  #Get the resourcePath containing values from input
  resourcePath = getFullResourcePath(action,resourcePath,input)

  #Get the path parameters
  pathParams = getPathParams(action,queryParams,input)
  #Get the body
  body = getBody(action,input)

  #Get the request Object
  request = getRequestObject(resourcePath,action,pathParams,body)

  #Add headers to request
  headers.each do |key,value|
    request.add_field(key,value)
  end

  fullUrl = @baseURL+resourcePath
  #Sign and get back the request
  request = Config.getAuthentication().signRequest(fullUrl,request,request.method,body,pathParams)

  uri = URI.parse(@baseURL)
  #Get the http object
  http = getHTTPObject(uri)

  if Config.isDebug
    puts "---- Request ----"
    puts ""
    puts "URL"
    puts @baseURL+request.path
    puts ""
    puts "Headers"
    request.each_header do |header_name, header_value|
      puts "#{header_name} : #{header_value}"
    end
    puts ""
    puts "Body"
    puts request.body
  end

  begin
    response = http.request(request)

    if Config.isDebug
      puts "---- Response ----"
      puts ""
      puts "Status Code"
      puts response.code
      puts ""
      puts "Headers"
      response.each_header do |header_name, header_value|
        puts "#{header_name} : #{header_value}"
      end
      puts ""
      puts "Body"
      puts response.body
    end


    return handleResponse(response,response.body)
  rescue Errno::ECONNREFUSED
    raise APIException.new ("Connection to server could not be established.")
  end


end