Sha256: 6aebc033ffb10cfcec66ffedc77c8ed9b7c78caa2aa7b6434608ca1397c2437d

Contents?: true

Size: 1.65 KB

Versions: 1

Compression:

Stored size: 1.65 KB

Contents

# frozen_string_literal: true

require 'yaml'
require 'hashie/mash'

class Hphones
  ##
  # Stores information for endpoints.
  #
  class Endpoint
    class MissingParameterError < StandardError; end

    # The path to the endpoints file.
    ENDPOINTS_PATH = File.join('data', 'endpoints.yml')

    class << self
      def lookup(key)
        !endpoints[key.to_s].nil?
      end

      def endpoints
        @endpoints ||= load_endpoints_from_file
      end

      private

      def load_endpoints_from_file
        YAML.load_file ENDPOINTS_PATH
      end
    end

    def initialize(key, api, params = {})
      @key = key.to_s
      @api = api
      @params = Hashie::Mash.new params
    end

    def fetch
      api_params = compile_params
      req = Hphones::Request.new(api)
      req.send endpoint_info['method'], api_params
    end

    private

    attr_reader :key, :api, :params

    def endpoint_info
      @endpoint_info ||= self.class.endpoints[key]
    end

    def compile_params
      param_specs = endpoint_info['params']
      pairs = param_specs.map { |spec| compile_pair spec }
      Hash[pairs.compact]
    end

    def compile_pair(spec)
      spec['value'] ? default_pair_for(spec) : merged_pair_for(spec)
    end

    def default_pair_for(spec)
      [spec['key'], spec['value']]
    end

    def merged_pair_for(spec)
      key = spec['key']
      spec['required'] ? required_pair_for(key) : optional_pair_for(key)
    end

    def required_pair_for(key)
      params[key] ? [key, params[key]] : raise(MissingParameterError, "Parameter missing: #{key}")
    end

    def optional_pair_for(key)
      params[key] ? [key, params[key]] : nil
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
hphones-ruby-0.0.1 lib/hphones/endpoint.rb