Module: Appboard::RestAPI

Included in:
Appboard
Defined in:
lib/appboard/rest_api.rb

Instance Method Summary collapse

Instance Method Details

#default_headersObject



4
5
6
7
8
9
# File 'lib/appboard/rest_api.rb', line 4

def default_headers
  {
    :content_type => :json,
    :accept       => :json
  }
end

#send_data(url, payload) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/appboard/rest_api.rb', line 11

def send_data(url, payload)
  json = JSON.generate(payload)
  
  begin                                                                                                            
    RestClient.put url, json, default_headers                             
  rescue RestClient::Exception => e
    message = ""
  
    begin    
      message = JSON.parse(e.response.body)["message"] if e.response.body
    rescue JSON::ParserError => pe
      # ignore
    end 
    
    raise Error::BadRequest, message if e.http_code == 400 
    raise Error::NotFound, message if e.http_code == 404 
    raise Error::ThrottleLimit, message if e.http_code == 503
    raise e
  end
end

#send_setup(url, config) ⇒ Object



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
68
69
70
# File 'lib/appboard/rest_api.rb', line 32

def send_setup(url, config)
  success = false
  json = config.get_dashboard.to_json

  begin                                                                                                            
    response = RestClient.put url, json, default_headers
  
    begin    
      dr = JSON.parse(response.body) if response.body
  
      # update widgets with data in response
      dr["widgets"].each do |rw| 
        widget = config.get_dashboard.widget(rw["name"])
        uid = rw["uid"]
  
        widget.uid(uid)
      end
    rescue JSON::ParserError => pe
      raise Error::MissingConfiguration
    end 
  
    success = true if response.code == 200
  rescue RestClient::Exception => e
    message = ""
  
    begin    
      message = JSON.parse(e.response.body)["message"] if e.response.body
    rescue JSON::ParserError => pe
      # ignore
    end 
  
    raise Error::BadRequest, message if e.http_code == 400 
    raise Error::NotFound, message if e.http_code == 404 
    raise Error::ThrottleLimit, message if e.http_code == 503
    raise e
  end
  
  success
end