class Screenplay::ApiActor
Attributes
Public Instance Methods
configure(config)
click to toggle source
# File lib/screenplay/actors/api.rb, line 16 def configure(config) @url = config[:url].to_s @url += '/' unless @url.end_with?('/') @headers = {} @config = config @cookies = nil end
play(params, input)
click to toggle source
# File lib/screenplay/actors/api.rb, line 24 def play(params, input) raise 'Missing configuration api.url' if @url == '/' path = params[:path] method = params[:method].downcase.to_sym rescue :get expects = (params[:expect] || 200).to_i data = params[:data] || {} rescue {} data = input if (data.is_a?(String)) && (data == '$input') data.stringify_keys! headers = {} headers['Content-Type'] = @config[:content_type] if (!@config[:content_type].nil?) headers[:cookie] = @cookies unless @cookies.nil? url = @url + path.to_s.replace_vars(input) if ([:get, :head, :delete].include?(method)) headers[:params] = data data = {} end if (headers['Content-Type'] == 'application/json') data = JSON.generate(data) end puts "\nAPI - #{method.to_s.upcase} - #{url}: #{data}" if Screenplay.options[:debug] begin response = RestClient::Request.execute({ url: url, method: method, headers: headers, payload: data }) rescue => e raise e if e.response.nil? response = e.response end if response.code != expects raise WrongResponseCodeException.new(expects, response.code, response.body.to_s) end unless response.nil? output = JSON.parse(response.body) rescue {} @cookies = response.headers[:set_cookie][0] rescue nil end return output || {} end