Class: ZenPush::Zendesk

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/zenpush/zendesk.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Zendesk

debug_output



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/zenpush/zendesk.rb', line 15

def initialize(options = {})

  default_options = {
    :uri => nil,
    :user => nil,
    :password => nil,
    :filenames_use_dashes_instead_of_spaces => false,
  }

  zenpush_yml = File.join(ENV['HOME'], '.zenpush.yml')

  if File.readable?(zenpush_yml)
    zenpush_yml_opts = YAML.load_file(zenpush_yml)
    default_options.merge!(zenpush_yml_opts)
  end

  opts = default_options.merge!(options)
  opts.each_pair { |k,v| raise "#{k} is nil" if v.nil? }

  @options = opts

  self.class.base_uri opts[:uri] + '/api/v2'
  self.class.basic_auth opts[:user], opts[:password]
end

Instance Attribute Details

#optionsObject

Returns the value of attribute options.



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

def options
  @options
end

Instance Method Details

#categories(options = {}) ⇒ Object



56
57
58
# File 'lib/zenpush/zendesk.rb', line 56

def categories(options = {})
  self.get('/categories.json', options).parsed_response['categories']
end

#category(category_id, options = {}) ⇒ Object



60
61
62
# File 'lib/zenpush/zendesk.rb', line 60

def category(category_id, options = {})
  self.get("/categories/#{category_id}.json", options).parsed_response
end

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



52
53
54
# File 'lib/zenpush/zendesk.rb', line 52

def delete(uri, options = {})
  self.class.delete(uri, options)
end

#find_category(category_name, options = {}) ⇒ Object

Find category by name



85
86
87
88
89
90
91
92
# File 'lib/zenpush/zendesk.rb', line 85

def find_category(category_name, options = {})
  categories = self.categories
  if categories.is_a?(Array)
    categories.detect { |c| c['name'] == category_name }
  else
    raise "Could not retrieve categories: #{categories}"
  end
end

#find_forum(category_name, forum_name, options = {}) ⇒ Object

Find forum by name, knowing the category name



102
103
104
105
106
107
# File 'lib/zenpush/zendesk.rb', line 102

def find_forum(category_name, forum_name, options = {})
  category = self.find_category(category_name, options)
  if category
    self.forums.detect {|f| f['category_id'] == category['id'] && f['name'] == forum_name }
  end
end

#find_or_create_category(category_name, options = { }) ⇒ Object

Find category by name, creating it if it doesn’t exist



95
96
97
98
99
# File 'lib/zenpush/zendesk.rb', line 95

def find_or_create_category(category_name, options={ })
  find_category(category_name, options) || begin
    (category_name, options={ })
  end
end

#find_or_create_forum(category_name, forum_name, options = { }) ⇒ Object

Given a category name, find a forum by name. Create the category and forum either doesn’t exist.



110
111
112
113
114
115
# File 'lib/zenpush/zendesk.rb', line 110

def find_or_create_forum(category_name, forum_name, options={ })
  category = self.find_or_create_category(category_name, options)
  if category
    self.forums.detect { |f| f['category_id'] == category['id'] && f['name'] == forum_name } || post_forum(category['id'], forum_name)
  end
end

#find_topic(category_name, forum_name, topic_title, options = {}) ⇒ Object

Find topic by name, knowing the forum name and category name



118
119
120
121
122
123
# File 'lib/zenpush/zendesk.rb', line 118

def find_topic(category_name, forum_name, topic_title, options = {})
  forum = self.find_forum(category_name, forum_name, options)
  if forum
    self.topics(forum['id'], options).detect {|t| t['title'] == topic_title}
  end
end

#forum(forum_id, options = {}) ⇒ Object



68
69
70
# File 'lib/zenpush/zendesk.rb', line 68

def forum(forum_id, options = {})
  self.get("/forums/#{forum_id}.json", options).parsed_response
end

#forums(options = {}) ⇒ Object



64
65
66
# File 'lib/zenpush/zendesk.rb', line 64

def forums(options = {})
  self.get('/forums.json', options).parsed_response['forums']
end

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



40
41
42
# File 'lib/zenpush/zendesk.rb', line 40

def get(uri, options = {})
  self.class.get(uri, options)
end

#post(uri, options = {}) ⇒ Object



44
45
46
# File 'lib/zenpush/zendesk.rb', line 44

def post(uri, options = {})
  self.class.post(uri, options)
end

#post_category(category_name, options = { }) ⇒ Object

Create a category with a given name



126
127
128
129
130
131
132
133
134
# File 'lib/zenpush/zendesk.rb', line 126

def (category_name, options={ })
  self.post('/categories.json',
            options.merge(
              :body => { :category => {
                :name => category_name
              } }.to_json
            )
  )['category']
end

#post_forum(category_id, forum_name, options = { }) ⇒ Object

Create a forum in the given category id



137
138
139
140
141
142
143
144
145
146
# File 'lib/zenpush/zendesk.rb', line 137

def post_forum(category_id, forum_name, options={ })
  self.post('/forums.json',
            options.merge(
              :body => { :forum => {
                :name        => forum_name,
                :category_id => category_id
              } }.to_json
            )
  )['forum']
end

#post_topic(forum_id, title, body, options = { }) ⇒ Object

Create a topic in the given forum id



149
150
151
152
153
154
155
156
157
# File 'lib/zenpush/zendesk.rb', line 149

def post_topic(forum_id, title, body, options = { })
  self.post("/topics.json",
            options.merge(
              :body => { :topic => {
                :forum_id => forum_id, :title => title, :body => body
              } }.to_json
            )
  )['topic']
end

#put(uri, options = {}) ⇒ Object



48
49
50
# File 'lib/zenpush/zendesk.rb', line 48

def put(uri, options = {})
  self.class.put(uri, options)
end

#put_topic(id, body, options = { }) ⇒ Object

Update a topic in the given forum id



160
161
162
163
164
165
166
# File 'lib/zenpush/zendesk.rb', line 160

def put_topic(id, body, options = { })
  self.put("/topics/#{id}.json",
           options.merge(
             :body => { :topic => { :body => body } }.to_json
           )
  )['topic']
end

#topic(topic_id, options = {}) ⇒ Object



80
81
82
# File 'lib/zenpush/zendesk.rb', line 80

def topic(topic_id, options = {})
  self.get("/topics/#{topic_id}.json", options).parsed_response
end

#topics(forum_id, options = { }) ⇒ Object



76
77
78
# File 'lib/zenpush/zendesk.rb', line 76

def topics(forum_id, options = { })
  self.get("/forums/#{forum_id}/topics.json", options).parsed_response['topics']
end

#users(options = {}) ⇒ Object



72
73
74
# File 'lib/zenpush/zendesk.rb', line 72

def users(options = {})
  self.get('/users.json', options).parsed_response['users']
end