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.5.0'
- 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
16
17
18
19
20
21
22
|
# File 'lib/phraseapp-ruby.rb', line 16
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
|
# File 'lib/phraseapp-ruby/request_handler.rb', line 150
def self.handleResponseStatus(resp, expectedStatus)
case resp.code.to_i
when expectedStatus
return
when 400
return PhraseApp::RequestErrors::ErrorResponse.new(resp)
when 404
return raise("not found")
when 422
return PhraseApp::RequestErrors::ValidationErrorResponse.new(resp)
when 429
return PhraseApp::RequestErrors::RateLimitingError.new(resp)
else
return raise("unexpected status code (#{resp.code}) received; expected #{expectedStatus}")
end
end
|
.http_send(credentials, req, status) ⇒ Object
109
110
111
112
113
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
|
# File 'lib/phraseapp-ruby/request_handler.rb', line 109
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
24
25
26
27
28
|
# File 'lib/phraseapp-ruby.rb', line 24
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
59
60
61
62
63
64
65
66
67
68
|
# File 'lib/phraseapp-ruby/request_handler.rb', line 59
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"
res << Array(v).join(',')
res << "\r\n"
end
end
|
.send_request(credentials, method, path, ctype, data, status) ⇒ Object
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
|
# File 'lib/phraseapp-ruby/request_handler.rb', line 83
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
70
71
72
73
74
75
76
77
78
79
80
81
|
# File 'lib/phraseapp-ruby/request_handler.rb', line 70
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
|