module PhraseApp

Constants

API_CLIENT_IDENTIFIER
MULTIPART_BOUNDARY
URL
VERSION

Public Class Methods

handleResponseStatus(resp, expectedStatus) click to toggle source
# 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
handle_times(obj) click to toggle source
# 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
http_send(credentials, req, status) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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 << "Content-Type: #{headers["Content-Type"]}\r\n" if headers["Content-Type"]
    res << "\r\n"
    res << Array(v).join(',')
    res << "\r\n"
  end
end
send_request(credentials, method, path, ctype, data, status) click to toggle source
# 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) click to toggle source
# 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