Class: Rainforest::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/rainforest/client.rb,
lib/rainforest/client/version.rb

Constant Summary collapse

API_END_POINT =
'https://app.rainforestqa.com/api/1/'
VERSION =
"0.0.10.1"
@@api_key =
nil

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeClient

Returns a new instance of Client.



21
22
# File 'lib/rainforest/client.rb', line 21

def initialize
end

Class Method Details

.api_keyObject



17
18
19
# File 'lib/rainforest/client.rb', line 17

def self.api_key
  @@api_key
end

.api_key=(api_key) ⇒ Object



14
15
16
# File 'lib/rainforest/client.rb', line 14

def self.api_key=(api_key)
  @@api_key = api_key
end

Instance Method Details

#build_request_uri(resource) ⇒ Object

More information about resource at: app.rainforestqa.com/docs

Raises:

  • (ArgumentError)


25
26
27
28
29
# File 'lib/rainforest/client.rb', line 25

def build_request_uri(resource)
  _resource = resource.split('/')[0]
  raise ArgumentError, 'resource is invalid' unless ['auth', 'clients', 'runs', 'tests', 'environments', 'pricing_plan'].include? _resource
  return "#{API_END_POINT}#{resource}"
end

#request(cmd, payload, method = :post) ⇒ Object



31
32
33
34
35
36
37
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
# File 'lib/rainforest/client.rb', line 31

def request(cmd, payload, method = :post)
  raise Rainforest::ConfigurationError, "Client token is missing" if @@api_key == nil

  url = build_request_uri(cmd)
  payload = payload.to_json if payload.class == Hash
  
  if [:get, :post, :put, :delete].include? method
    case method
      when :get
        response = HTTParty.get url, {
          body: payload, 
          headers: {"CLIENT_TOKEN" => @@api_key, "Content-type" => "application/json"}
        }
      when :post
        response = HTTParty.post url, {
          body: payload, 
          headers: {"CLIENT_TOKEN" => @@api_key, "Content-type" => "application/json"}
        }
      when :put
        response = HTTParty.put url, {
          body: payload, 
          headers: {"CLIENT_TOKEN" => @@api_key, "Content-type" => "application/json"}
        }
      when :delete
        response = HTTParty.delete url, {
          body: payload, 
          headers: {"CLIENT_TOKEN" => @@api_key, "Content-type" => "application/json"}
        }
    end
    body = JSON.parse(response.body)
    if body.include? 'error'
      raise body['error']
    end 
    return JSON.parse(response.body)
  end
  raise ArgumentError, "Invalid Method. We support only: :get, :post, :put, :delete"
end