Class: ZenPush::Zendesk
- Inherits:
-
Object
- Object
- ZenPush::Zendesk
- Includes:
- HTTParty
- Defined in:
- lib/zenpush/zendesk.rb
Instance Attribute Summary collapse
-
#options ⇒ Object
Returns the value of attribute options.
Instance Method Summary collapse
- #categories(options = {}) ⇒ Object
- #category(category_id, options = {}) ⇒ Object
- #delete(uri, options = {}) ⇒ Object
-
#find_category(category_name, options = {}) ⇒ Object
Find category by name.
-
#find_forum(category_name, forum_name, options = {}) ⇒ Object
Find forum by name, knowing the category name.
-
#find_or_create_category(category_name, options = { }) ⇒ Object
Find category by name, creating it if it doesn’t exist.
-
#find_or_create_forum(category_name, forum_name, options = { }) ⇒ Object
Given a category name, find a forum by name.
-
#find_topic(category_name, forum_name, topic_title, options = {}) ⇒ Object
Find topic by name, knowing the forum name and category name.
- #forum(forum_id, options = {}) ⇒ Object
- #forums(options = {}) ⇒ Object
- #get(uri, options = {}) ⇒ Object
-
#initialize(options = {}) ⇒ Zendesk
constructor
debug_output.
- #post(uri, options = {}) ⇒ Object
-
#post_category(category_name, options = { }) ⇒ Object
Create a category with a given name.
-
#post_forum(category_id, forum_name, options = { }) ⇒ Object
Create a forum in the given category id.
-
#post_topic(forum_id, title, body, options = { }) ⇒ Object
Create a topic in the given forum id.
- #put(uri, options = {}) ⇒ Object
-
#put_topic(id, body, options = { }) ⇒ Object
Update a topic in the given forum id.
- #topic(topic_id, options = {}) ⇒ Object
- #topics(forum_id, options = { }) ⇒ Object
- #users(options = {}) ⇒ Object
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( = {}) = { :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) .merge!(zenpush_yml_opts) end opts = .merge!() 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
#options ⇒ Object
Returns the value of attribute options.
10 11 12 |
# File 'lib/zenpush/zendesk.rb', line 10 def @options end |
Instance Method Details
#categories(options = {}) ⇒ Object
56 57 58 |
# File 'lib/zenpush/zendesk.rb', line 56 def categories( = {}) self.get('/categories.json', ).parsed_response['categories'] end |
#category(category_id, options = {}) ⇒ Object
60 61 62 |
# File 'lib/zenpush/zendesk.rb', line 60 def category(category_id, = {}) self.get("/categories/#{category_id}.json", ).parsed_response end |
#delete(uri, options = {}) ⇒ Object
52 53 54 |
# File 'lib/zenpush/zendesk.rb', line 52 def delete(uri, = {}) self.class.delete(uri, ) 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, = {}) 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, = {}) category = self.find_category(category_name, ) 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, ={ }) find_category(category_name, ) || begin post_category(category_name, ={ }) 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, ={ }) category = self.find_or_create_category(category_name, ) 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, = {}) forum = self.find_forum(category_name, forum_name, ) if forum self.topics(forum['id'], ).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, = {}) self.get("/forums/#{forum_id}.json", ).parsed_response end |
#forums(options = {}) ⇒ Object
64 65 66 |
# File 'lib/zenpush/zendesk.rb', line 64 def forums( = {}) self.get('/forums.json', ).parsed_response['forums'] end |
#get(uri, options = {}) ⇒ Object
40 41 42 |
# File 'lib/zenpush/zendesk.rb', line 40 def get(uri, = {}) self.class.get(uri, ) end |
#post(uri, options = {}) ⇒ Object
44 45 46 |
# File 'lib/zenpush/zendesk.rb', line 44 def post(uri, = {}) self.class.post(uri, ) 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 post_category(category_name, ={ }) self.post('/categories.json', .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, ={ }) self.post('/forums.json', .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, = { }) self.post("/topics.json", .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, = {}) self.class.put(uri, ) 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, = { }) self.put("/topics/#{id}.json", .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, = {}) self.get("/topics/#{topic_id}.json", ).parsed_response end |
#topics(forum_id, options = { }) ⇒ Object
76 77 78 |
# File 'lib/zenpush/zendesk.rb', line 76 def topics(forum_id, = { }) self.get("/forums/#{forum_id}/topics.json", ).parsed_response['topics'] end |
#users(options = {}) ⇒ Object
72 73 74 |
# File 'lib/zenpush/zendesk.rb', line 72 def users( = {}) self.get('/users.json', ).parsed_response['users'] end |