discourse_api 0.45.1 → 0.46.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 10c80ce6388205a855042bd1093627508e4929c6bf79902487549568af7304b0
4
- data.tar.gz: 4aab3d70ba9343f6717a4e4ffce233b015565904129fd4b1244e7bdac64ef565
3
+ metadata.gz: 83d6fc91bd7fd472cc3eab0fa06b79d0fbaa5e4cf7a2003bef82eee43f33acf4
4
+ data.tar.gz: c0aef1ea4eee35c3cf3143d100707c2850829e5551c79af3a6d6f76efb3b262c
5
5
  SHA512:
6
- metadata.gz: b37002fd695ca45872ba4b2ca09161173050f70692c8f051df737dd87370cb06c5103b51d5691807041ffbac9a0a9a1713fd74f406da192eeb8e29b41460c90a
7
- data.tar.gz: 13163d2718c324780cb081aaca1e63f865e9b503bc008f4d1fbcb1145b6f3175e6621f6dbbaff14a30f8d730d9800ab6af8e3e1caee93b8baed8682d275f4b68
6
+ metadata.gz: 4227051151b05bd67b403a9922d0199b4cdaf84ccf5973e4be57fb63af8345053b0d80ca763aa2a9c49c27a1a73a623540bde643a714710298c57e73c4a3a0d3
7
+ data.tar.gz: 5a6dfdc85bf1db5edb7e474ecfa1df038a617ebb4d3bf890611d6e195e3093d441e2dbdb3826ad468cc397e6de420f4d4d295b275ad0febb78fb32ae86dfec8e
data/CHANGELOG.md CHANGED
@@ -6,6 +6,21 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
6
6
 
7
7
  ## [Unreleased]
8
8
 
9
+ ## [0.46.0] - 2021-04-12
10
+ ### Added
11
+ - Allow bookmarking topics
12
+ - Add timeout to requests
13
+ - Add params to get_topic_posts
14
+
15
+ ## [0.45.1] - 2021-03-11
16
+ ### Added
17
+ - Fetch global top topics
18
+ - Allow setting topic notifications
19
+ - Return full category response
20
+
21
+ ### Changed
22
+ - Use new search endpoint
23
+
9
24
  ## [0.45.0] - 2021-01-15
10
25
  ### Added
11
26
  - Tag configuration in create_category/update_category
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
3
+ require File.expand_path('../../lib/discourse_api', __FILE__)
4
+
5
+ config = DiscourseApi::ExampleHelper.load_yml
6
+
7
+ client = DiscourseApi::Client.new(config['host'] || 'http://localhost:3000')
8
+ client.api_key = config['api_key'] || "YOUR_API_KEY"
9
+ client.api_username = config['api_username'] || "YOUR_USERNAME"
10
+
11
+ # Bookmark topic
12
+ puts client.bookmark_topic(1418)
13
+
14
+ # Remove bookmark from topic
15
+ puts client.remove_topic_bookmark(1418)
@@ -74,13 +74,22 @@ module DiscourseApi
74
74
  delete("/t/#{id}.json")
75
75
  end
76
76
 
77
- def topic_posts(topic_id, post_ids = [])
77
+ def topic_posts(topic_id, post_ids = [], params = {})
78
+ params = API.params(params)
79
+ .optional(:asc,
80
+ :filter,
81
+ :include_raw,
82
+ :include_suggested,
83
+ :post_number,
84
+ :username_filters,
85
+ )
86
+
78
87
  url = ["/t/#{topic_id}/posts.json"]
79
88
  if post_ids.count > 0
80
89
  url.push('?')
81
90
  url.push(post_ids.map { |id| "post_ids[]=#{id}" }.join('&'))
82
91
  end
83
- response = get(url.join)
92
+ response = get(url.join, params)
84
93
  response[:body]
85
94
  end
86
95
 
@@ -96,6 +105,14 @@ module DiscourseApi
96
105
  .required(:notification_level)
97
106
  post("/t/#{topic_id}/notifications", params)
98
107
  end
108
+
109
+ def bookmark_topic(topic_id)
110
+ put("/t/#{topic_id}/bookmark.json")
111
+ end
112
+
113
+ def remove_topic_bookmark(topic_id)
114
+ put("/t/#{topic_id}/remove_bookmarks.json")
115
+ end
99
116
  end
100
117
  end
101
118
  end
@@ -29,7 +29,9 @@ module DiscourseApi
29
29
  class Client
30
30
  attr_accessor :api_key
31
31
  attr_accessor :basic_auth
32
- attr_reader :host, :api_username
32
+ attr_reader :host, :api_username, :timeout
33
+
34
+ DEFAULT_TIMEOUT = 30
33
35
 
34
36
  include DiscourseApi::API::Categories
35
37
  include DiscourseApi::API::Search
@@ -60,6 +62,11 @@ module DiscourseApi
60
62
  @use_relative = check_subdirectory(host)
61
63
  end
62
64
 
65
+ def timeout=(timeout)
66
+ @timeout = timeout
67
+ @connection.options.timeout = timeout if @connection
68
+ end
69
+
63
70
  def api_username=(api_username)
64
71
  @api_username = api_username
65
72
  @connection.headers['Api-Username'] = api_username unless @connection.nil?
@@ -68,6 +75,9 @@ module DiscourseApi
68
75
  def connection_options
69
76
  @connection_options ||= {
70
77
  url: @host,
78
+ request: {
79
+ timeout: @timeout || DEFAULT_TIMEOUT
80
+ },
71
81
  headers: {
72
82
  accept: 'application/json',
73
83
  user_agent: user_agent,
@@ -158,6 +168,8 @@ module DiscourseApi
158
168
  response.env
159
169
  rescue Faraday::ClientError, JSON::ParserError
160
170
  raise DiscourseApi::Error
171
+ rescue Faraday::ConnectionFailed
172
+ raise DiscourseApi::Timeout
161
173
  end
162
174
 
163
175
  def handle_error(response)
@@ -33,4 +33,7 @@ module DiscourseApi
33
33
 
34
34
  class TooManyRequests < DiscourseError
35
35
  end
36
+
37
+ class Timeout < DiscourseError
38
+ end
36
39
  end
@@ -1,4 +1,4 @@
1
1
  # frozen_string_literal: true
2
2
  module DiscourseApi
3
- VERSION = "0.45.1"
3
+ VERSION = "0.46.0"
4
4
  end
@@ -170,6 +170,14 @@ describe DiscourseApi::API::Topics do
170
170
  expect(body['post_stream']['posts']).to be_an Array
171
171
  expect(body['post_stream']['posts'].first).to be_a Hash
172
172
  end
173
+
174
+ it "can retrieve a topic posts' raw attribute" do
175
+ body = subject.topic_posts(57, [123], { include_raw: true })
176
+ expect(body).to be_a Hash
177
+ expect(body['post_stream']['posts']).to be_an Array
178
+ expect(body['post_stream']['posts'].first).to be_a Hash
179
+ expect(body['post_stream']['posts'].first['raw']).to be_an Array
180
+ end
173
181
  end
174
182
 
175
183
  describe "#create_topic_with_tags" do
@@ -200,4 +208,28 @@ describe DiscourseApi::API::Topics do
200
208
  expect(response['success']).to eq('OK')
201
209
  end
202
210
  end
211
+
212
+ describe "#bookmark_topic" do
213
+ before do
214
+ stub_put("#{host}/t/1/bookmark.json").to_return(body: "", headers: { content_type: "application/json" })
215
+ end
216
+
217
+ it "makes the put request" do
218
+ response = subject.bookmark_topic(1)
219
+ expect(a_put("#{host}/t/1/bookmark.json")).to have_been_made
220
+ expect(response.body).to eq(nil)
221
+ end
222
+ end
223
+
224
+ describe "#remove_topic_bookmark" do
225
+ before do
226
+ stub_put("#{host}/t/1/remove_bookmarks.json").to_return(body: "", headers: { content_type: "application/json" })
227
+ end
228
+
229
+ it "makes the put request" do
230
+ response = subject.remove_topic_bookmark(1)
231
+ expect(a_put("#{host}/t/1/remove_bookmarks.json")).to have_been_made
232
+ expect(response.body).to eq(nil)
233
+ end
234
+ end
203
235
  end
@@ -28,6 +28,21 @@ describe DiscourseApi::Client do
28
28
  end
29
29
  end
30
30
 
31
+ describe "#timeout" do
32
+ context 'custom timeout' do
33
+ it "is set to Faraday connection" do
34
+ expect(subject.send(:connection).options.timeout).to eq(30)
35
+ end
36
+ end
37
+
38
+ context 'default timeout' do
39
+ it "is set to Faraday connection" do
40
+ subject.timeout = 25
41
+ expect(subject.send(:connection).options.timeout).to eq(25)
42
+ end
43
+ end
44
+ end
45
+
31
46
  describe "#api_key" do
32
47
  it "is publically accessible" do
33
48
  subject.api_key = "test_d7fd0429940"
@@ -34,6 +34,7 @@
34
34
  "read":true,
35
35
  "user_title":null,
36
36
  "actions_summary":[{"id":2,"count":3,"can_act":true}],
37
+ "raw": [{"type":"paragraph","children":[{"text":"This is a raw post I've got white space!... and emojis 😈😈😈😈😈😈"}]}],
37
38
  "moderator":false,
38
39
  "admin":false,
39
40
  "staff":false,
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: discourse_api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.45.1
4
+ version: 0.46.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sam Saffron
@@ -11,7 +11,7 @@ authors:
11
11
  autorequire:
12
12
  bindir: bin
13
13
  cert_chain: []
14
- date: 2021-03-11 00:00:00.000000000 Z
14
+ date: 2021-04-12 00:00:00.000000000 Z
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
17
17
  name: faraday
@@ -204,6 +204,7 @@ files:
204
204
  - discourse_api.gemspec
205
205
  - examples/backups.rb
206
206
  - examples/badges.rb
207
+ - examples/bookmark_topic.rb
207
208
  - examples/category.rb
208
209
  - examples/change_topic_status.rb
209
210
  - examples/create_private_message.rb
@@ -344,7 +345,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
344
345
  - !ruby/object:Gem::Version
345
346
  version: '0'
346
347
  requirements: []
347
- rubygems_version: 3.1.4
348
+ rubygems_version: 3.1.6
348
349
  signing_key:
349
350
  specification_version: 4
350
351
  summary: Allows access to the Discourse API