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.



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

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

Instance Method Details

#buddy_profile(buddy_id) ⇒ Object



109
110
111
112
113
114
115
116
117
# File 'lib/mypeople/client.rb', line 109

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



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

def configuration
  @configuration ||= Configuration.new
end

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

Yields:



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

def configure
  yield configuration if block_given?
end

#download_file(file_id, file_name) ⇒ Object



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

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

  request(method, path, data)
end

#exit_from_group(group_id) ⇒ Object



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

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) ⇒ Object



77
78
79
80
81
82
# File 'lib/mypeople/client.rb', line 77

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

  resp.body
end

#get_members(group_id) ⇒ Object



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

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) ⇒ Object



84
85
86
87
88
89
# File 'lib/mypeople/client.rb', line 84

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

  resp.body
end

#request(method, path, data) ⇒ Object



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

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

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

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



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/mypeople/client.rb', line 91

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



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

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