Class: Testrail::Client::Api

Inherits:
Request
  • Object
show all
Defined in:
lib/testrail/api.rb

Overview

Metaprogrammed class to provide an idiomatic interface to the testrails API

Instance Attribute Summary

Attributes inherited from Request

#password, #url, #user

Method Summary

Methods inherited from Request

#initialize, #send_get, #send_post

Constructor Details

This class inherits a constructor from Testrail::Client::Request

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, *args, &block) ⇒ Object (private)

Using Meta programming, catch all endpoints adn send a request to them

Parameters:

  • m (string)

    name of the missing method

  • args (Array)

    Arguments

  • block (Proc)

    Block



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/testrail/api.rb', line 12

def method_missing(m, *args, &block)
  x,y = *args
  id = x if x.is_a? Integer #if Integer it is an ID
  opts = x if x.is_a? Hash #if Hash it is an options array
  opts ||= y #if opts not set, take second arg
  begin
    case m
    when /get/
      send_get("#{m}/#{id}&#{_param_stringify(opts)}")
    when /add_attachment/
      send_post("#{m}/#{id}",opts.to_a)
    else
      send_post("#{m}/#{id}",opts)
    end
  rescue Testrail::Client::APIError => error
    if error.message.include? "API Rate"
      time_to_wait = 60
      STDERR.puts("Hit Testrail\'s API rate Limits, Sleeing #{time_to_wait} seconds")
      sleep(time_to_wait)
      STDERR.puts("Retrying #{m} with #{args}")
      retry
    else
      raise error
    end
  end
end