Class: DiscourseApi::Client

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

Instance Attribute Summary collapse

Instance Method Summary collapse

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, #invite_admin, #log_out, #update_avatar, #update_email, #update_user, #update_username, #user

Methods included from API::Posts

#create_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::SSO

#sync_sso

Methods included from API::Search

#search

Methods included from API::Categories

#categories, #category_latest_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)


33
34
35
36
37
38
39
40
# File 'lib/discourse_api/client.rb', line 33

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.



19
20
21
# File 'lib/discourse_api/client.rb', line 19

def api_key
  @api_key
end

#api_usernameObject

Returns the value of attribute api_username.



19
20
21
# File 'lib/discourse_api/client.rb', line 19

def api_username
  @api_username
end

#hostObject (readonly)

Returns the value of attribute host.



20
21
22
# File 'lib/discourse_api/client.rb', line 20

def host
  @host
end

Instance Method Details

#connection_optionsObject



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

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

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



52
53
54
# File 'lib/discourse_api/client.rb', line 52

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

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



56
57
58
# File 'lib/discourse_api/client.rb', line 56

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

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



74
75
76
# File 'lib/discourse_api/client.rb', line 74

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

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



60
61
62
63
64
65
66
67
68
# File 'lib/discourse_api/client.rb', line 60

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

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



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

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

#user_agentObject



78
79
80
# File 'lib/discourse_api/client.rb', line 78

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