Class: RCloud::Request

Inherits:
Object
  • Object
show all
Defined in:
lib/rcloud.rb

Constant Summary collapse

BASE_URL =
"https://api.cn.rong.io"
URLS =
["/user/getToken",
"/user/refresh",
"/user/checkOnline",	
"/user/block",
"/user/unblock",
"/user/block/query",
"/user/blacklist/add",
"/user/blacklist/remove",
"/user/blacklist/query",
"/message/private/publish", 
"/message/system/publish",
"/message/group/publish",
"/message/chatroom/publish",
"/message/broadcast",
"/message/history",
"/message/history/delete",
"/group/sync",
"/group/create",
"/group/join",
"/group/quit",
"/group/dismiss",
"/group/refresh",
"/chatroom/create",
"/chatroom/destroy",
"/chatroom/query",
"/chatroom/user/query"]
SUFFIX =
".json"
PARAMS =

user

[ [:userId, :name, :portraitUri],  # user
				   [:userId, :name, :portraitUri],
				   [:userId], 
				   [:userId, :minute], # block
				   [:userId],
				   [], 
				   [:userId, :blackUserId], # blacklist
				   [:userId, :blackUserId],
				   [:userId],
				   [:fromUserId, :toUserId, :objectName, :content, :pushContent, :pushData ], #message
				   [:fromUserId, :toUserId, :objectName, :content, :pushContent, :pushData ], 
				   [:fromUserId, :toGroupId, :objectName, :content, :pushContent, :pushData ],
				   [:fromUserId, :toChatroomId, :objectName, :content, :pushContent, :pushData ],
				   [:fromUserId, :objectName, :content, :pushContent, :pushData ],
				   [:date], # message history
				   [:date], 
				   [:userId], # Group Sync redefine
				   [:userId, :groupId, :groupName] ,
				   [:userId, :groupId, :groupName] ,
				   [:userId, :groupId],
				   [:userId, :groupId],
				   [:groupId, :groupName], 
				   [:userId],# Chatroom redefine
				   [:chatroomId],# 
				   [:chatroomId],# 
				   [:chatroomId]# 
]
@@app_key =
""
@@app_sec =
""
@@mapping =
Proc.new do |url|
						url.downcase.gsub(/\//, '_')[1..-1].to_sym
end

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app_key, app_sec, mapping = nil) ⇒ Request

Returns a new instance of Request.



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/rcloud.rb', line 95

def initialize(app_key, app_sec, mapping = nil)
	@@app_key = app_key
	@@app_sec = app_sec
	@@mapping = mapping if mapping

	URLS.each_with_index do |url, index| 
		Request.creat_api_method(@@mapping.call(url), url,  PARAMS[index])
	end

	self.class.send(:define_method, @@mapping.call("/chatroom/create") ) do  |hash|
		p = {}
		hash.each do |k,v| 
			p["chatroom[#{k}]"] = v 
		end
		resp = RestClient.post BASE_URL + "/chatroom/create" + SUFFIX , p , http_header
		result = JSON.parse(resp)
	end

	self.class.send(:define_method, @@mapping.call("/group/sync") ) do  |*args|
		p = {}
		p["userId"] = args[0].to_s
		hash = *args[1]
		hash.each do |k,v| 
			p["group[#{k}]"] = v 
		end
		resp = RestClient.post BASE_URL +  "/group/sync" + SUFFIX , p , http_header
		result = JSON.parse(resp)
	end


end

Class Method Details

.creat_api_method(method_name, url, params) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/rcloud.rb', line 76

def self.creat_api_method(method_name, url, params)  
   	self.send(:define_method, method_name) do  |*args|
       	p = Hash.new
				params.each_with_index  do |k, i|
p[k] = *args[i]
p[k] = p[k][0].to_s
				end  
				resp = nil
				begin
resp = RestClient.post BASE_URL + url + SUFFIX , p , http_header
				rescue => e
p e 
				end
				result = JSON.parse(resp)
   	end  
end

Instance Method Details

#http_headerObject



129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/rcloud.rb', line 129

def http_header
	time = Time.now.to_i
	r = Random.new
	random = r.rand(100000000)
	sig_str = @@app_sec + random.to_s + time.to_s
	sig = Digest::SHA1.hexdigest sig_str
	header = {
		"RC-App-Key" => @@app_key,
		"RC-Nonce" => random,
		"RC-Timestamp" => time,
		"RC-Signature" => sig
	}
end