Class: PixivApi::Response

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Memoizable
Defined in:
lib/pixiv_api/response.rb,
lib/pixiv_api/response/tag.rb,
lib/pixiv_api/response/user.rb,
lib/pixiv_api/response/work.rb,
lib/pixiv_api/response/manga.rb,
lib/pixiv_api/response/novel.rb,
lib/pixiv_api/response/action.rb,
lib/pixiv_api/response/friend.rb,
lib/pixiv_api/response/ugoira.rb,
lib/pixiv_api/response/request.rb,
lib/pixiv_api/response/version.rb,
lib/pixiv_api/response/identity.rb,
lib/pixiv_api/response/promotion.rb,
lib/pixiv_api/response/pagination.rb,
lib/pixiv_api/response/illustration.rb,
lib/pixiv_api/response/upload_token.rb,
lib/pixiv_api/response/upload_status.rb,
lib/pixiv_api/response/signup_validation_result.rb

Defined Under Namespace

Classes: Action, Friend, Identity, Illustration, Manga, Novel, Pagination, Promotion, Request, SignupValidationResult, Tag, Ugoira, UploadStatus, UploadToken, User, Version, Work

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(response, attributes = {}) ⇒ Response

Returns a new instance of Response.



111
112
113
114
115
# 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

Instance Attribute Details

#attributesObject (readonly) Also known as: to_h

Returns the value of attribute attributes.



27
28
29
# File 'lib/pixiv_api/response.rb', line 27

def attributes
  @attributes
end

#responseObject (readonly)

Returns the value of attribute response.



27
28
29
# File 'lib/pixiv_api/response.rb', line 27

def response
  @response
end

Class Method Details

.attr_reader(*attributes) ⇒ Object



37
38
39
40
41
42
# 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) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# 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) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
# 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) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
# 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) ⇒ Object



76
77
78
79
80
81
82
83
84
# 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) ⇒ Object



33
34
35
# File 'lib/pixiv_api/response.rb', line 33

def from_response(response, *attributes)
  new(response, *attributes)
end

Instance Method Details

#[](method) ⇒ Object



117
118
119
120
121
# File 'lib/pixiv_api/response.rb', line 117

def [](method)
  public_send(method.to_sym)
rescue NoMethodError
  nil
end