Class: GAAPI::Response

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

Overview

Holds the result of a Google Analytics query, and provides some methods to present the result in useful ways.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(response) ⇒ Response

Returns a new instance of Response.



45
46
47
# File 'lib/gaapi/response.rb', line 45

def initialize(response)
  @response = response
end

Instance Attribute Details

#responseObject (readonly)

Returns the value of attribute response.



7
8
9
# File 'lib/gaapi/response.rb', line 7

def response
  @response
end

Instance Method Details

#bodyString

Raw body of the response. Typically only used for diagnostic purposes.

Returns:

  • (String)

    The unformatted body of the response.



11
12
13
# File 'lib/gaapi/response.rb', line 11

def body
  response.body
end

#codeString

Raw HTTP status code of the response. Typically only used for diagnostic purposes.

Returns:

  • (String)

    The HTTP response code.



17
18
19
# File 'lib/gaapi/response.rb', line 17

def code
  response.code
end

#csvString

Convert a response from Google Analytics into a comma-separated values format file.

Returns:

  • (String)

    The result of the query formatted as a comma-separated values string.



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/gaapi/response.rb', line 25

def csv
  @csv ||= CSV.generate do |csv|
    reports.each(&:report).each do |report|
      # If there are no dimensions, but totals, we need to put an extra
      # column at the start for the word "Total".
      # I don't see how totals will be different than the metrics if you
      # don't specify dimensions, but whatever.
      totals_column = report.totals? && !report.dimensions? ? [nil] : []
      csv << totals_column + report.headers
      report.rows.each { |row| csv << totals_column + row.to_a }
      next unless report.totals? # Rubocop did this. Not sure I like it.
      csv << ["Totals"] + if !report.dimensions?
                            report.totals
                          else
                            report.totals[1..-1]
                          end
    end
  end
end

#ppString

Return the JSON result in a readable format.

Returns:

  • (String)

    The JSON result formatted in a human-readable way.



51
52
53
# File 'lib/gaapi/response.rb', line 51

def pp
  @pp ||= JSON.pretty_generate(to_json)
end

#reportsArray

The array of reports returned by the query.

Returns:

  • (Array)

    An array of reports.



57
58
59
60
61
62
63
64
65
# File 'lib/gaapi/response.rb', line 57

def reports
  @reports ||= if success?
                 to_json["reports"].map do |report|
                   Report.new(self, report)
                 end
               else
                 []
               end
end

#success?Boolean

Return true if the request was successful.

Returns:

  • (Boolean)

    True if the request was successful (response code 200).



69
70
71
# File 'lib/gaapi/response.rb', line 69

def success?
  code == "200"
end

#to_jsonString

Return the body of the response

Returns:

  • (String)

    JSON-formatted response in a String.



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

def to_json
  @to_json ||= JSON.parse(to_s)
end

#to_sString

Return the body of the response

Returns:

  • (String)

    JSON-formatted response in a String.



81
82
83
# File 'lib/gaapi/response.rb', line 81

def to_s
  response.body
end