Class: RestClientWrapper

Inherits:
RestClientHelper show all
Includes:
Singleton
Defined in:
lib/cloudscale/rest/rest_client.rb

Overview

Author:

  • Johannes Hiemer, cloudscale.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from RestClientHelper

load_entity_id, load_entity_ref, #load_token, #removeLinks, #uri, #uriWithId, #uri_with_params

Constructor Details

#initializeRestClientWrapper

Returns a new instance of RestClientWrapper.



25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/cloudscale/rest/rest_client.rb', line 25

def initialize
  if (endpoint == nil && key == nil && token == nil)
    hash = load_hash()
  end
  @endpoint = hash["endpoint"]
  @key = hash["key"]
  @token = load_token("token")
  @header = { 
    :key => key, 
    :token => token, 
    'Content-Type' => 'application/json',     
  }
end

Instance Attribute Details

#endpointObject

Returns the value of attribute endpoint.



15
16
17
# File 'lib/cloudscale/rest/rest_client.rb', line 15

def endpoint
  @endpoint
end

#headerObject

Returns the value of attribute header.



15
16
17
# File 'lib/cloudscale/rest/rest_client.rb', line 15

def header
  @header
end

#keyObject

Returns the value of attribute key.



15
16
17
# File 'lib/cloudscale/rest/rest_client.rb', line 15

def key
  @key
end

#tokenObject

Returns the value of attribute token.



15
16
17
# File 'lib/cloudscale/rest/rest_client.rb', line 15

def token
  @token
end

Instance Method Details

#customDelete(entity, method, params) ⇒ Object



161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/cloudscale/rest/rest_client.rb', line 161

def customDelete(entity, method, params)
  id = id.to_s.gsub('{?projection}','')
  
  query = String::new(uri(entity) + '/' + method + '?') 
     
  params.each { | key, value|
    query += key.to_s + '=' + value.to_s + '&'  
  }
  RestClient.delete(query, header) { | response, request, result |
    case response.code
      when 200
      else
        puts response.code
    end
  }
end

#delete(entity, id) ⇒ Object



149
150
151
152
153
154
155
156
157
158
159
# File 'lib/cloudscale/rest/rest_client.rb', line 149

def delete(entity, id)
  id = id.to_s.gsub('{?projection}','')
  
  RestClient.delete(uriWithId(entity, id), header) { | response, request, result |
    case response.code
      when 200, 204
      else
        puts response.code
    end
  }
end

#get(entity, id) ⇒ Object



49
50
51
52
# File 'lib/cloudscale/rest/rest_client.rb', line 49

def get(entity, id)
  uri = uriWithId(entity, id)
  load(uri)
end

#list(entity) ⇒ Object



44
45
46
47
# File 'lib/cloudscale/rest/rest_client.rb', line 44

def list(entity)
  uri = uri(entity)
  load(uri)
end

#list_with_params(entity, params) ⇒ Object



39
40
41
42
# File 'lib/cloudscale/rest/rest_client.rb', line 39

def list_with_params(entity, params)
  uri = uri_with_params(entity, params)
  load(uri)
end

#load(uri) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/cloudscale/rest/rest_client.rb', line 54

def load(uri)
  RestClient.get(uri, header) { | response, request, result | 
    case response.code
    when 200
        return JSON.parse(response.body)
      else
        puts request.to_yaml
        puts response.code
        puts response.body
    end
  }
end

#load_hashObject



17
18
19
20
21
22
23
# File 'lib/cloudscale/rest/rest_client.rb', line 17

def load_hash()
  begin 
    YAML.load(File.read("#{File.dirname(__FILE__)}/../store/rest/settings.yml"))
  rescue
    return nil
  end
end

#post(entity, value) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/cloudscale/rest/rest_client.rb', line 108

def post(entity, value)
  
  if (value.is_a?(Object) || value.is_a?(Hash))
    hash = value.to_dh
  end
  
  RestClient.post(uri(entity), hash.to_json, header) { | response, request, result |
    case response.code
    when 200, 201
        if (response.body != nil && response.body.length > 2)
          return JSON.parse(response.body)
        end  
      else
        puts request.to_yaml
        puts response.code
        puts response.body
    end
  }
end

#put(entity, id, value) ⇒ Object



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/cloudscale/rest/rest_client.rb', line 128

def put(entity, id, value)
  id = id.to_s.gsub('{?projection}','')
  
  if (value.is_a?(Object) || value.is_a?(Hash))
    hash = value.to_dh             
  end

  RestClient.put(uriWithId(entity, id), hash.to_json, header) { | response, request, result |
    case response.code
      when 200, 204
        if (response.body != nil && response.body.length > 2)
          return JSON.parse(response.body)
        end        
      else
        puts request.to_yaml
        puts response.code
        puts response.body
    end
  }
end

#searchAny(entity, method, params) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/cloudscale/rest/rest_client.rb', line 89

def searchAny(entity, method, params)
  searchQuery = String::new(uri(entity) + '/search/' + method + '?') 
 
  params.each { | key, value|
    searchQuery += key.to_s + '=' + value.to_s + '&'  
  }
  
  RestClient.get(searchQuery, header) { | response, request, result | 
    case response.code
      when 200
        return JSON.parse(response.body)          
      else
        puts request.to_yaml
        puts response.code
        puts response.body
    end
  }
end

#searchOne(entity, method, params) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/cloudscale/rest/rest_client.rb', line 67

def searchOne(entity, method, params)
  searchQuery = String::new(uri(entity) + '/search/' + method + '?') 
 
  params.each { | key, value|
    searchQuery += key.to_s + '=' + value.to_s + '&'  
  }
  
  RestClient.get(searchQuery, header) { | response, request, result | 
    case response.code
      when 200
        responseBody = JSON.parse(response.body)
        if (responseBody["content"].length == 1)
          return responseBody["content"][0]
        end
      else
        puts request.to_yaml
        puts response.code
        puts response.body
    end
  }
end