Class: Phrase::Response

Inherits:
Object
  • Object
show all
Defined in:
lib/phrase/response.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data, headers) ⇒ Response

Returns a new instance of Response.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/phrase/response.rb', line 5

def initialize(data, headers)
  @data = data
  @paginated = false
  @next_page = nil

  link_headers = headers["link"]
  if link_headers
    @paginated = true
    parsed_links = LinkHeaderParser.parse(link_headers, base: 'https://api.phrase.com').group_by_relation_type
    next_page_link = parsed_links[:next]&.first
    if next_page_link
      @next_page = CGI.parse(URI.parse(next_page_link.target_uri).query)["page"]&.first&.to_i
    end
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object



21
22
23
24
25
26
27
# File 'lib/phrase/response.rb', line 21

def method_missing(method, *args, &block)
  if data.respond_to?(method)
    data.send(method, *args, &block)
  else
    super
  end
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



3
4
5
# File 'lib/phrase/response.rb', line 3

def data
  @data
end

Instance Method Details

#next_pageObject



29
30
31
# File 'lib/phrase/response.rb', line 29

def next_page
  @next_page
end

#next_page?Boolean

Returns:

  • (Boolean)


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

def next_page?
  paginated? && !next_page.nil?
end

#paginated?Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/phrase/response.rb', line 37

def paginated?
  @paginated
end