Class: GAAPI::Response
- Inherits:
-
Object
- Object
- GAAPI::Response
- 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
-
#response ⇒ Object
readonly
Returns the value of attribute response.
Instance Method Summary collapse
-
#body ⇒ String
Raw body of the response.
-
#code ⇒ String
Raw HTTP status code of the response.
-
#csv ⇒ String
Convert a response from Google Analytics into a comma-separated values format file.
-
#initialize(response) ⇒ Response
constructor
A new instance of Response.
-
#pp ⇒ String
Return the JSON result in a readable format.
-
#reports ⇒ Array
The array of reports returned by the query.
-
#success? ⇒ Boolean
Return true if the request was successful.
-
#to_json ⇒ String
Return the body of the response.
-
#to_s ⇒ String
Return the body of the response.
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
#response ⇒ Object (readonly)
Returns the value of attribute response.
7 8 9 |
# File 'lib/gaapi/response.rb', line 7 def response @response end |
Instance Method Details
#body ⇒ String
Raw body of the response. Typically only used for diagnostic purposes.
11 12 13 |
# File 'lib/gaapi/response.rb', line 11 def body response.body end |
#code ⇒ String
Raw HTTP status code of the response. Typically only used for diagnostic purposes.
17 18 19 |
# File 'lib/gaapi/response.rb', line 17 def code response.code end |
#csv ⇒ String
Convert a response from Google Analytics into a comma-separated values format file.
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 |
#pp ⇒ String
Return the JSON result in a readable format.
51 52 53 |
# File 'lib/gaapi/response.rb', line 51 def pp @pp ||= JSON.pretty_generate(to_json) end |
#reports ⇒ Array
The array of reports returned by the query.
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.
69 70 71 |
# File 'lib/gaapi/response.rb', line 69 def success? code == "200" end |
#to_json ⇒ String
Return the body of the response
75 76 77 |
# File 'lib/gaapi/response.rb', line 75 def to_json @to_json ||= JSON.parse(to_s) end |
#to_s ⇒ String
Return the body of the response
81 82 83 |
# File 'lib/gaapi/response.rb', line 81 def to_s response.body end |