Class: Mints::Client

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host, api_key, scope = nil, session_token = nil) ⇒ Client

Returns a new instance of Client.



12
13
14
15
16
17
# File 'lib/client.rb', line 12

def initialize(host, api_key, scope = nil, session_token = nil)
    @host = host
    @api_key = api_key
    @session_token = session_token
    self.set_scope(scope)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object



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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/client.rb', line 49

def method_missing(name, *args, &block)     
  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'].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_keyObject (readonly)

Returns the value of attribute api_key.



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

def api_key
  @api_key
end

#base_urlObject (readonly)

Returns the value of attribute base_url.



9
10
11
# File 'lib/client.rb', line 9

def base_url
  @base_url
end

#hostObject (readonly)

Returns the value of attribute host.



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

def host
  @host
end

#scopeObject (readonly)

Returns the value of attribute scope.



8
9
10
# File 'lib/client.rb', line 8

def scope
  @scope
end

#session_tokenObject

Returns the value of attribute session_token.



10
11
12
# File 'lib/client.rb', line 10

def session_token
  @session_token
end

Instance Method Details

#contact_get(url) ⇒ Object

Start contact context



161
162
163
164
165
166
167
168
# File 'lib/client.rb', line 161

def contact_get(url)
  headers = {
    "ApiKey" => @api_key,
    "Accept" => "application/json"
  }
  headers["Authorization"] = "Bearer #{@session_token}" if @session_token
  return self.http_get(url, headers)
end

#contact_post(url, data) ⇒ Object



170
171
172
173
174
175
176
177
# File 'lib/client.rb', line 170

def contact_post(url, data)
  headers = {
    "ApiKey" => @api_key,
    "Accept" => "application/json"
  }
  headers["Authorization"] = "Bearer #{@session_token}" if @session_token
  return self.http_post(url, headers, data)
end

#contact_put(url, data) ⇒ Object



179
180
181
182
183
184
185
186
# File 'lib/client.rb', line 179

def contact_put(url, data)
  headers = {
    "ApiKey" => @api_key,
    "Accept" => "application/json"
  }
  headers["Authorization"] = "Bearer #{@session_token}" if @session_token
  return self.http_post(url, headers, data)
end

#get_url(route, object, uri, slug = nil) ⇒ Object



114
115
116
117
118
119
120
# File 'lib/client.rb', line 114

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_get(url, headers = nil) ⇒ Object

HTTTP CLIENTS ###### Simple HTTP GET



146
147
148
# File 'lib/client.rb', line 146

def http_get(url, headers = nil)
  return headers ? HTTParty.get(url, :headers => headers) : HTTParty.get(url)
end

#http_post(url, headers = nil, data = nil) ⇒ Object

Simple HTTP POST



151
152
153
# File 'lib/client.rb', line 151

def http_post(url, headers = nil, data = nil)
  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



156
157
158
# File 'lib/client.rb', line 156

def http_put(url, headers = nil, data = nil)
  return headers ? HTTParty.put(url, :headers=> headers, :body => data) : HTTParty.put(url, :body => data)
end

#public_get(url, headers = nil) ⇒ Object

End User Context



218
219
220
221
222
223
224
225
226
# File 'lib/client.rb', line 218

def public_get(url, headers = nil)
  h = {"Accept" => "application/json", "Contet-Type" => "application/json", "ApiKey" => @api_key}
  if headers
    headers.each do |k,v|
      h[k] = v
    end
  end
  self.http_get(url, h)
end

#public_post(url, headers = nil, data) ⇒ Object



228
229
230
231
232
233
234
235
236
# File 'lib/client.rb', line 228

def public_post(url, headers = nil, data)
  h = {"Accept" => "application/json", "Contet-Type" => "application/json", "ApiKey" => @api_key}
  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) ⇒ Object



238
239
240
241
242
243
244
245
246
# File 'lib/client.rb', line 238

def public_put(url, headers = nil, data)
  h = {"Accept" => "application/json", "Contet-Type" => "application/json", "ApiKey" => @api_key}
  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) ⇒ Object



19
20
21
22
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
# File 'lib/client.rb', line 19

def raw(action, url, options = nil, data = nil, base_url = nil)
  base_url = @base_url if !base_url        
  
  if (options && options.class == Hash)
    uri = Addressable::URI.new
    uri.query_values = options
  else
    uri = ""
  end

  full_url = "#{@host}#{base_url}#{url}#{uri}"
  if action === 'get'          
    response = self.send("#{@scope}_#{action}", "#{full_url}")
  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)
  end
  if (response.response.code == "404")
    raise 'NotFoundError'
  end
  parsed_response = JSON.parse(response.body) 
  if parsed_response.key?('data')
    return parsed_response['data']
  end
  return parsed_response
end

#replacementsObject



122
123
124
125
126
127
128
# File 'lib/client.rb', line 122

def replacements
  return [
      {old_value: '_', new_value: '-'},
      {old_value: 'people', new_value: 'crm'},
      {old_value: 'store', new_value: 'ecommerce'}
  ]
end

#set_scope(scope) ⇒ Object



130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/client.rb', line 130

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_get(url) ⇒ Object

Start User context



189
190
191
192
193
194
195
196
# File 'lib/client.rb', line 189

def user_get(url)
  headers = {
    "ApiKey" => @api_key,
    "Accept" => "application/json"
  }
  headers["Authorization"] = "Bearer #{@session_token}" if @session_token
  return self.http_get(url, headers)
end

#user_post(url, data) ⇒ Object



198
199
200
201
202
203
204
205
# File 'lib/client.rb', line 198

def user_post(url, data)
  headers = {
    "ApiKey" => @api_key,
    "Accept" => "application/json"
  }
  headers["Authorization"] = "Bearer #{@session_token}" if @session_token
  return self.http_post(url, headers, data)
end

#user_put(url, data) ⇒ Object



207
208
209
210
211
212
213
214
215
# File 'lib/client.rb', line 207

def user_put(url, data)
  headers = {
    "ApiKey" => @api_key,
    "Accept" => "application/json",
    "Contet-Type" => "application/json"
  }
  headers["Authorization"] = "Bearer #{@session_token}" if @session_token
  return self.http_put(url, headers, data)
end