Sha256: 0b0f01f7ab33c7d8896e3457b72f933c71029e0d31e2daf67cd167f27d836d03

Contents?: true

Size: 1 KB

Versions: 1

Compression:

Stored size: 1 KB

Contents

# frozen_string_literal: true

require 'dribbble/utils'
require 'dribbble/utils/has_children'

module Dribbble
  class Base
    include Dribbble::Utils
    extend Dribbble::Utils

    include Dribbble::Utils::HasChildren

    attr_reader :token, :dribbble_url

    def initialize(token, json, dribbble_url = '')
      @token = token

      @raw = json.is_a?(Hash) ? json : JSON.parse(json)
      @dribbble_url = build_dribbble_url(@raw['id'].to_s, dribbble_url)

      @raw.each do |k, _v|
        define_singleton_method(k) { @raw[k] } unless respond_to?(k)
      end
    end

    def self.batch_new(token, json, kind = nil, url = '')
      json = JSON.parse json unless json.is_a? Hash
      json.map do |obj|
        if kind
          new token, obj[kind], url
        else
          new token, obj, url
        end
      end
    end

    private

    def build_dribbble_url(id, dribbble_url)
      if dribbble_url.end_with?(id)
        dribbble_url
      else
        "#{dribbble_url}/#{id}"
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
dribbble-2.0.0 lib/dribbble/base.rb