Module: PhraseApp
- Defined in:
- lib/auth.rb,
lib/version.rb,
lib/phraseapp-ruby.rb,
lib/phraseapp-ruby.rb,
lib/request_handler.rb
Defined Under Namespace
Modules: Auth, ParamsHelpers, RequestErrors, RequestParams, ResponseObjects
Classes: Client
Constant Summary
collapse
- URL =
"https://api.phraseapp.com"
- VERSION =
'1.0.17'
- MULTIPART_BOUNDARY =
"{PHRASE!!EOF}"
- API_CLIENT_IDENTIFIER =
"PhraseApp Ruby " + VERSION
Class Method Summary
collapse
-
.handle_times(obj) ⇒ Object
-
.handleResponseStatus(resp, expectedStatus) ⇒ Object
-
.is_a_date?(str) ⇒ Boolean
-
.multipart(hash) ⇒ Object
-
.send(req, status) ⇒ Object
-
.send_request(method, path, ctype, data, status) ⇒ Object
-
.send_request_paginated(method, path_with_query, ctype, body, status, page, per_page) ⇒ Object
Class Method Details
.handle_times(obj) ⇒ Object
12
13
14
15
16
17
18
|
# File 'lib/phraseapp-ruby.rb', line 12
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
|
# File 'lib/request_handler.rb', line 151
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::RateLimitError.new(resp)
if err != nil
return err
end
return e
else
return raise("unexpected status code (#{resp.code}) received; expected #{expectedStatus}")
end
end
|
.is_a_date?(str) ⇒ Boolean
20
21
22
23
24
|
# File 'lib/phraseapp-ruby.rb', line 20
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/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 << "#{v}\r\n"
res
end
end
|
.send(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
149
|
# File 'lib/request_handler.rb', line 109
def self.send(req, status)
err = PhraseApp::Auth.authenticate(req)
if err != nil
return nil, err
end
req["User-Agent"] = API_CLIENT_IDENTIFIER
uri = URI.parse(PhraseApp::Auth.host)
if PhraseApp::Auth.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 PhraseApp::Auth::skip_ssl_verification
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
else
http.verify_mode = OpenSSL::SSL::VERIFY_PEER
end
end
if PhraseApp::Auth.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
|
.send_request(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/request_handler.rb', line 83
def self.send_request(method, path, ctype, data, status)
req = Net::HTTPGenericRequest.new(method,
Module.const_get("Net::HTTP::#{method.capitalize}::REQUEST_HAS_BODY"),
Module.const_get("Net::HTTP::#{method.capitalize}::RESPONSE_HAS_BODY"),
path)
if PhraseApp::Auth.debug
puts "-------"
puts "data:"
puts data.inspect
end
req.body = data
if ctype != ""
req["Content-Type"] = ctype
end
resp, err = send(req, status)
return resp, err
end
|
.send_request_paginated(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/request_handler.rb', line 70
def self.send_request_paginated(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(method, path, ctype, body, status)
end
|