Skip to content

Commit

Permalink
Merge pull request #1 from testCloud/hotfix/name_colision
Browse files Browse the repository at this point in the history
Hotfix/name colision
  • Loading branch information
garciaf committed Jan 28, 2016
2 parents ccd2854 + c70f272 commit 8d6be11
Show file tree
Hide file tree
Showing 62 changed files with 1,592 additions and 1,488 deletions.
2 changes: 1 addition & 1 deletion leantesting.gemspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Gem::Specification.new do |s|
s.name = 'leantesting'
s.version = '1.0.1'
s.version = '1.0.2'
s.date = '2015-11-20'
s.platform = Gem::Platform::RUBY
s.summary = 'Lean Testing Ruby SDK'
Expand Down
305 changes: 154 additions & 151 deletions lib/BaseClass/APIRequest.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,193 +3,196 @@
#
# An APIRequest's parameters can be modified on demand and can be executed multiple times for the same instance.
#
class APIRequest

#
# Constructs API request definition.
#
# Arguments:
# origin Client -- Originating client reference
# endpoint String -- API endpoint
# method String -- Method for cURL call - supports GET, POST, PUT or DELETE only
# opts Hash -- (optional) Additional options to pass to request.
# Request parameters (if any) must bep assed here.
#
# Exceptions:
# SDKInvalidArgException if method is non-string.
# SDKInvalidArgException if unsupported method is provided.
# SDKInvalidArgException if endpoint is non-string.
# SDKInvalidArgException if opts param is not a hash.
#
def initialize(origin, endpoint, method, opts = nil)
@default_ops = { # Basic support for extended opts
'base_uri' => 'https://api.leantesting.com', # assumed default for API base
'form_data' => false, # sets content type to multipart/form-data if true
'params' => {} # params to be pased in request
}

if !opts
opts = {}
end

if !method.is_a? String
raise SDKInvalidArgException, '`method` must be a string'
elsif !['GET', 'POST', 'PUT', 'DELETE'].include? method
raise SDKInvalidArgException, 'unsupported ' + method + ' `method`'
elsif !endpoint.is_a? String
raise SDKInvalidArgException, '`endpoint` must be a string'
elsif !opts.is_a? Hash
raise SDKInvalidArgException, '`opts` must be a hash'
end
module LeanTesting
class APIRequest

#
# Constructs API request definition.
#
# Arguments:
# origin Client -- Originating client reference
# endpoint String -- API endpoint
# method String -- Method for cURL call - supports GET, POST, PUT or DELETE only
# opts Hash -- (optional) Additional options to pass to request.
# Request parameters (if any) must bep assed here.
#
# Exceptions:
# SDKInvalidArgException if method is non-string.
# SDKInvalidArgException if unsupported method is provided.
# SDKInvalidArgException if endpoint is non-string.
# SDKInvalidArgException if opts param is not a hash.
#
def initialize(origin, endpoint, method, opts = nil)
@default_ops = { # Basic support for extended opts
'base_uri' => 'https://api.leantesting.com', # assumed default for API base
'form_data' => false, # sets content type to multipart/form-data if true
'params' => {} # params to be pased in request
}

if !opts
opts = {}
end

@opts = @default_ops.clone
self.updateOpts(opts)
if !method.is_a? String
raise SDKInvalidArgException, '`method` must be a string'
elsif !['GET', 'POST', 'PUT', 'DELETE'].include? method
raise SDKInvalidArgException, 'unsupported ' + method + ' `method`'
elsif !endpoint.is_a? String
raise SDKInvalidArgException, '`endpoint` must be a string'
elsif !opts.is_a? Hash
raise SDKInvalidArgException, '`opts` must be a hash'
end

@origin = origin
@endpoint = endpoint
@method = method
end
@opts = @default_ops.clone
self.updateOpts(opts)

#
# Updates options list inside API request definition.
#
# Arguments:
# opts Hash -- (optional) Additional options array to merge with previous option values
#
# Exceptions:
# SDKInvalidArgException if opts param is not a hash.
# SDKInvalidArgException if provided parameter list is non-hash parameter.
#
def updateOpts(opts = nil)
if !opts
opts = {}
@origin = origin
@endpoint = endpoint
@method = method
end

if !opts.is_a? Hash
raise SDKInvalidArgException, '`opts` must be a hash'
elsif opts.has_key? 'params' && !opts['params'].is_a?(Hash)
raise SDKInvalidArgException '`opts[\'params\']` must be a hash'
#
# Updates options list inside API request definition.
#
# Arguments:
# opts Hash -- (optional) Additional options array to merge with previous option values
#
# Exceptions:
# SDKInvalidArgException if opts param is not a hash.
# SDKInvalidArgException if provided parameter list is non-hash parameter.
#
def updateOpts(opts = nil)
if !opts
opts = {}
end

if !opts.is_a? Hash
raise SDKInvalidArgException, '`opts` must be a hash'
elsif opts.has_key? 'params' && !opts['params'].is_a?(Hash)
raise SDKInvalidArgException '`opts[\'params\']` must be a hash'
end

@opts.merge!(opts)
end

@opts.merge!(opts)
end
#
# Executes cURL call as per current API definition state.
#
# Returns:
# String -- Returns resulting data response from server (including errors and inconsistencies)
#
def call

#
# Executes cURL call as per current API definition state.
#
# Returns:
# String -- Returns resulting data response from server (including errors and inconsistencies)
#
def call
ch = Curl::Easy.new

ch = Curl::Easy.new
callUrl = @opts['base_uri'] + @endpoint

callUrl = @opts['base_uri'] + @endpoint
if @origin.getCurrentToken.is_a? String
ch.headers['Authorization'] = 'Bearer ' + @origin.getCurrentToken
end

if @origin.getCurrentToken.is_a? String
ch.headers['Authorization'] = 'Bearer ' + @origin.getCurrentToken
end
ch.url = callUrl

ch.url = callUrl
ch.header_in_body = false

ch.header_in_body = false
case @method
when 'GET'
callUrl += '?' + Curl::postalize(@opts['params'])
ch.url = callUrl

case @method
when 'GET'
callUrl += '?' + Curl::postalize(@opts['params'])
ch.url = callUrl
ch.http_get
when 'POST'
if @opts['form_data'] == true && (@opts.has_key? 'file_path')
ch.headers['Content-Type'] = 'multipart/form-data'
ch.multipart_form_post = true

ch.http_get
when 'POST'
if @opts['form_data'] == true && (@opts.has_key? 'file_path')
ch.headers['Content-Type'] = 'multipart/form-data'
ch.multipart_form_post = true
ch.http_post(Curl::PostField.file('file', @opts['file_path']))
else
jsonData = JSON.generate(@opts['params'])

ch.http_post(Curl::PostField.file('file', @opts['file_path']))
else
ch.headers['Content-Type'] = 'application/json'
ch.headers['Content-Length'] = jsonData.length

ch.http_post(jsonData)
end
when 'PUT'
jsonData = JSON.generate(@opts['params'])

ch.headers['Content-Type'] = 'application/json'
ch.headers['Content-Length'] = jsonData.length

ch.http_post(jsonData)
ch.http_put(jsonData)
when 'DELETE'
ch.http_delete
end
when 'PUT'
jsonData = JSON.generate(@opts['params'])

ch.headers['Content-Type'] = 'application/json'
ch.headers['Content-Length'] = jsonData.length
curlData = ch.body_str
curlStatus = ch.status.to_i()

ch.close
ch = nil

ch.http_put(jsonData)
when 'DELETE'
ch.http_delete
{
'data' => curlData,
'status' => curlStatus
}
end

curlData = ch.body_str
curlStatus = ch.status.to_i()
#
# Does cURL data interpretation
#
# Exceptions:
# SDKErrorResponseException if the remote response is an error.
# A server response is interpreted as an error if obtained status code differs from expected status code.
# Expected status codes are `200 OK` for GET/POST/PUT, `204 No Content` for DELETE.
# SDKBadJSONResponseException if the remote response contains erronated or invalid JSON contents
#
# Returns:
# Hash -- In case of successful request, a JSON decoded object is returned.
# Boolean -- If a DELETE request is issued, returns true if call is successful (exception otherwise).
#
def exec
if @origin.debugReturn && @origin.debugReturn.has_key?('data') && @origin.debugReturn.has_key?('status')

curlData = @origin.debugReturn['data']
curlStatus = @origin.debugReturn['status']

ch.close
ch = nil
else

{
'data' => curlData,
'status' => curlStatus
}
end
callReturn = call
curlData = callReturn['data']
curlStatus = callReturn['status']

#
# Does cURL data interpretation
#
# Exceptions:
# SDKErrorResponseException if the remote response is an error.
# A server response is interpreted as an error if obtained status code differs from expected status code.
# Expected status codes are `200 OK` for GET/POST/PUT, `204 No Content` for DELETE.
# SDKBadJSONResponseException if the remote response contains erronated or invalid JSON contents
#
# Returns:
# Hash -- In case of successful request, a JSON decoded object is returned.
# Boolean -- If a DELETE request is issued, returns true if call is successful (exception otherwise).
#
def exec
if @origin.debugReturn && @origin.debugReturn.has_key?('data') && @origin.debugReturn.has_key?('status')

curlData = @origin.debugReturn['data']
curlStatus = @origin.debugReturn['status']

else

callReturn = call
curlData = callReturn['data']
curlStatus = callReturn['status']
end

end
if @method == 'DELETE'
expectedHTTPStatus = 204
else
expectedHTTPStatus = 200
end

if @method == 'DELETE'
expectedHTTPStatus = 204
else
expectedHTTPStatus = 200
end
if curlStatus != expectedHTTPStatus
raise SDKErrorResponseException, curlStatus.to_s() + ' - ' + curlData
end

if curlStatus != expectedHTTPStatus
raise SDKErrorResponseException, curlStatus.to_s() + ' - ' + curlData
end
if @method == 'DELETE' # if DELETE request, expect no output
return true
end

if @method == 'DELETE' # if DELETE request, expect no output
return true
end
begin
jsonData = JSON.parse(curlData) # normally, expect JSON qualified output
rescue JSON::ParserError
raise SDKBadJSONResponseException, curlData
end

begin
jsonData = JSON.parse(curlData) # normally, expect JSON qualified output
rescue JSON::ParserError
raise SDKBadJSONResponseException, curlData
end
if jsonData.length.zero?
raise SDKUnexpectedResponseException, 'Empty object received'
end

if jsonData.length.zero?
raise SDKUnexpectedResponseException, 'Empty object received'
end
return jsonData

return jsonData
end

end

end
end
Loading

0 comments on commit 8d6be11

Please sign in to comment.