Class: Zendesk::Main

Inherits:
Object
  • Object
show all
Includes:
Attachment, Comment, Entry, Forum, Group, Organization, Search, Tag, Ticket, User, UserIdentity
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 UserIdentity

#user_add_email, #user_add_twitter, #user_delete_identity, #user_identities, #user_primary_identity

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



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

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 = {}, options = {}) ⇒ Object



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
# File 'lib/zendesk/main.rb', line 55

def make_request(end_url, body = {}, options = {})
  options.reverse_merge!({:on_behalf_of => nil})
  
  curl = Curl::Easy.new(main_url + end_url + ".#{@format}")
  curl.userpwd = "#{@username}:#{@password}"
  
  curl.headers={}
  curl.headers.merge!({"X-On-Behalf-Of" => options[:on_behalf_of]}) if options[:on_behalf_of].present?
  
  if body.empty? or body[:list]
    curl.url = curl.url + params_list(body[:list]) if body[:list]
    curl.perform
  elsif body[:post]
    curl.headers.merge!({"Content-Type" => "application/xml"})
    curl.http_post 
  elsif body[:create]
    curl.headers.merge!({"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.merge!({ "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, format)
end

#params_list(list) ⇒ Object



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

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



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

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