Module: PhraseApp
- Defined in:
- lib/phraseapp-ruby.rb,
lib/phraseapp-ruby.rb,
lib/phraseapp-ruby/auth.rb,
lib/phraseapp-ruby/version.rb,
lib/phraseapp-ruby/request_handler.rb
Defined Under Namespace
Modules: Auth, ParamsHelpers, RequestErrors, RequestParams, ResponseObjects
Classes: Client
Constant Summary
collapse
- MULTIPART_BOUNDARY =
"{PHRASE!!EOF}"
- URL =
"https://api.phraseapp.com"
- VERSION =
'1.2.3'
- API_CLIENT_IDENTIFIER =
"PhraseApp Ruby #{VERSION}"
Class Method Summary
collapse
-
.handle_times(obj) ⇒ Object
-
.handleResponseStatus(resp, expectedStatus) ⇒ Object
-
.http_send(credentials, req, status) ⇒ Object
-
.is_a_date?(str) ⇒ Boolean
-
.multipart(hash) ⇒ Object
-
.send_request(credentials, method, path, ctype, data, status) ⇒ Object
-
.send_request_paginated(credentials, method, path_with_query, ctype, body, status, page, per_page) ⇒ Object
Class Method Details
.handle_times(obj) ⇒ Object
13
14
15
16
17
18
19
|
# File 'lib/phraseapp-ruby.rb', line 13
def self.handle_times(obj)
obj.each_pair do |k, v|
if is_a_date?(v)
obj.send(:"#{k}=", DateTime.parse(v))
end
end
end
|
.handleResponseStatus(resp, expectedStatus) ⇒ Object
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
|
# File 'lib/phraseapp-ruby/request_handler.rb', line 155
def self.handleResponseStatus(resp, expectedStatus)
case resp.code.to_i
when expectedStatus
return
when 400
e = PhraseApp::RequestErrors::ErrorResponse.new(resp)
return e
when 404
return raise("not found")
when 422
e = PhraseApp::RequestErrors::ValidationErrorResponse.new(resp)
if e != nil
return e
end
return e
when 429
e, err = PhraseApp::RequestErrors::RateLimitingError.new(resp)
if err != nil
return err
end
return e
else
return raise("unexpected status code (#{resp.code}) received; expected #{expectedStatus}")
end
end
|
.http_send(credentials, req, status) ⇒ Object
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
|
# File 'lib/phraseapp-ruby/request_handler.rb', line 114
def self.http_send(credentials, req, status)
err = credentials.authenticate(req)
if err != nil
return nil, err
end
req["User-Agent"] = API_CLIENT_IDENTIFIER
uri = URI.parse(credentials.host)
if credentials.debug
puts "uri:"
puts uri.inspect
end
http = Net::HTTP.new(uri.host, uri.port)
if uri.is_a?(URI::HTTPS)
http.use_ssl = true
if credentials.skip_ssl_verification
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
else
http.verify_mode = OpenSSL::SSL::VERIFY_PEER
end
end
if credentials.debug
puts "method:"
puts req.method
puts "path:"
puts req.path
puts "body:"
puts req.body.inspect
puts "-------"
end
resp = http.request(req)
err = handleResponseStatus(resp, status)
return resp, err
end
|
.is_a_date?(str) ⇒ Boolean
21
22
23
24
25
|
# File 'lib/phraseapp-ruby.rb', line 21
def self.is_a_date?(str)
if !str.nil? && str.is_a?(String)
str.match(/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}/)
end
end
|
.multipart(hash) ⇒ Object
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
|
# File 'lib/phraseapp-ruby/request_handler.rb', line 57
def self.multipart(hash)
hash.inject("") do |res, (k, v)|
res << "--#{PhraseApp::MULTIPART_BOUNDARY}\r\n"
res << "Content-Disposition: form-data; name=\"#{k}\"\r\n"
res << "\r\n"
if v.is_a?(Array)
v.each do |vv|
res << vv+","
end
res << "\r\n"
else
res << "#{v}\r\n"
end
res
end
end
|
.send_request(credentials, method, path, ctype, data, status) ⇒ Object
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
|
# File 'lib/phraseapp-ruby/request_handler.rb', line 88
def self.send_request(credentials, method, path, ctype, data, status)
req = Net::HTTPGenericRequest.new(method,
Net::HTTP.const_get(method.capitalize).const_get(:REQUEST_HAS_BODY),
Net::HTTP.const_get(method.capitalize).const_get(:RESPONSE_HAS_BODY),
path)
if credentials.debug
puts "-------"
puts "data:"
puts data.inspect
end
req.body = data
if ctype != ""
req["Content-Type"] = ctype
end
resp, err = http_send(credentials, req, status)
return resp, err
end
|
.send_request_paginated(credentials, method, path_with_query, ctype, body, status, page, per_page) ⇒ Object
75
76
77
78
79
80
81
82
83
84
85
86
|
# File 'lib/phraseapp-ruby/request_handler.rb', line 75
def self.send_request_paginated(credentials, method, path_with_query, ctype, body, status, page, per_page)
uri = URI.parse(path_with_query)
hash = if uri.query then CGI::parse(uri.query) else {} end
hash["page"] = page
hash["per_page"] = per_page
query_str = URI.encode_www_form(hash)
path = [uri.path, query_str].compact.join('?')
return send_request(credentials, method, path, ctype, body, status)
end
|