Class: DiscourseApi::Client

Inherits:
Object
  • Object
show all
Includes:
API::ApiKey, API::Backups, API::Badges, API::Categories, API::Email, API::Groups, API::Invite, API::Notifications, API::Posts, API::PrivateMessages, API::SSO, API::Search, API::Tags, API::Topics, API::Users
Defined in:
lib/discourse_api/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from API::Backups

#backups, #create_backup, #download_backup, #restore_backup

Methods included from API::ApiKey

#api, #generate_master_key, #generate_user_api_key, #regenerate_api_key, #revoke_api_key, #revoke_user_api_key

Methods included from API::Email

#email_settings, #list_email

Methods included from API::Badges

#badges, #grant_user_badge, #user_badges

Methods included from API::Notifications

#notifications

Methods included from API::PrivateMessages

#private_messages

Methods included from API::Invite

#disposable_tokens, #invite_user, #invite_user_to_topic

Methods included from API::Groups

#create_group, #group_add, #group_remove, #groups

Methods included from API::Users

#activate, #create_user, #grant_admin, #invite_admin, #list_users, #log_out, #revoke_admin, #update_avatar, #update_email, #update_trust_level, #update_user, #update_username, #user

Methods included from API::Posts

#create_post, #edit_post, #get_post, #wikify_post

Methods included from API::Topics

#create_topic, #delete_topic, #latest_topics, #new_topics, #recategorize_topic, #rename_topic, #topic, #topics_by

Methods included from API::Tags

#show_tag

Methods included from API::SSO

#sync_sso

Methods included from API::Search

#search

Methods included from API::Categories

#categories, #category, #category_latest_topics, #category_new_topics, #category_top_topics, #create_category

Constructor Details

#initialize(host = ENV["DISCOURSE_URL"], api_key = ENV["DISCOURSE_API_KEY"], api_username = ENV["DISCOURSE_USERNAME"]) ⇒ Client

Returns a new instance of Client.

Raises:

  • (ArgumentError)


43
44
45
46
47
48
49
50
# File 'lib/discourse_api/client.rb', line 43

def initialize(host = ENV["DISCOURSE_URL"],
               api_key = ENV["DISCOURSE_API_KEY"],
               api_username = ENV["DISCOURSE_USERNAME"])
  raise ArgumentError, 'host needs to be defined' if host.nil? || host.empty?
  @host         = host
  @api_key      = api_key
  @api_username = api_username
end

Instance Attribute Details

#api_keyObject

Returns the value of attribute api_key.



24
25
26
# File 'lib/discourse_api/client.rb', line 24

def api_key
  @api_key
end

#api_usernameObject

Returns the value of attribute api_username.



24
25
26
# File 'lib/discourse_api/client.rb', line 24

def api_username
  @api_username
end

#hostObject (readonly)

Returns the value of attribute host.



25
26
27
# File 'lib/discourse_api/client.rb', line 25

def host
  @host
end

Instance Method Details

#connection_optionsObject



52
53
54
55
56
57
58
59
60
# File 'lib/discourse_api/client.rb', line 52

def connection_options
  @connection_options ||= {
    url: @host,
    headers: {
      accept: 'application/json',
      user_agent: user_agent,
    }
  }
end

#delete(path, params = {}) ⇒ Object



66
67
68
# File 'lib/discourse_api/client.rb', line 66

def delete(path, params={})
  request(:delete, path, params)
end

#get(path, params = {}) ⇒ Object



70
71
72
# File 'lib/discourse_api/client.rb', line 70

def get(path, params={})
  request(:get, path, params)
end

#patch(path, params = {}) ⇒ Object



88
89
90
# File 'lib/discourse_api/client.rb', line 88

def patch(path, params={})
  request(:patch, path, params)
end

#post(path, params = {}) ⇒ Object



74
75
76
77
78
79
80
81
82
# File 'lib/discourse_api/client.rb', line 74

def post(path, params={})
  response = request(:post, path, params)
  case response.status
  when 200
    response.body
  else
    raise DiscourseApi::Error, response.body
  end
end

#put(path, params = {}) ⇒ Object



84
85
86
# File 'lib/discourse_api/client.rb', line 84

def put(path, params={})
  request(:put, path, params)
end

#ssl(options) ⇒ Object



62
63
64
# File 'lib/discourse_api/client.rb', line 62

def ssl(options)
  connection_options[:ssl] = options
end

#user_agentObject



92
93
94
# File 'lib/discourse_api/client.rb', line 92

def user_agent
  @user_agent ||= "DiscourseAPI Ruby Gem #{DiscourseApi::VERSION}"
end