Sha256: 43d65cdde6f6222122ea13e1b344ac32523c73463a825a7c78e75f2253ff20a9

Contents?: true

Size: 1.41 KB

Versions: 8

Compression:

Stored size: 1.41 KB

Contents

# frozen_string_literal: true

module Mihari
  module Web
    module Endpoints
      #
      # IP address API endpoint
      #
      class IPAddresses < Grape::API
        namespace :ip_addresses do
          desc "Get IP address data", {
            success: Entities::IPAddress,
            failure: [
              { code: 404, model: Entities::ErrorMessage },
              { code: 422, model: Entities::ErrorMessage }
            ],
            summary: "Get IP address data"
          }
          params do
            requires :ip, type: String
          end
          get "/:ip", requirements: { ip: %r{[^/]+} } do
            ip = params[:ip].to_s
            result = Services::IPGetter.result(ip)
            if result.success?
              value = result.value!
              return present(
                {
                  country_code: value.country_code,
                  asn: value.asn,
                  loc: value.loc
                },
                with: Entities::IPAddress
              )
            end

            failure = result.failure
            case failure
            when Mihari::StatusError
              error!({ message: "IP:#{ip} not found" }, failure.status_code) if failure.status_code == 404
              error!({ message: "IP format invalid" }, failure.status_code) if failure.status_code == 422
            end
            raise failure
          end
        end
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
mihari-7.3.2 lib/mihari/web/endpoints/ip_addresses.rb
mihari-7.3.1 lib/mihari/web/endpoints/ip_addresses.rb
mihari-7.3.0 lib/mihari/web/endpoints/ip_addresses.rb
mihari-7.2.0 lib/mihari/web/endpoints/ip_addresses.rb
mihari-7.1.3 lib/mihari/web/endpoints/ip_addresses.rb
mihari-7.1.2 lib/mihari/web/endpoints/ip_addresses.rb
mihari-7.1.1 lib/mihari/web/endpoints/ip_addresses.rb
mihari-7.1.0 lib/mihari/web/endpoints/ip_addresses.rb