Class: HotDogPrincess::Client

Inherits:
Object
  • Object
show all
Includes:
Customers, Slas, Tickets
Defined in:
lib/hotdogprincess/client.rb,
lib/hotdogprincess/client/slas.rb,
lib/hotdogprincess/client/tickets.rb,
lib/hotdogprincess/client/customers.rb

Defined Under Namespace

Modules: Customers, Slas, Tickets

Instance Method Summary collapse

Methods included from Slas

#fetch_slas, #parse_sla, #sla, #sla_count, #slas, #slas_raw

Methods included from Tickets

#create_ticket, #fetch_tickets, #parse_ticket, #ticket, #ticket_count, #tickets, #tickets_raw, #update_ticket

Methods included from Customers

#create_customer, #customer, #customer_count, #customers, #customers_raw, #fetch_customers, #find_customer_by_email, #parse_customer, #update_customer

Constructor Details

#initialize(options = {}) ⇒ Client

Returns a new instance of Client.



16
17
18
19
20
21
22
# File 'lib/hotdogprincess/client.rb', line 16

def initialize(options = {})
  @host = options[:host]
  @token = options[:token]
  @account_id = options[:account_id]
  @department_id = options[:department_id]
  @output_format = :json
end

Instance Method Details

#account_idObject



98
99
100
# File 'lib/hotdogprincess/client.rb', line 98

def 
  @account_id
end

#account_id=(value) ⇒ Object



94
95
96
# File 'lib/hotdogprincess/client.rb', line 94

def (value)
  @account_id = value
end

#clean_response(response) ⇒ Object



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/hotdogprincess/client.rb', line 114

def clean_response(response)
  return nil  unless response and response.class == String
  return nil  unless response.length >= 3
  begin
    # "\xEF", "\xBB", "\xBF"
    if response[0].ord == 239 and response[1].ord == 187 and response[2].ord == 191
      response_hash = JSON.parse response[3..-1]
    else
      response_hash = JSON.parse response
    end
  rescue
    response_hash = nil
  end
  response_hash
end

#delete(path, options = {}) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/hotdogprincess/client.rb', line 64

def delete(path, options = {})
  url = "https://#{@host}/api/v1/#{@account_id}/#{@department_id}/#{path.to_s}"
  options = {
    _output_: @output_format,
    _token_: @token
  }.merge(options)
  begin
    @last_response = RestClient.delete url, { params: options }
  rescue => e
    raise HotDogPrincess::Error.new(e.response, self)
  end
  @last_response
end

#department_idObject



106
107
108
# File 'lib/hotdogprincess/client.rb', line 106

def department_id
  @department_id
end

#department_id=(value) ⇒ Object



102
103
104
# File 'lib/hotdogprincess/client.rb', line 102

def department_id=(value)
  @department_id = value
end

#get(path, options = {}) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/hotdogprincess/client.rb', line 24

def get(path, options = {})
  url = "https://#{@host}/api/v1/#{@account_id}/#{@department_id}/#{path.to_s}"
  options = {
    _output_: @output_format,
    _token_: @token
  }.merge(options)
  begin
    @last_response = RestClient.get url, { params: options }
  rescue => e
    raise HotDogPrincess::Error.new(e.response, self)
  end
  clean_response @last_response
end

#hostObject



82
83
84
# File 'lib/hotdogprincess/client.rb', line 82

def host
  @host
end

#host=(value) ⇒ Object



78
79
80
# File 'lib/hotdogprincess/client.rb', line 78

def host=(value)
  @host = value
end

#last_responseObject



110
111
112
# File 'lib/hotdogprincess/client.rb', line 110

def last_response
  @last_response if defined? @last_response
end

#post(path, body, options = {}) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/hotdogprincess/client.rb', line 38

def post(path, body, options = {})
  url = "https://#{@host}/api/v1/#{@account_id}/#{@department_id}/#{path.to_s}?_token_=#{@token}"
  options = {
    content_type: :xml
  }.merge(options)
  begin
    @last_response = RestClient.post url, body, options
  rescue => e
    raise HotDogPrincess::Error.new(e.response, self)
  end
  @last_response
end

#put(path, body, options = {}) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/hotdogprincess/client.rb', line 51

def put(path, body, options = {})
  url = "https://#{@host}/api/v1/#{@account_id}/#{@department_id}/#{path.to_s}?_token_=#{@token}&_enforceRequiredFields_=false"
  options = {
    content_type: :xml
  }.merge(options)
  begin
    @last_response = RestClient.put url, body, options
  rescue => e
    raise HotDogPrincess::Error.new(e.response, self)
  end
  @last_response
end

#schema(object) ⇒ Object



176
177
178
179
180
# File 'lib/hotdogprincess/client.rb', line 176

def schema(object)
  schema = schema_raw(object)
  schema_hash = schema_parse_hash(schema[object])
  schema_hash
end

#schema_json(object) ⇒ Object



182
183
184
185
186
# File 'lib/hotdogprincess/client.rb', line 182

def schema_json(object)
  schema = schema_raw(object)
  schema_hash = schema_parse_hash(schema[object])
  JSON.generate(schema_hash)
end

#schema_parse(input) ⇒ Object



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

def schema_parse(input)
  output = input
  if input.class == Array
    output = schema_parse_array(input)
  elsif input.class == Hash
    output = schema_parse_hash(input)
  elsif input.class == String
    output = schema_parse_string(input)
  end
  output
end

#schema_parse_array(input_array) ⇒ Object



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

def schema_parse_array(input_array)
  output = []
  input_array.each do |item|
    if item.class == Array
      output.push  schema_parse_array(item)
    elsif item.class == Hash
      output.push  schema_parse_hash(item)
    elsif value.class == String
      output.push schema_parse_string(item)
    else
      output.push  item
    end
  end
  output
end

#schema_parse_hash(input_hash) ⇒ Object



216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
# File 'lib/hotdogprincess/client.rb', line 216

def schema_parse_hash(input_hash)
  output = {}
  input_hash.each do |key, value|
    if value.class == Array
      output[key.to_s.name_to_key] = schema_parse_array(value)
    elsif value.class == Hash
      output[key.to_s.name_to_key] = schema_parse_hash(value)
    elsif value.class == String
      output[key.to_s.name_to_key] = schema_parse_string(value)
    else
      output[key.to_s.name_to_key] = value
    end
  end
  output
end

#schema_parse_string(input_string) ⇒ Object



232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
# File 'lib/hotdogprincess/client.rb', line 232

def schema_parse_string(input_string)
  case input_string
  when 'true'
    true
  when 'false'
    false
  else
    # Check for Integers / Floats
    if /^-?(?:[0-9]+|[0-9]*\.[0-9]+)$/ =~ input_string
      if input_string.to_i == input_string.to_f
        input_string.to_i
      else
        input_string.to_f
      end
    else
      input_string
    end
  end
end

#schema_raw(object) ⇒ Object



172
173
174
# File 'lib/hotdogprincess/client.rb', line 172

def schema_raw(object)
  get "#{object.to_s}/schema"
end

#status(object) ⇒ Object



134
135
136
137
138
139
140
141
142
143
# File 'lib/hotdogprincess/client.rb', line 134

def status(object)
  status = status_raw(object)
  if status['Entities'] and status['Entities']['Status']
    status_hash = schema_parse(status['Entities']['Status'])
  elsif status['entities'] and status['entities']['Status']
    # With _output_=json the container is lowercase for some reason.
    status_hash = schema_parse(status['entities']['Status'])
  end
  status_hash
end

#status_json(object) ⇒ Object



145
146
147
148
149
150
151
152
153
154
# File 'lib/hotdogprincess/client.rb', line 145

def status_json(object)
  status = status_raw(object)
  if status['Entities'] and status['Entities']['Status']
    status_hash = schema_parse(status['Entities']['Status'])
  elsif status['entities'] and status['entities']['Status']
    # With _output_=json the container is lowercase for some reason.
    status_hash = schema_parse(status['entities']['Status'])
  end
  JSON.generate(status_hash)
end

#status_raw(object) ⇒ Object



130
131
132
# File 'lib/hotdogprincess/client.rb', line 130

def status_raw(object)
  get "#{object.to_s}/status"
end

#tokenObject



90
91
92
# File 'lib/hotdogprincess/client.rb', line 90

def token
  @token
end

#token=(value) ⇒ Object



86
87
88
# File 'lib/hotdogprincess/client.rb', line 86

def token=(value)
  @token = value
end

#view(object) ⇒ Object



160
161
162
163
164
# File 'lib/hotdogprincess/client.rb', line 160

def view(object)
  views = view_raw(object)
  views_hash = schema_parse(views['Entities']['View'])
  views_hash
end

#view_json(object) ⇒ Object



166
167
168
169
170
# File 'lib/hotdogprincess/client.rb', line 166

def view_json(object)
  views = view_raw(object)
  views_hash = schema_parse(views['Entities']['View'])
  JSON.generate(views_hash)
end

#view_raw(object) ⇒ Object



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

def view_raw(object)
  get "#{object.to_s}/view"
end