Class: TinyAppstoreConnect::Response

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/tiny_appstore_connect/response.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(response, client) ⇒ Response

Returns a new instance of Response.



15
16
17
18
# File 'lib/tiny_appstore_connect/response.rb', line 15

def initialize(response, client)
  @response = response
  @client = client
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



13
14
15
# File 'lib/tiny_appstore_connect/response.rb', line 13

def client
  @client
end

#responseObject (readonly)

Returns the value of attribute response.



13
14
15
# File 'lib/tiny_appstore_connect/response.rb', line 13

def response
  @response
end

Instance Method Details

#all_pages(flatten: false, &block) ⇒ Object



74
75
76
77
# File 'lib/tiny_appstore_connect/response.rb', line 74

def all_pages(flatten: false, &block)
  responses = next_pages(count: nil, &block)
  flatten ? responses.flat_map(&:to_models) : responses
end

#each(&block) ⇒ Object



90
91
92
93
94
# File 'lib/tiny_appstore_connect/response.rb', line 90

def each(&block)
  to_models.each do |model|
    yield(model)
  end
end

#next_page {|url| ... } ⇒ Object

Yields:

  • (url)


47
48
49
50
51
52
53
# File 'lib/tiny_appstore_connect/response.rb', line 47

def next_page(&block)
  url = next_url
  return if url.nil?
  return client.get(url) unless block_given?

  yield(url)
end

#next_pages(count: 1, &block) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/tiny_appstore_connect/response.rb', line 55

def next_pages(count: 1, &block)
  count = 0 if !count.nil? && count < 0
  responses = [self]
  counter = 0

  resp = self
  loop do
    resp = resp.next_page(&block)
    break if resp.nil?

    responses << resp
    counter += 1

    break if !count.nil? && counter >= count
  end

  responses
end

#next_urlObject



40
41
42
43
44
45
# File 'lib/tiny_appstore_connect/response.rb', line 40

def next_url
  return if body.nil?

  links = body['links'] || {}
  links['next']
end

#rateObject



26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/tiny_appstore_connect/response.rb', line 26

def rate
  return {} unless value = response.headers[:x_rate_limit]

  value.split(';').inject({}) do |r, q|
    k, v = q.split(':')
    case k
    when 'user-hour-lim'
      r.merge!(limit: v)
    when 'user-hour-rem'
      r.merge!(remaining: v)
    end
  end
end

#request_urlObject



22
23
24
# File 'lib/tiny_appstore_connect/response.rb', line 22

def request_url
  @response.env.url
end

#to_modelObject



79
80
81
# File 'lib/tiny_appstore_connect/response.rb', line 79

def to_model
  to_models.first
end

#to_modelsObject



83
84
85
86
87
88
# File 'lib/tiny_appstore_connect/response.rb', line 83

def to_models
  return [] if body.nil?

  model_or_models = Parser.parse(response, rate)
  [model_or_models].flatten
end