summaryrefslogtreecommitdiff
path: root/lib/nickserver/nicknym/source.rb
blob: 9c2d8c43d0ebad30494f21edb4864ee0e9879adb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
require 'nickserver/source'
require 'nickserver/response'

module Nickserver
  module Nicknym
    class Source < Nickserver::Source
      # port we expect the remote nicknym to listen on
      PORT = 6425

      def available_for?(domain)
        status, body = adapter.get "https://#{domain}/provider.json",
                                   rescue: 'failed to connect: getaddrinfo'
        status == 200 && provider_with_mx?(body)
      end

      def query(email)
        status, body = nicknym_get email.domain, address: email.to_s
        Nickserver::Response.new(status, body)
      end

      protected

      def nicknym_get(domain, query = {})
        url = "https://nicknym.#{domain}:#{PORT}"
        adapter.get(url, query: query)
      end

      def provider_with_mx?(provider_json)
        provider = JSON.parse provider_json
        services = provider['services'] || []
        services.include?('mx')
      rescue JSON::ParserError
        return false
      end
    end
  end
end