Class: DiscourseApi::Client

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from API::Dashboard

#get_dashboard_stats, #get_dashboard_stats_totals

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, #create_badge, #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, #delete_group, #group_add, #group_remove, #groups

Methods included from API::Users

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

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, #topic_posts, #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, api_key = nil, api_username = nil) ⇒ Client

Returns a new instance of Client.

Raises:

  • (ArgumentError)


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

def initialize(host, api_key = nil, api_username = nil)
  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



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

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

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



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

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

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



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

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

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



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

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

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



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

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



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

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

#ssl(options) ⇒ Object



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

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

#user_agentObject



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

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