Class: ChinoBaseAPI

Inherits:
CheckValues show all
Defined in:
lib/chino_ruby.rb

Overview

Base class of every resource class. It contains the functions for the GET, POST, PUT, PATCH and DELETE requests

Instance Method Summary collapse

Methods inherited from CheckValues

#check_boolean, #check_int, #check_json, #check_string

Constructor Details

#initialize(customer_id, customer_key, host_url) ⇒ ChinoBaseAPI

Used to inizialize a customer or a user. If you want to authenticate a user, simply pass “” as the customer_id



108
109
110
111
112
113
114
115
# File 'lib/chino_ruby.rb', line 108

def initialize(customer_id, customer_key, host_url)
    if customer_id == ""
        @customer_id = "Bearer "
    end
    @customer_id = customer_id
    @customer_key = customer_key
    @host_url = host_url
end

Instance Method Details

#delete_resource(path, force) ⇒ Object

base function to DELETE a resource



235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
# File 'lib/chino_ruby.rb', line 235

def delete_resource(path, force)
    check_string(path)
    check_boolean(force)
    if force
        uri = return_uri(path+"?force=true")
    else
        uri = return_uri(path)
    end
    req = Net::HTTP::Delete.new(uri)
    if @customer_id == "Bearer "
        req.add_field("Authorization", @customer_id+@customer_key)
    else
        req.basic_auth @customer_id, @customer_key
    end
    res = Net::HTTP.start(uri.hostname, uri.port, :use_ssl => true) {|http|
        http.request(req)
    }
    JSON.parse(parse_response(res).to_json)['result']
end

#get_resource(path, limit = nil, offset = nil, full_document = nil) ⇒ Object

base function to GET a resource with the proper params if specified



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/chino_ruby.rb', line 133

def get_resource(path, limit=nil, offset=nil, full_document=nil)
    check_string(path)
    if (limit==nil) && (offset==nil)
        uri = return_uri(path)
    elsif full_document==nil
        uri = return_uri(path, limit, offset)
    else
        uri = return_uri(path, limit, offset, full_document)
    end
    req = Net::HTTP::Get.new(uri.path)
    if @customer_id == "Bearer "
        req.add_field("Authorization", @customer_id+@customer_key)
    else
        req.basic_auth @customer_id, @customer_key
    end
    res = Net::HTTP.start(uri.hostname, uri.port, :use_ssl => true) {|http|
        http.request(req)
    }
    parse_response(res)['data']
end

#parse_response(response, raw = false) ⇒ Object

base function to parse the response and raise “chino” errors if problems occurred



256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
# File 'lib/chino_ruby.rb', line 256

def parse_response(response, raw=false)
    if response.is_a?(Net::HTTPServerError)
        raise ChinoError.new("Chino Server Error: #{response} - #{response.body}", response)
        elsif response.is_a?(Net::HTTPUnauthorized)
        d = JSON.parse(response.body)
        raise ChinoAuthError.new("Chino authentication error: #{d['message']}", response)
        elsif !response.is_a?(Net::HTTPSuccess)
        begin
            d = JSON.parse(response.body)
            rescue
            raise ChinoError.new("Chino Server Error: body=#{response.body}", response)
        end
        if d['user_error'] and d['error']
            raise ChinoError.new(d['error'], response, d['user_error'])  #user_error is translated
            elsif d['error']
            raise ChinoError.new(d['error'], response)
            else
            raise ChinoError.new(response.body, response)
        end
    end
    
    return response.body if raw
    
    begin
        return JSON.parse(response.body)
        rescue JSON::ParserError
        raise ChinoError.new("Unable to parse JSON response: #{response.body}", response)
    end
end

#patch_resource(path, data) ⇒ Object

base function to PATCH a resource



218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
# File 'lib/chino_ruby.rb', line 218

def patch_resource(path, data)
    check_string(path)
    uri = return_uri(path)
    req = Net::HTTP::Patch.new(uri.path)
    if @customer_id == "Bearer "
        req.add_field("Authorization", @customer_id+@customer_key)
    else
        req.basic_auth @customer_id, @customer_key
    end
    req.body = data
    res = Net::HTTP.start(uri.hostname, uri.port, :use_ssl => true) {|http|
        http.request(req)
    }
    parse_response(res)['data']
end

#post_resource(path, data = nil, limit = nil, offset = nil, full_document = nil) ⇒ Object

base function to POST a resource with the proper params if specified



155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/chino_ruby.rb', line 155

def post_resource(path, data=nil, limit=nil, offset=nil, full_document=nil)
    check_string(path)
    if (limit==nil) && (offset==nil)
        uri = return_uri(path)
    elsif full_document==nil
        uri = return_uri(path, limit, offset)
    else
        uri = return_uri(path, limit, offset, full_document)
    end
    req = Net::HTTP::Post.new(uri.path)
    if @customer_id == "Bearer "
        req.add_field("Authorization", @customer_id+@customer_key)
    else
        req.basic_auth @customer_id, @customer_key
    end
    if data!=nil
        req.body = data
    end
    res = Net::HTTP.start(uri.hostname, uri.port, :use_ssl => true) {|http|
        http.request(req)
    }
    if data!=nil
        parse_response(res)['data']
    else
        JSON.parse(parse_response(res).to_json)['result']
    end
end

#post_resource_with_string_result(path, data) ⇒ Object

base function to POST a resource with string result



184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/chino_ruby.rb', line 184

def post_resource_with_string_result(path, data)
    check_string(path)
    uri = return_uri(path)
    req = Net::HTTP::Post.new(uri.path)
    if @customer_id == "Bearer "
        req.add_field("Authorization", @customer_id+@customer_key)
    else
        req.basic_auth @customer_id, @customer_key
    end
    req.body = data
    res = Net::HTTP.start(uri.hostname, uri.port, :use_ssl => true) {|http|
        http.request(req)
    }
    JSON.parse(parse_response(res).to_json)['result']
end

#put_resource(path, data) ⇒ Object

base function to PUT a resource



201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'lib/chino_ruby.rb', line 201

def put_resource(path, data)
    check_string(path)
    uri = return_uri(path)
    req = Net::HTTP::Put.new(uri.path)
    if @customer_id == "Bearer "
        req.add_field("Authorization", @customer_id+@customer_key)
    else
        req.basic_auth @customer_id, @customer_key
    end
    req.body = data
    res = Net::HTTP.start(uri.hostname, uri.port, :use_ssl => true) {|http|
        http.request(req)
    }
    parse_response(res)['data']
end

#return_uri(path, limit = nil, offset = nil, full_document = nil) ⇒ Object

returns the uri with the proper params if specified



118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/chino_ruby.rb', line 118

def return_uri(path, limit=nil, offset=nil, full_document=nil)
    uri = URI(@host_url+path)
    if limit!=nil && offset!=nil
        if full_document!=nil
            params = { :"full_document" => true, :"limit" => limit, :"offset" => offset}
            uri.query = URI.encode_www_form(params)
        else
            params = { "limit" => limit, :"offset" => offset}
            uri.query = URI.encode_www_form(params)
        end
    end
    uri
end