Class: Mypeople::Client

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/mypeople/client.rb

Constant Summary collapse

APIS =
{
  :send_message_to_buddy => {:method => "POST", :path => "/mypeople/buddy/send.json"}, # 1:1 대화 메세지 보내기
  :buddy_profile => {:method => "GET", :path => "/mypeople/profile/buddy.json"}, # 친구 프로필 정보 보기
  :get_members => {:method => "GET", :path => "/mypeople/group/members.json"}, # 그룹 대화방 친구 목록 보기
  :send_message_to_group => {:method => "POST", :path => "/mypeople/group/send.json"}, # 그룹 대화방에 메세지 보내기
  :exit_from_group => {:method => "GET", :path => "/mypeople/group/exit.json"}, # 그룹 대화방 나가기
  :download_file => {:method => "GET", :path => "/mypeople/file/download.json"} # 파일 및 사진 받기
}

Instance Method Summary collapse

Constructor Details

#initialize(attrs = {}) ⇒ Client

Returns a new instance of Client.



54
55
56
# File 'lib/mypeople/client.rb', line 54

def initialize(attrs = {})
  self.configuration.attributes = attrs
end

Instance Method Details

#buddy_profile(buddy_id) ⇒ Object



118
119
120
121
122
123
124
125
126
# File 'lib/mypeople/client.rb', line 118

def buddy_profile(buddy_id)
  method = APIS[:buddy_profile][:method]
  path = APIS[:buddy_profile][:path]
  data = {
    "buddyId" => buddy_id
  }

  request(method, path, data)
end

#configurationObject



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

def configuration
  @configuration ||= Configuration.new
end

#configure {|configuration| ... } ⇒ Object

Yields:



58
59
60
# File 'lib/mypeople/client.rb', line 58

def configure
  yield configuration if block_given?
end

#download_file(file_id) ⇒ Object



159
160
161
162
163
164
165
166
167
# File 'lib/mypeople/client.rb', line 159

def download_file(file_id)
  method = APIS[:download_file][:method]
  path = APIS[:download_file][:path]
  data = {
    "fileId" => file_id
  }

  request(method, path, data, false)
end

#exit_from_group(group_id) ⇒ Object



149
150
151
152
153
154
155
156
157
# File 'lib/mypeople/client.rb', line 149

def exit_from_group(group_id)
  method = APIS[:exit_from_group][:method]
  path = APIS[:exit_from_group][:path]
  data = {
    "groupId" => group_id
  }

  request(method, path, data)
end

#get(http, path, data, json) ⇒ Object



78
79
80
81
82
83
84
85
86
87
# File 'lib/mypeople/client.rb', line 78

def get(http, path, data, json)
  data = URI.encode_www_form(data)
  resp, body = http.send_request("GET", "#{path}?#{data}")

  if json
    JSON.parse(resp.body)
  else
    resp.body
  end
end

#get_members(group_id) ⇒ Object



128
129
130
131
132
133
134
135
136
# File 'lib/mypeople/client.rb', line 128

def get_members(group_id)
  method = APIS[:get_members][:method]
  path = APIS[:get_members][:path]
  data = {
    "groupId" => group_id
  }

  request(method, path, data)
end

#post(http, path, data, json) ⇒ Object



89
90
91
92
93
94
95
96
97
98
# File 'lib/mypeople/client.rb', line 89

def post(http, path, data, json)
  data = URI.encode_www_form(data)
  resp, body = http.send_request("POST", path, data)

  if json
    JSON.parse(resp.body)
  else
    resp.body
  end
end

#request(method, path, data, json = true) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
# File 'lib/mypeople/client.rb', line 66

def request(method, path, data, json = true)
  http = configuration.http
  data = configuration.data.merge(data)

  case method
  when "GET"
    get(http, path, data, json)
  when "POST"
    post(http, path, data, json)
  end
end

#send_message_to_buddy(buddy_id, content, attach = nil) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/mypeople/client.rb', line 100

def send_message_to_buddy(buddy_id, content, attach = nil) 
  method = APIS[:send_message_to_buddy][:method]
  path = APIS[:send_message_to_buddy][:path]
  data = if content.empty?
           {
             "buddyId" => buddy_id,
             "attach" => attach
           }
         else
           {
             "buddyId" => buddy_id,
             "content" => content
           }
         end

  request(method, path, data)
end

#send_message_to_group(group_id, content, attach = nil) ⇒ Object



138
139
140
141
142
143
144
145
146
147
# File 'lib/mypeople/client.rb', line 138

def send_message_to_group(group_id, content, attach = nil) 
  method = APIS[:send_message_to_group][:method]
  path = APIS[:send_message_to_group][:path]
  data = {
    "groupId" => group_id,
    "content" => content
  }

  request(method, path, data)
end