Sha256: 0640db50f66abea1c1dae23ccef5e3707f17a1a1f2301b4e619473c4a81bd0fa

Contents?: true

Size: 1.22 KB

Versions: 2

Compression:

Stored size: 1.22 KB

Contents

module Pige::Client
  class Label < Resource
    attr_accessor :timestamp, :name
    time_attribute :timestamp

    def older_than?(date)
      self.timestamp < date
    end

    def self.all(options = {})
      [].tap do |labels|
        page = 1
        empty_page = false

        begin
          labels_in_page = page(page, options)
          unless labels_in_page.empty?
            labels.push *labels_in_page
            page += 1
          else
            empty_page = true
          end
        end until empty_page
      end
    end

    def self.by_name(name, options = {})
      options = options.merge(term: name)
      attributes = get("#{Pige::Client.box_url}/sources/1/labels.json", query: options).first
      Label.new attributes if attributes
    end

    def self.older_than(date)
      all(before: date)
    end

    def self.page(number, options = {})
      additionnal_query = options.map do |k,v|
        "#{k}=#{URI.escape(v.to_s)}" if v
      end.join('&')
      additionnal_query = "&#{additionnal_query}" unless additionnal_query.empty?

      get("#{Pige::Client.box_url}/sources/1/labels.json?page=#{number}#{additionnal_query}").map do |attributes|
        Label.new attributes["label"]
      end
    end

  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
tryphon-pige-client-1.0.1 lib/pige/client/label.rb
tryphon-pige-client-1.0 lib/pige/client/label.rb