Skip to content

Commit bce93f6

Browse files
kimardenmilleroblakeerickson
authored andcommitted
- Example files now read config.yml file when present for client settings (#189)
1 parent 5030a0b commit bce93f6

26 files changed

+142
-72
lines changed

.gitignore

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,6 @@ bin/
1919
.ruby-gemset
2020
.ruby-version
2121
.env
22+
/config.yml
2223

23-
24-
.rubocop-https---raw-githubusercontent-com-discourse-discourse-master--rubocop-yml
24+
.rubocop-https---raw-githubusercontent-com-discourse-discourse-master--rubocop-yml

config_example.yml

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Specify your environment by making a copy of this file to create a file in your root directory called config.yml with your environment settings.
2+
3+
# host e.g. localhost:3000 or discourse.my_domain.com
4+
host: YOUR_HOST_NAME
5+
6+
# api user (can effect results returned, e.g. .categories method returns only the categories your api_username can see)
7+
# create a new client with when changing api user
8+
api_username: YOUR_API_USERNAME
9+
10+
# api key from Discourse admin panel /admin/api/keys
11+
api_key: YOUR_API_KEY

examples/backups.rb

+5-3
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22
$LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
33
require File.expand_path('../../lib/discourse_api', __FILE__)
44

5-
client = DiscourseApi::Client.new("http://localhost:3000")
6-
client.api_key = "YOUR_API_KEY"
7-
client.api_username = "YOUR_USERNAME"
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"
810

911
# get list of backup files
1012
puts client.backups()

examples/badges.rb

+5-3
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22
$LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
33
require File.expand_path('../../lib/discourse_api', __FILE__)
44

5-
client = DiscourseApi::Client.new("http://localhost:3000")
6-
client.api_key = "YOUR_API_KEY"
7-
client.api_username = "YOUR_USERNAME"
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"
810

911
# get badges
1012
puts client.badges

examples/category.rb

+5-3
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22
$LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
33
require File.expand_path('../../lib/discourse_api', __FILE__)
44

5-
client = DiscourseApi::Client.new("http://localhost:3000")
6-
client.api_key = "YOUR_API_KEY"
7-
client.api_username = "YOUR_USERNAME"
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"
810

911
# get categories
1012
puts client.categories()

examples/change_topic_status.rb

+5-3
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22
$LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
33
require File.expand_path('../../lib/discourse_api', __FILE__)
44

5-
client = DiscourseApi::Client.new("http://localhost:3000")
6-
client.api_key = "YOUR_API_KEY"
7-
client.api_username = "YOUR_USERNAME"
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"
810

911
response = client.create_topic(
1012
category: 1,

examples/create_private_message.rb

+5-3
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22
$LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
33
require File.expand_path('../../lib/discourse_api', __FILE__)
44

5-
client = DiscourseApi::Client.new("http://localhost:3000")
6-
client.api_key = "YOUR_API_KEY"
7-
client.api_username = "YOUR_USERNAME"
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"
810

911
client.create_private_message(
1012
title: "Confidential: Hello World!",

examples/create_topic.rb

+5-3
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22
$LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
33
require File.expand_path('../../lib/discourse_api', __FILE__)
44

5-
client = DiscourseApi::Client.new("http://localhost:3000")
6-
client.api_key = "YOUR_API_KEY"
7-
client.api_username = "YOUR_USERNAME"
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"
810

911
client.create_topic(
1012
category: 1,

examples/create_update_category.rb

+4-7
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,11 @@
22
$LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
33
require File.expand_path('../../lib/discourse_api', __FILE__)
44

5-
# client = DiscourseApi::Client.new("http://localhost:3000")
6-
# client.api_key = "YOUR_API_KEY"
7-
# client.api_username = "YOUR_USERNAME"
8-
#
5+
config = DiscourseApi::ExampleHelper.load_yml
96

10-
client = DiscourseApi::Client.new("http://localhost:8080")
11-
client.api_key = "a56a349e1870529d8d8da11453ea782ce8ab8de34d6564a65727171163be4338"
12-
client.api_username = "system"
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"
1310

1411
###
1512
# Required category params:

examples/create_user.rb

+5-3
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22
$LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
33
require File.expand_path('../../lib/discourse_api', __FILE__)
44

5-
client = DiscourseApi::Client.new("http://localhost:3000")
6-
client.api_key = "YOUR_API_KEY"
7-
client.api_username = "YOUR_USERNAME"
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"
810

911
# create user
1012
user = client.create_user(

examples/dashboard.rb

+5-3
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22
$LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
33
require File.expand_path('../../lib/discourse_api', __FILE__)
44

5-
client = DiscourseApi::Client.new("http://localhost:3000")
6-
client.api_key = "YOUR_API_KEY"
7-
client.api_username = "YOUR_USERNAME"
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"
810

911
# get all dashboard status as json
1012
puts client.get_dashboard_stats

examples/disposable_invite_tokens.rb

+5-3
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@
66
$LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
77
require File.expand_path('../../lib/discourse_api', __FILE__)
88

9-
client = DiscourseApi::Client.new("http://localhost:3000")
10-
client.api_key = "YOUR_API_KEY"
11-
client.api_username = "YOUR_USERNAME"
9+
config = DiscourseApi::ExampleHelper.load_yml
10+
11+
client = DiscourseApi::Client.new(config['host'] || 'http://localhost:3000')
12+
client.api_key = config['api_key'] || "YOUR_API_KEY"
13+
client.api_username = config['api_username'] || "YOUR_USERNAME"
1214

1315
# fetch email-less invite tokens
1416
invite_tokens = client.disposable_tokens(username: "eviltrout", quantity: 5, group_names: "security,support")

examples/example.rb

+5-3
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22
$LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
33
require File.expand_path('../../lib/discourse_api', __FILE__)
44

5-
client = DiscourseApi::Client.new("http://localhost:3000")
6-
client.api_key = "YOUR_API_KEY"
7-
client.api_username = "YOUR_USERNAME"
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"
810

911
# get latest topics
1012
puts client.latest_topics

examples/group_set_user_notification_level.rb

+5-3
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22
$LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
33
require File.expand_path('../../lib/discourse_api', __FILE__)
44

5-
client = DiscourseApi::Client.new("http://localhost:3000")
6-
client.api_key = "YOUR_API_KEY"
7-
client.api_username = "YOUR_USERNAME"
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"
810

911
@target_username = "YOUR_TARGET_USERNAME"
1012
@target_group_id = # YOUR NUMERIC TARGET GROUP ID

examples/groups.rb

+5-3
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22
$LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
33
require File.expand_path('../../lib/discourse_api', __FILE__)
44

5-
client = DiscourseApi::Client.new("http://localhost:3000")
6-
client.api_key = "YOUR_API_KEY"
7-
client.api_username = "YOUR_USERNAME"
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"
810

911
response = client.create_group(name: "engineering_team")
1012
group_id = response["basic_group"]["id"]

examples/invite_users.rb

+5-3
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22
$LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
33
require File.expand_path('../../lib/discourse_api', __FILE__)
44

5-
client = DiscourseApi::Client.new("http://localhost:3000")
6-
client.api_key = "YOUR_API_KEY"
7-
client.api_username = "YOUR_USERNAME"
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"
810

911
# invite user
1012
client.invite_user(email: "[email protected]", group_ids: "41,42")

examples/polls.rb

+5-3
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33
$LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
44
require File.expand_path('../../lib/discourse_api', __FILE__)
55

6-
client = DiscourseApi::Client.new("http://localhost:3000")
7-
client.api_key = "YOUR_API_KEY"
8-
client.api_username = "YOUR_USERNAME"
6+
config = DiscourseApi::ExampleHelper.load_yml
7+
8+
client = DiscourseApi::Client.new(config['host'] || 'http://localhost:3000')
9+
client.api_key = config['api_key'] || "YOUR_API_KEY"
10+
client.api_username = config['api_username'] || "YOUR_USERNAME"
911

1012
options = ['8b4736b1ae3dfb5a28088530f036f9e5']
1113
poll_option_votes = client.poll_vote post_id: 5, poll_name: 'poll', options: options

examples/post_action.rb

+5-3
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22
$LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
33
require File.expand_path('../../lib/discourse_api', __FILE__)
44

5-
client = DiscourseApi::Client.new("http://localhost:3000")
6-
client.api_key = "YOUR_API_KEY"
7-
client.api_username = "YOUR_USERNAME"
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"
810

911
# Post Action Type IDs
1012
# 1 - Bookmark

examples/search.rb

+5-3
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22
$LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
33
require File.expand_path('../../lib/discourse_api', __FILE__)
44

5-
client = DiscourseApi::Client.new("http://localhost:3000")
6-
client.api_key = "YOUR_API_KEY"
7-
client.api_username = "YOUR_USERNAME"
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"
810

911
# search for term
1012
puts client.search("discourse")

examples/sent_private_messages.rb

+5-3
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22
$LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
33
require File.expand_path('../../lib/discourse_api', __FILE__)
44

5-
client = DiscourseApi::Client.new("http://localhost:3000")
6-
client.api_key = "YOUR_API_KEY"
7-
client.api_username = "YOUR_USERNAME"
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"
810

911
client.sent_private_messages('test_user')

examples/sso.rb

+5-3
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22
$LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
33
require File.expand_path('../../lib/discourse_api', __FILE__)
44

5-
client = DiscourseApi::Client.new("http://localhost:3000")
6-
client.api_key = "YOUR_API_KEY"
7-
client.api_username = "YOUR_USERNAME"
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"
810

911
client.sync_sso(
1012
sso_secret: "discourse_sso_rocks",

examples/topic_lists.rb

+5-3
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22
$LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
33
require File.expand_path('../../lib/discourse_api', __FILE__)
44

5-
client = DiscourseApi::Client.new("http://localhost:3000")
6-
client.api_key = "YOUR_API_KEY"
7-
client.api_username = "YOUR_USERNAME"
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"
810

911
# get latest topics
1012
puts client.latest_topics({})

examples/update_user.rb

+5-3
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22
$LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
33
require File.expand_path('../../lib/discourse_api', __FILE__)
44

5-
client = DiscourseApi::Client.new("http://localhost:3000")
6-
client.api_key = "YOUR_API_KEY"
7-
client.api_username = "YOUR_USERNAME"
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"
810

911
# update username from "robin" to "batman"
1012
puts client.update_username("robin", "batman")

examples/upload_file.rb

+5-3
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22
$LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
33
require File.expand_path('../../lib/discourse_api', __FILE__)
44

5-
client = DiscourseApi::Client.new("http://localhost:3000")
6-
client.api_key = "YOUR_API_KEY"
7-
client.api_username = "YOUR_USERNAME"
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"
810

911
# Upload a file
1012
file = Faraday::UploadIO.new('grumpy_cat.pdf', "application/pdf")

lib/discourse_api.rb

+1
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@
33
require "discourse_api/client"
44
require "discourse_api/error"
55
require "discourse_api/version"
6+
require "discourse_api/example_helper"
67
require "discourse_api/single_sign_on"

lib/discourse_api/example_helper.rb

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# frozen_string_literal: true
2+
require 'yaml'
3+
4+
module DiscourseApi
5+
class ExampleHelper
6+
7+
def self.load_yml
8+
config_yml = File.expand_path('../../../config.yml', __FILE__)
9+
puts config_yml
10+
begin
11+
config = YAML.load_file config_yml
12+
rescue Errno::ENOENT
13+
config = {}
14+
end
15+
config
16+
end
17+
18+
end
19+
end

0 commit comments

Comments
 (0)