Class: Mchat::Request

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/mchat/api.rb

Instance Method Summary collapse

Constructor Details

#initialize(server) ⇒ Request

Returns a new instance of Request.



45
46
47
48
# File 'lib/mchat/api.rb', line 45

def initialize(server)
  self.class.base_uri server
  @headers = {"Accept": "application/json"}
end

Instance Method Details

#conn_server_startupObject



66
67
68
# File 'lib/mchat/api.rb', line 66

def conn_server_startup
  json_parse self.class.get("/startup")
end

#create_channel(channel_name) ⇒ Object



80
81
82
83
# File 'lib/mchat/api.rb', line 80

def create_channel(channel_name)
  # 创建用户频道
  json_parse self.class.post("/channels/#{channel_name}")
end

#create_channel_message(channel_name, user_name, content) ⇒ Object



113
114
115
116
117
118
119
120
121
122
# File 'lib/mchat/api.rb', line 113

def create_channel_message(channel_name, user_name, content)
  json_parse self.class.post(
    "/channels/#{channel_name}/messages",
    body: {
      user_name: user_name,
      content: content
    }.to_json,
    headers: @headers
  )
end

#delete_channel(channel_name) ⇒ Object



85
86
87
# File 'lib/mchat/api.rb', line 85

def delete_channel(channel_name)
  json_parse self.class.delete("/channels/#{channel_name}")
end

#fetch_channel_message(channel_name) ⇒ Object



124
125
126
127
128
129
# File 'lib/mchat/api.rb', line 124

def fetch_channel_message(channel_name)
  json_parse self.class.get(
    "/channels/#{channel_name}/messages",
    headers: @headers
  )
end

#get_channel(channel_name) ⇒ Object



75
76
77
78
# File 'lib/mchat/api.rb', line 75

def get_channel(channel_name)
  # 获得 channels 详情
  json_parse self.class.get("/channels/#{channel_name}")
end

#get_channelsObject



70
71
72
73
# File 'lib/mchat/api.rb', line 70

def get_channels
  # 获得 channels列表
  json_parse self.class.get("/channels")
end

#get_server_timestampObject



62
63
64
# File 'lib/mchat/api.rb', line 62

def get_server_timestamp
  json_parse self.class.get("/timestamp")
end

#join_channel(channel_name, user_name) ⇒ Object



89
90
91
92
93
94
95
# File 'lib/mchat/api.rb', line 89

def join_channel(channel_name, user_name)
  json_parse self.class.post(
    "/channels/#{channel_name}/join",
    body: { user_name: user_name }.to_json,
    headers: @headers
  )
end

#json_parse(resp) ⇒ Object



50
51
52
# File 'lib/mchat/api.rb', line 50

def json_parse(resp)
  JSON.parse(resp.body)
end

#leave_channel(channel_name, user_name) ⇒ Object



97
98
99
100
101
102
103
# File 'lib/mchat/api.rb', line 97

def leave_channel(channel_name, user_name)
  json_parse self.class.post(
    "/channels/#{channel_name}/leave",
    body: { user_name: user_name }.to_json,
    headers: @headers
  )
end

#ping_channel(channel_name, user_name) ⇒ Object



105
106
107
108
109
110
111
# File 'lib/mchat/api.rb', line 105

def ping_channel(channel_name, user_name)
  json_parse self.class.post(
    "/channels/#{channel_name}/ping",
    body: { user_name: user_name }.to_json,
    headers: @headers
  )
end

#ping_serverObject



58
59
60
# File 'lib/mchat/api.rb', line 58

def ping_server
  json_parse self.class.get("/ping")
end

#server_homeObject



54
55
56
# File 'lib/mchat/api.rb', line 54

def server_home
  json_parse self.class.get("/")
end