Sha256: 7cb60ccc5cfada54a6cf614df4ea9e9cb1db902ac4e8c8d5b0f643bcba95f082
Contents?: true
Size: 2 KB
Versions: 4
Compression:
Stored size: 2 KB
Contents
module ZendeskAppsTools module APIConnection DEFAULT_URL_TEMPLATE = 'https://%s.zendesk.com/' # taken from zendesk/lib/vars.rb SUBDOMAIN_VALIDATION_PATTERN = /^[a-z0-9][a-z0-9\-]{1,}[a-z0-9]$/i ZENDESK_URL_VALIDATION_PATTERN = /^(https?):\/\/[a-z0-9]+(([\.]|[\-]{1,2})[a-z0-9]+)*\.([a-z]{2,16}|[0-9]{1,3})((:[0-9]{1,5})?(\/?|\/.*))?$/ix EMAIL_REGEX = /^([\w\.%\+\-]+)@([\w\-]+\.)+([\w]{2,})(\/token)?$/i EMAIL_ERROR_MSG = 'Please enter a valid email address.' PROMPT_FOR_URL = 'Enter your Zendesk subdomain or full URL (including protocol):' URL_ERROR_MSG = [ 'URL error. Example URL: https://mysubdomain.zendesk.com', 'If you are using a full URL, follow the example format shown above.', 'If you are using a subdomain, ensure that it contains only valid characters (a-z, A-Z, 0-9, and hyphens).' ].join('\n') def prepare_api_auth @subdomain ||= cache.fetch('subdomain') || get_value_from_stdin(PROMPT_FOR_URL) say_error_and_exit URL_ERROR_MSG unless valid_subdomain? || valid_full_url? @username ||= cache.fetch('username', @subdomain) || get_value_from_stdin('Enter your username:') say_error_and_exit EMAIL_ERROR_MSG unless valid_email? @password ||= cache.fetch('password', @subdomain) || get_password_from_stdin('Enter your password:') end def get_connection(encoding = :url_encoded) require 'net/http' require 'faraday' prepare_api_auth unless @subdomain && @username && @password Faraday.new full_url do |f| f.request encoding if encoding f.adapter :net_http f.basic_auth @username, @password end end private def full_url valid_full_url? ? @subdomain : (DEFAULT_URL_TEMPLATE % @subdomain) end def valid_full_url? !!ZENDESK_URL_VALIDATION_PATTERN.match(@subdomain) end def valid_subdomain? !!SUBDOMAIN_VALIDATION_PATTERN.match(@subdomain) end def valid_email? !!EMAIL_REGEX.match(@username) end end end
Version data entries
4 entries across 4 versions & 1 rubygems