Class: Scirocco::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/scirocco/client.rb

Constant Summary collapse

API_POLL_SEC =

test result polled every poll seconds

15

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key, options = {}) ⇒ Client

Returns a new instance of Client.



10
11
12
13
14
15
16
17
18
19
20
# File 'lib/scirocco/client.rb', line 10

def initialize(api_key, options={})
  @api_key = api_key
  @scheme, @host, @port, @version, @request_timeout, @open_timeout = {
    scheme: "https",
    host: HOSTNAME,
    port: 443,
    version: API_VERSION,
    request_timeout: 600,
    open_timeout: 10
  }.merge(options.symbolize_keys).values
end

Instance Attribute Details

#api_keyObject

Returns the value of attribute api_key.



6
7
8
# File 'lib/scirocco/client.rb', line 6

def api_key
  @api_key
end

#hostObject

Returns the value of attribute host.



6
7
8
# File 'lib/scirocco/client.rb', line 6

def host
  @host
end

#last_requestObject

Returns the value of attribute last_request.



6
7
8
# File 'lib/scirocco/client.rb', line 6

def last_request
  @last_request
end

#open_timeoutObject

Returns the value of attribute open_timeout.



6
7
8
# File 'lib/scirocco/client.rb', line 6

def open_timeout
  @open_timeout
end

#portObject

Returns the value of attribute port.



6
7
8
# File 'lib/scirocco/client.rb', line 6

def port
  @port
end

#request_timeoutObject

Returns the value of attribute request_timeout.



6
7
8
# File 'lib/scirocco/client.rb', line 6

def request_timeout
  @request_timeout
end

#schemeObject

Returns the value of attribute scheme.



6
7
8
# File 'lib/scirocco/client.rb', line 6

def scheme
  @scheme
end

#versionObject

Returns the value of attribute version.



6
7
8
# File 'lib/scirocco/client.rb', line 6

def version
  @version
end

Instance Method Details

#abort_allObject



88
89
90
91
92
93
94
# File 'lib/scirocco/client.rb', line 88

def abort_all
  url = build_url("tests", "abort_all")
  data = {
    :api_key => @api_key,
  }
  post(url, data)
end

#abort_test(test_job_id) ⇒ Object



79
80
81
82
83
84
85
86
# File 'lib/scirocco/client.rb', line 79

def abort_test(test_job_id)
  url = build_url("tests", "abort")
  data = {
    :api_key => @api_key,
    :test_job_id => test_job_id
  }
  post(url, data)
end

#apps(project_id) ⇒ Object

App API



101
102
103
104
# File 'lib/scirocco/client.rb', line 101

def apps(project_id)
  url = build_url("apps")
  get(url, {:project_id => project_id})
end

#build_url(type, resource = nil) ⇒ Object



117
118
119
# File 'lib/scirocco/client.rb', line 117

def build_url(type, resource=nil)
  @scheme + "://" + [@host + ":" + @port.to_s, "api", @version, type, resource].compact.join("/") + "/"
end

#check_test(test_job_id) ⇒ Object



73
74
75
76
77
# File 'lib/scirocco/client.rb', line 73

def check_test(test_job_id)
  url = build_url("tests", "check")
  params = { test_job_id: test_job_id }
  get(url, params)
end

#devices(project_id, params = {}) ⇒ Object

Device API



31
32
33
34
# File 'lib/scirocco/client.rb', line 31

def devices(project_id, params={})
  url = build_url("devices")
  get(url, {:project_id => project_id}.merge(params))
end

#get(url, params = {}) ⇒ Object



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/scirocco/client.rb', line 121

def get(url, params={})
  @last_request = {
    url: url,
    request: params
  }

  query_string_params = params.collect{ |p| "&#{p[0].to_s}=#{p[1].to_s}" }.join
  #p "#{url}?api_key=#{@api_key}#{query_string_params}"

  begin
    response = RestClient::Request.execute(:method => :get, :url => "#{url}?api_key=#{@api_key}#{query_string_params}", :timeout => @request_timeout, :open_timeout => @open_timeout)
    @last_request[:response] = response
    return JSON.parse(response)
  rescue => err
    if err.response
      pp JSON.parse(err.response)
      raise err.message
    else
      raise err
    end
  end
end

#poll_test_result(test_job_id) ⇒ Object

Checks for when test completes



58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/scirocco/client.rb', line 58

def poll_test_result(test_job_id)
  status = nil
  while !['passed', 'failed'].include?(status)
    sleep(API_POLL_SEC)
    test_status = check_test(test_job_id)["test_status"]
    status = test_status["status"]
    if ['passed', 'failed'].include?(status)
      return test_status
    else
      puts "polling...   status: #{status}"
    end
  end

end

#post(url, data, capture_request = true) ⇒ Object



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/scirocco/client.rb', line 144

def post(url, data, capture_request=true)
  if capture_request
    @last_request = {
      url: url,
      request: data
    }
  end

  begin
    result = RestClient::Request.execute(:method => :post, :url => url, :payload => data, :timeout => @request_timeout, :open_timeout => @open_timeout)
    @last_request[:response] = result if capture_request
    JSON.parse(result)
  rescue => err
    if err.response
      pp JSON.parse(err.response)
      raise err.message
    else
      raise err
    end
  end
end

#projectsObject



22
23
24
25
# File 'lib/scirocco/client.rb', line 22

def projects
  url = build_url("projects")
  get(url)
end

#run_test(test_class_id, device_id) ⇒ Object



46
47
48
49
50
51
52
53
54
55
# File 'lib/scirocco/client.rb', line 46

def run_test(test_class_id, device_id)
  url = build_url("tests", "run")
  data = {
    :api_key => @api_key,
    :test_class_id => test_class_id,
    :device_id => device_id
  }

  post(url, data)
end

#tests(project_id) ⇒ Object

Test API



41
42
43
44
# File 'lib/scirocco/client.rb', line 41

def tests(project_id)
  url = build_url("tests")
  get(url, {:project_id => project_id})
end

#upload_app(project_id, app_path) ⇒ Object



106
107
108
109
110
111
112
113
114
115
# File 'lib/scirocco/client.rb', line 106

def upload_app(project_id, app_path)
  url = build_url("apps", "upload")
  data = {
    :multipart => true,
    :api_key    => @api_key,
    :project_id => project_id,
    :file   => File.new(app_path)
  }
  post(url, data)
end