module Gimizen

Constants

VERSION

Attributes

api_token[RW]
domain[RW]
email_address[RW]

Public Class Methods

configure(&blk) click to toggle source
# File lib/gimizen.rb, line 10
def configure(&blk); class_eval(&blk); end
create_ticket(hash, full_response=false) click to toggle source
# File lib/gimizen.rb, line 13
def self.create_ticket(hash, full_response=false)
  # Format the subject and comment
  subject = format_subject(hash[:subject])
  comment = format_comment(hash[:comment])
  # Format json data
  json_data = format_json_data(hash)
  # Perform request
  return post_to_zendesk("#{@domain}/tickets.json", json_data, full_response)
end
format_comment(comment = nil) click to toggle source
# File lib/gimizen.rb, line 28
def self.format_comment(comment = nil)
  raise Exception.new('No comment provided') if comment.nil? || comment.empty?
  comment
end
format_json_data(hash) click to toggle source
# File lib/gimizen.rb, line 33
def self.format_json_data(hash)
  json_data = {
    ticket: {
      subject: hash[:subject],
      comment: {
        body: hash[:comment]
      }
    }
  }.to_json
end
format_subject(subject = nil) click to toggle source
# File lib/gimizen.rb, line 23
def self.format_subject(subject = nil)
  raise Exception.new('No subject provided') if subject.nil? || subject.empty?
  subject
end
handle_response(response) click to toggle source
# File lib/gimizen.rb, line 56
def self.handle_response(response)
  # Format Zendesk response
  response = JSON.parse(response).symbolize_keys(true)
  response = OpenStruct.new response[:ticket]
  response
end
post_to_zendesk(url, data, full_response=false) click to toggle source
# File lib/gimizen.rb, line 44
def self.post_to_zendesk(url, data, full_response=false)
  # Post method to Zendesk
  response = HTTParty.post(
    url, 
    body: data,
    basic_auth: {username: "#{@email_address}/token", password: @api_token},
    headers: {'Content-Type' => 'application/json'}
  )
  # Return full response or succes message
  full_response ? handle_response(response.body) : succes_message(response.code)
end
succes_message(response_code) click to toggle source
# File lib/gimizen.rb, line 63
def self.succes_message(response_code)
  # Format status message
  status_message = {created: true}
  # Change hash if tickets was nit created
  status_message[:created] = false if response_code != 201
  # Return status message
  OpenStruct.new status_message
end
version_string() click to toggle source
# File lib/gimizen.rb, line 72
def self.version_string
  "Gimizen version #{Gimizen::VERSION}"
end