Sha256: 734a4ed0d2e986103281d4bea29e9663c103069f76179dd074907aa9490c50bc

Contents?: true

Size: 1.57 KB

Versions: 2

Compression:

Stored size: 1.57 KB

Contents

module Masheri
  class RestClient
    class Query
      def initialize(options)
        @options  = options
      end

      def format
        @options[:format] || "csv"
      end

      def limit
        @options[:limit] || 1000
      end

      def start_date
        @options[:start_date].strftime("%Y-%m-%dT00:00:00Z")
      end

      def end_date
        @options[:end_date].strftime("%Y-%m-%dT00:00:00Z")
      end

      def check_params!
        raise QueryParamMissing.new("end_date")   if @options[:end_date].blank?
        raise QueryParamMissing.new("start_date") if @options[:start_date].blank?
      end

      def check_dates!
        if @options[:end_date] - @options[:start_date] > 7.days
          raise InvalidDateRange.new(@options[:start_date], @options[:end_date])
        end
      end

      def params
        check_params!
        check_dates!

        params = {
          start_date: start_date,
          end_date:   end_date,
          format:     format,
          limit:      limit,
          apikey:     config.key,
          sig:        config.signature
        }
      end

      def config
        Masheri.config
      end

      def query_params
        URI.encode_www_form(params).gsub("%3A", ":")
      end

      def service_id
        @options[:service_id]
      end

      def resource
        @options[:resource]
      end

      def path
        "/v2/rest/#{config.site_id}/reports/calls/#{resource}/service/#{service_id}"
      end

      def url
        URI::HTTP.build(host: config.host, path: path, query: query_params).to_s
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
masheri-0.3.5 lib/masheri/rest_client/query.rb
masheri-0.3.4 lib/masheri/rest_client/query.rb