Class: Zendesk::Main

Inherits:
Object
  • Object
show all
Includes:
Attachment, Entry, Forum, Group, Organization, Search, Tag, Ticket, User
Defined in:
lib/zendesk/main.rb

Defined Under Namespace

Classes: Response

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Search

#search

Methods included from Entry

#create_entrie, #delete_entrie, #get_entries, #get_entry, #update_entrie

Methods included from Forum

#create_forum, #delete_forum, #get_forum, #get_forums, #update_forum

Methods included from Tag

#get_tags

Methods included from Ticket

#create_ticket, #delete_ticket, #get_ticket, #get_tickets, #update_ticket

Methods included from Group

#create_group, #delete_group, #get_group, #get_groups, #update_group

Methods included from Organization

#create_organization, #delete_organization, #get_organization, #get_organizations, #update_organization

Methods included from User

#create_user, #delete_user, #get_user, #get_users, #update_user

Constructor Details

#initialize(account, username, password, options = {}) ⇒ Main

Returns a new instance of Main.



6
7
8
9
10
11
12
13
14
15
16
# File 'lib/zendesk/main.rb', line 6

def initialize(, username, password, options = {})
  @account = 
  @username = username
  @password = password
  @options = options
  if options[:format] && ['xml', 'json'].any?{|f| f == options[:format]}
    @format = options[:format]
  else
    @format = 'xml'
  end
end

Instance Attribute Details

#formatObject

Returns the value of attribute format.



3
4
5
# File 'lib/zendesk/main.rb', line 3

def format
  @format
end

#main_urlObject

Returns the value of attribute main_url.



3
4
5
# File 'lib/zendesk/main.rb', line 3

def main_url
  @main_url
end

#responseObject (readonly)

Returns the value of attribute response.



4
5
6
# File 'lib/zendesk/main.rb', line 4

def response
  @response
end

#response_rawObject (readonly)

Returns the value of attribute response_raw.



4
5
6
# File 'lib/zendesk/main.rb', line 4

def response_raw
  @response_raw
end

Class Method Details

.to_xml(function_name, input) ⇒ Object



24
25
26
27
28
29
30
# File 'lib/zendesk/main.rb', line 24

def self.to_xml(function_name, input)
  if input.is_a?(String)
    input
  else
    input.to_xml({:root => function_name})
  end
end

Instance Method Details

#make_request(end_url, body = {}) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/zendesk/main.rb', line 53

def make_request(end_url, body = {})
  curl = Curl::Easy.new(main_url + end_url + ".#{@format}")
  curl.userpwd = "#{@username}:#{@password}"
  if body.empty? or body[:list]
    curl.url = curl.url + params_list(body[:list]) if body[:list]
    curl.perform
  elsif body[:create]
    curl.headers = "Content-Type: application/xml"
    curl.http_post(string_body(body))
  elsif body[:update]
    # PUT seems badly broken, at least I can't get it to work without always
    # raising an exception about rewinding the data stream
    # curl.http_put(final_body)
    curl.headers = { "Content-Type" => "application/xml", "X-Http-Method-Override" => "put" }    
    curl.http_post(string_body(body))
  elsif body[:destroy]
    curl.http_delete
  end

  if curl.body_str == "<error>Couldn't authenticate you</error>"
    return "string" #raise CouldNotAuthenticateYou 
  end
  Response.new(curl)
end

#params_list(list) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
# File 'lib/zendesk/main.rb', line 33

def params_list(list)
  params = "?" + list.map do |k, v|
    if v.is_a?(Array)
      v.map do |val|
        "#{k}[]=#{val}"
      end.join("&")
    else
      "#{k}=#{v}"
    end
  end.join("&")
end

#string_body(body) ⇒ Object



45
46
47
48
49
50
51
# File 'lib/zendesk/main.rb', line 45

def string_body(body)
  if body.values.first.is_a?(Hash)
    body.values.first.to_xml.strip
  elsif body.values.first.is_a?(String)
    body.values.first
  end
end