class PixivApi::Response

Attributes

attributes[R]
response[R]
to_h[R]

Public Class Methods

attr_reader(*attributes) click to toggle source
# File lib/pixiv_api/response.rb, line 37
def attr_reader(*attributes)
  attributes.each do |attr|
    define_attribute_method(attr)
    define_predicate_method(attr)
  end
end
define_attribute_method(key1, klass = nil, key2 = nil) click to toggle source
# File lib/pixiv_api/response.rb, line 44
def define_attribute_method(key1, klass = nil, key2 = nil)
  define_method(key1) do
    if klass.nil?
      @attributes[key1]
    else
      if @attributes[key1]
        dup = attributes_for_object(key1, key2)
        klass.new(@response, dup)
      else
        nil
      end
    end
  end

  memoize(key1)
end
define_attributes_method(key1, klass = nil, key2 = nil) click to toggle source
# File lib/pixiv_api/response.rb, line 61
def define_attributes_method(key1, klass = nil, key2 = nil)
  define_method(key1) do
    if klass.nil?
      @attributes[key1]
    else
      @attributes[key1].map do |attr|
        dup = attributes_for_object(key1, key2)
        klass.new(@response, dup)
      end
    end
  end

  memoize(key1)
end
define_blob_method(key) click to toggle source
# File lib/pixiv_api/response.rb, line 86
def define_blob_method(key)
  method_name = :"blob_#{key}"

  define_method method_name do
    fetch(key, {}).each_with_object({}) do |(key, value), memo|
      memo[key] = PixivApi::PixivBlob.new(value)
    end
  end

  memoize(method_name)
end
define_time_method(*attributes) click to toggle source
# File lib/pixiv_api/response.rb, line 76
def define_time_method(*attributes)
  attributes.each do |attr|
    define_method(attr) do
      Time.parse(@attributes[attr])
    end

    memoize(attr)
  end
end
from_response(response, *attributes) click to toggle source
# File lib/pixiv_api/response.rb, line 33
def from_response(response, *attributes)
  new(response, *attributes)
end
new(response, attributes = {}) click to toggle source
# File lib/pixiv_api/response.rb, line 111
def initialize(response, attributes = {})
  @response = response
  @request = PixivApi::Response::Request.new(response)
  @attributes = attributes.with_indifferent_access
end

Private Class Methods

define_predicate_method(key1, key2 = key1) click to toggle source
# File lib/pixiv_api/response.rb, line 100
def define_predicate_method(key1, key2 = key1)
  predicate_method = :"#{key1}?"

  define_method(predicate_method) do ||
    !!@attributes[key2]
  end

  memoize(predicate_method)
end

Public Instance Methods

[](method) click to toggle source
# File lib/pixiv_api/response.rb, line 117
def [](method)
  public_send(method.to_sym)
rescue NoMethodError
  nil
end

Private Instance Methods

attributes_for_object(key1, key2=nil) click to toggle source
# File lib/pixiv_api/response.rb, line 125
def attributes_for_object(key1, key2=nil)
  if key2.nil?
    @attributes[key1]
  else
    attributes = @attributes.dup
    attributes.delete(key1).merge(key2 => attributes)
  end
end