Sha256: 55752adced09310e61bfbd1fe5f96ceabbe36cb02cc20ede61556f40ac27e48d

Contents?: true

Size: 1.71 KB

Versions: 11

Compression:

Stored size: 1.71 KB

Contents

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

      def service_id
        @options[:service_id]
      end

      def resource
        @options[:resource]
      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("resource")   if @options[:resource].blank?
        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 rest_path
        "/v2/rest/#{config.site_id}/reports/calls/#{resource}/service/#{service_id}"
      end

      def url
        uri        = URI::HTTP.build(host: config.host, path: rest_path, query: query_params)
        uri.scheme = "https"
        uri.to_s
      end
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
masheri-0.6.1 lib/masheri/rest_client/query.rb
masheri-0.6.0 lib/masheri/rest_client/query.rb
masheri-0.5.6 lib/masheri/rest_client/query.rb
masheri-0.5.5 lib/masheri/rest_client/query.rb
masheri-0.5.2 lib/masheri/rest_client/query.rb
masheri-0.5.1 lib/masheri/rest_client/query.rb
masheri-0.5.0 lib/masheri/rest_client/query.rb
masheri-0.4.0 lib/masheri/rest_client/query.rb
masheri-0.3.10 lib/masheri/rest_client/query.rb
masheri-0.3.7 lib/masheri/rest_client/query.rb
masheri-0.3.6 lib/masheri/rest_client/query.rb