Class: Mints::Client
- Inherits:
-
Object
- Object
- Mints::Client
- Defined in:
- lib/client.rb
Instance Attribute Summary collapse
-
#api_key ⇒ Object
readonly
Returns the value of attribute api_key.
-
#base_url ⇒ Object
readonly
Returns the value of attribute base_url.
-
#contact_token_id ⇒ Object
Returns the value of attribute contact_token_id.
-
#host ⇒ Object
readonly
Returns the value of attribute host.
-
#scope ⇒ Object
readonly
Returns the value of attribute scope.
-
#session_token ⇒ Object
Returns the value of attribute session_token.
Instance Method Summary collapse
-
#contact_get(url, compatibility_options) ⇒ Object
Start contact context.
- #contact_post(url, data, compatibility_options) ⇒ Object
- #contact_put(url, data, compatibility_options) ⇒ Object
- #get_url(route, object, uri, slug = nil) ⇒ Object
-
#http_delete(url, headers = nil, data = nil) ⇒ Object
Simple HTTP DELETE.
-
#http_get(url, headers = nil) ⇒ Object
HTTTP CLIENTS ###### Simple HTTP GET.
-
#http_post(url, headers = nil, data = nil) ⇒ Object
Simple HTTP POST.
-
#http_put(url, headers = nil, data = nil) ⇒ Object
Simple HTTP PUT.
-
#initialize(host, api_key, scope = nil, session_token = nil, contact_token_id = nil, debug = false) ⇒ Client
constructor
A new instance of Client.
- #method_missing(name, *args, &block) ⇒ Object
-
#public_get(url, headers = nil, compatibility_options) ⇒ Object
End User Context.
- #public_post(url, headers = nil, data, compatibility_options) ⇒ Object
- #public_put(url, headers = nil, data, compatibility_options) ⇒ Object
- #raw(action, url, options = nil, data = nil, base_url = nil, compatibility_options = {}) ⇒ Object
- #replacements ⇒ Object
- #set_scope(scope) ⇒ Object
- #user_delete(url, data, compatibility_options) ⇒ Object
-
#user_get(url, compatibility_options) ⇒ Object
Start User context.
- #user_post(url, data, compatibility_options) ⇒ Object
- #user_put(url, data, compatibility_options) ⇒ Object
Constructor Details
#initialize(host, api_key, scope = nil, session_token = nil, contact_token_id = nil, debug = false) ⇒ Client
Returns a new instance of Client.
14 15 16 17 18 19 20 21 |
# File 'lib/client.rb', line 14 def initialize(host, api_key, scope = nil, session_token = nil, contact_token_id = nil, debug = false) @host = host @api_key = api_key @session_token = session_token @contact_token_id = contact_token_id @debug = debug self.set_scope(scope) end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args, &block) ⇒ Object
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 |
# File 'lib/client.rb', line 90 def method_missing(name, *args, &block) puts name name.to_s.include?("__") ? separator = "__" : separator = "_" # split the name to identify their elements name_spplited = name.to_s.split(separator) # count the elments name_len = name_spplited.size # the action always be the first element action = name_spplited.first raise 'NoActionError' unless ['get', 'create', 'post', 'update', 'put', 'delete', 'destroy'].include?(action) # the object always be the last element object = separator == "__" ? name_spplited.last.gsub("_","-") : name_spplited.last # get intermediate url elements route_array = [] (name_len-1).times do |n| if n == 0 or n == name_len-1 next end n = name_spplited[n] self.replacements().each do |object| n = n.gsub(object[:old_value], object[:new_value]) end route_array.push n end route = route_array.join("/") slug = nil uri = Addressable::URI.new if action == "get" if args.first.class == Hash uri.query_values = args.first elsif args.first.class == String or Integer slug = args.first uri.query_values = args[1] end url = self.get_url(route, object, uri, slug) response = self.send("#{@scope}_#{action}", url, ) elsif action == "post" or action == "create" if args[1].class == Hash uri.query_values = args[1] end url = self.get_url(route, object, uri, slug) action = 'post' data = args[0] response = self.send("#{@scope}_#{action}", url, {data: data}, ) elsif action == "put" or action == "update" if args.first.class == String or Integer slug = args.first uri.query_values = args[2] end url = self.get_url(route, object, uri, slug) action = 'put' id = args[0] data = args[1] response = self.send("#{@scope}_#{action}", "#{url}", {data: data}, ) end if response.response.code == "404" raise 'NotFoundError' elsif response.response.code == "500" raise 'InternalServerError' end return JSON.parse(response.body) end |
Instance Attribute Details
#api_key ⇒ Object (readonly)
Returns the value of attribute api_key.
8 9 10 |
# File 'lib/client.rb', line 8 def api_key @api_key end |
#base_url ⇒ Object (readonly)
Returns the value of attribute base_url.
10 11 12 |
# File 'lib/client.rb', line 10 def base_url @base_url end |
#contact_token_id ⇒ Object
Returns the value of attribute contact_token_id.
12 13 14 |
# File 'lib/client.rb', line 12 def contact_token_id @contact_token_id end |
#host ⇒ Object (readonly)
Returns the value of attribute host.
7 8 9 |
# File 'lib/client.rb', line 7 def host @host end |
#scope ⇒ Object (readonly)
Returns the value of attribute scope.
9 10 11 |
# File 'lib/client.rb', line 9 def scope @scope end |
#session_token ⇒ Object
Returns the value of attribute session_token.
11 12 13 |
# File 'lib/client.rb', line 11 def session_token @session_token end |
Instance Method Details
#contact_get(url, compatibility_options) ⇒ Object
Start contact context
242 243 244 245 246 247 248 249 250 251 |
# File 'lib/client.rb', line 242 def contact_get(url, ) headers = { "ApiKey" => @api_key, "Accept" => "application/json", "ContactToken" => @contact_token_id } headers["Content-Type"] = "application/json" unless ['no_content_type'] headers["Authorization"] = "Bearer #{@session_token}" if @session_token return self.http_get(url, headers) end |
#contact_post(url, data, compatibility_options) ⇒ Object
253 254 255 256 257 258 259 260 261 262 |
# File 'lib/client.rb', line 253 def contact_post(url, data, ) headers = { "ApiKey" => @api_key, "Accept" => "application/json", "ContactToken" => @contact_token_id } headers["Content-Type"] = "application/json" unless ['no_content_type'] headers["Authorization"] = "Bearer #{@session_token}" if @session_token return self.http_post(url, headers, data) end |
#contact_put(url, data, compatibility_options) ⇒ Object
264 265 266 267 268 269 270 271 272 273 |
# File 'lib/client.rb', line 264 def contact_put(url, data, ) headers = { "ApiKey" => @api_key, "Accept" => "application/json", "ContactToken" => @contact_token_id } headers["Content-Type"] = "application/json" unless ['no_content_type'] headers["Authorization"] = "Bearer #{@session_token}" if @session_token return self.http_put(url, headers, data) end |
#get_url(route, object, uri, slug = nil) ⇒ Object
156 157 158 159 160 161 162 |
# File 'lib/client.rb', line 156 def get_url(route, object, uri, slug = nil) if (slug) return "#{@host}#{@base_url}/#{route}/#{object}/#{slug}#{uri}" else return "#{@host}#{@base_url}/#{route}/#{object}#{uri}" end end |
#http_delete(url, headers = nil, data = nil) ⇒ Object
Simple HTTP DELETE
228 229 230 231 232 233 234 235 236 237 238 239 |
# File 'lib/client.rb', line 228 def http_delete(url, headers = nil, data = nil) if @debug puts "Url:" puts url puts "Headers:" puts headers puts "Data:" puts data puts "Method: delete" end return headers ? HTTParty.delete(url, :headers=> headers, :body => data) : HTTParty.delete(url, :body => data) end |
#http_get(url, headers = nil) ⇒ Object
HTTTP CLIENTS ###### Simple HTTP GET
188 189 190 191 192 193 194 195 196 197 |
# File 'lib/client.rb', line 188 def http_get(url, headers = nil) if @debug puts "Url:" puts url puts "Headers:" puts headers puts "Method: get" end return headers ? HTTParty.get(url, :headers => headers) : HTTParty.get(url) end |
#http_post(url, headers = nil, data = nil) ⇒ Object
Simple HTTP POST
200 201 202 203 204 205 206 207 208 209 210 211 |
# File 'lib/client.rb', line 200 def http_post(url, headers = nil, data = nil) if @debug puts "Url:" puts url puts "Headers:" puts headers puts "Data:" puts data puts "Method: post" end return headers ? HTTParty.post(url, :headers=> headers, :body => data) : HTTParty.post(url, :body => data) end |
#http_put(url, headers = nil, data = nil) ⇒ Object
Simple HTTP PUT
214 215 216 217 218 219 220 221 222 223 224 225 |
# File 'lib/client.rb', line 214 def http_put(url, headers = nil, data = nil) if @debug puts "Url:" puts url puts "Headers:" puts headers puts "Data:" puts data puts "Method: put" end return headers ? HTTParty.put(url, :headers=> headers, :body => data) : HTTParty.put(url, :body => data) end |
#public_get(url, headers = nil, compatibility_options) ⇒ Object
End User Context
317 318 319 320 321 322 323 324 325 326 327 328 329 330 |
# File 'lib/client.rb', line 317 def public_get(url, headers = nil, ) h = { "Accept" => "application/json", "ApiKey" => @api_key } h["Content-Type"] = "application/json" unless ['no_content_type'] h["ContactToken"] = @contact_token_id if @contact_token_id if headers headers.each do |k,v| h[k] = v end end self.http_get(url, h) end |
#public_post(url, headers = nil, data, compatibility_options) ⇒ Object
332 333 334 335 336 337 338 339 340 341 342 343 344 345 |
# File 'lib/client.rb', line 332 def public_post(url, headers = nil, data, ) h = { "Accept" => "application/json", "ApiKey" => @api_key } h["Content-Type"] = "application/json" unless ['no_content_type'] h["ContactToken"] = @session_token if @session_token if headers headers.each do |k,v| h[k] = v end end self.http_post(url, h, data) end |
#public_put(url, headers = nil, data, compatibility_options) ⇒ Object
347 348 349 350 351 352 353 354 355 356 357 358 359 360 |
# File 'lib/client.rb', line 347 def public_put(url, headers = nil, data, ) h = { "Accept" => "application/json", "ApiKey" => @api_key } h["Content-Type"] = "application/json" unless ['no_content_type'] h["ContactToken"] = @contact_token_id if @contact_token_id if headers headers.each do |k,v| h[k] = v end end self.http_put(url, h, data) end |
#raw(action, url, options = nil, data = nil, base_url = nil, compatibility_options = {}) ⇒ Object
23 24 25 26 27 28 29 30 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 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/client.rb', line 23 def raw(action, url, = nil, data = nil, base_url = nil, = {}) base_url = @base_url if !base_url uri = "" if ( && .class == Hash) if ([:jfilters] && [:jfilters].class == Hash) [:jfilters] = Base64.encode64(JSON.generate([:jfilters])) end uri = Addressable::URI.new uri.query_values = end full_url = "#{@host}#{base_url}#{url}#{uri}" response = nil if action === 'get' template = ERB.new File.new("#{Rails.root}/mints_config.yml.erb").read config = YAML.load template.result(binding) url_need_cache = false result_from_cache = false time = 0 if config['redis_cache']['use_cache'] config['redis_cache']['groups'].each do |group| group['urls'].each do |url| if full_url.match url time = group['time'] url_need_cache = true @redis_server = Redis.new(host: config['redis_cache']['redis_host'], port: config['redis_cache']['redis_port'] ? config['redis_cache']['redis_port'] : 6379, db: config['redis_cache']['redis_db'] ? config['redis_cache']['redis_db'] : 1) if @redis_server.get(full_url) response = @redis_server.get(full_url) result_from_cache = true else response = self.send("#{@scope}_#{action}", "#{full_url}", ) @redis_server.setex(full_url,time,response) end break end end break if url_need_cache end end if !url_need_cache response = self.send("#{@scope}_#{action}", "#{full_url}", ) end elsif action === 'create' or action === 'post' action = 'post' response = self.send("#{@scope}_#{action}", "#{full_url}", data, ) elsif action === 'put' or action === 'patch' or action ==='update' action = 'put' response = self.send("#{@scope}_#{action}", "#{full_url}", data, ) elsif action === 'delete' or action === 'destroy' action = 'delete' response = self.send("#{@scope}_#{action}", "#{full_url}", data, ) end if result_from_cache return parsed_response = JSON.parse(response) else if (response.response.code == "404") raise 'NotFoundError' end parsed_response = JSON.parse(response.body) return parsed_response end end |
#replacements ⇒ Object
164 165 166 167 168 169 170 |
# File 'lib/client.rb', line 164 def replacements return [ {old_value: '_', new_value: '-'}, {old_value: 'people', new_value: 'crm'}, {old_value: 'store', new_value: 'ecommerce'} ] end |
#set_scope(scope) ⇒ Object
172 173 174 175 176 177 178 179 180 181 182 183 184 |
# File 'lib/client.rb', line 172 def set_scope(scope) @scope = scope if scope == "public" @base_url = "/api/v1" elsif scope == "contact" @base_url = "/api/v1" elsif scope == "user" @base_url = "/api/user/v1" else @scope = "public" @base_url = "/api/v1" end end |
#user_delete(url, data, compatibility_options) ⇒ Object
306 307 308 309 310 311 312 313 314 |
# File 'lib/client.rb', line 306 def user_delete(url, data, ) headers = { "Accept" => "application/json", "ApiKey" => @api_key } headers["Content-Type"] = "application/json" unless ['no_content_type'] headers["Authorization"] = "Bearer #{@session_token}" if @session_token return self.http_delete(url, headers, data) end |
#user_get(url, compatibility_options) ⇒ Object
Start User context
276 277 278 279 280 281 282 283 284 |
# File 'lib/client.rb', line 276 def user_get(url, ) headers = { "Accept" => "application/json", "ApiKey" => @api_key } headers["Content-Type"] = "application/json" unless ['no_content_type'] headers["Authorization"] = "Bearer #{@session_token}" if @session_token return self.http_get(url, headers) end |
#user_post(url, data, compatibility_options) ⇒ Object
286 287 288 289 290 291 292 293 294 |
# File 'lib/client.rb', line 286 def user_post(url, data, ) headers = { "Accept" => "application/json", "ApiKey" => @api_key } headers["Content-Type"] = "application/json" unless ['no_content_type'] headers["Authorization"] = "Bearer #{@session_token}" if @session_token return self.http_post(url, headers, data) end |
#user_put(url, data, compatibility_options) ⇒ Object
296 297 298 299 300 301 302 303 304 |
# File 'lib/client.rb', line 296 def user_put(url, data, ) headers = { "Accept" => "application/json", "ApiKey" => @api_key } headers["Content-Type"] = "application/json" unless ['no_content_type'] headers["Authorization"] = "Bearer #{@session_token}" if @session_token return self.http_put(url, headers, data) end |