Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for OAuth2::Response #109

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/garb.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def from_google_analytics(thing)
alias :from_ga :from_google_analytics

def parse_properties(entry)
Hash[entry['dxp$property'].map {|p| [Garb.from_ga(p['name']),p['value']]}]
Hash[entry['property'].map {|p| [Garb.from_ga(p['name']),p['value']]}]
end

def parse_link(entry, rel)
Expand Down
5 changes: 4 additions & 1 deletion lib/garb/management/feed.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
module Garb
module Management
class Feed
require 'xmlsimple'

BASE_URL = "https://www.google.com/analytics/feeds/datasources/ga"

attr_reader :request
Expand All @@ -10,7 +12,8 @@ def initialize(session, path)
end

def parsed_response
@parsed_response ||= JSON.parse(response.body)
# @parsed_response ||= JSON.parse(response.body)
@parsed_response ||= {'feed' => XmlSimple.xml_in(response.body)}
end

def entries
Expand Down
9 changes: 6 additions & 3 deletions lib/garb/report_response.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
module Garb
class ReportResponse
KEYS = ['dxp$metric', 'dxp$dimension']
require 'xmlsimple'

KEYS = ['metric', 'dimension']

def initialize(response_body, instance_klass = OpenStruct)
@data = response_body
Expand Down Expand Up @@ -30,7 +32,7 @@ def parse
end

def entries
feed? ? [parsed_data['feed']['entry']].flatten.compact : []
feed? ? [parsed_data['feed']['aggregates']].flatten.compact : []
end

def parse_total_results
Expand All @@ -42,7 +44,8 @@ def parse_sampled_flag
end

def parsed_data
@parsed_data ||= JSON.parse(@data)
# @parsed_data ||= JSON.parse(@data)
@parsed_data ||= {'feed' => XmlSimple.xml_in(@data)}
end

def feed?
Expand Down
6 changes: 3 additions & 3 deletions lib/garb/request/data.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ def parameters
end

def query_string
parameters.merge!("alt" => format)
# parameters.merge!("alt" => format)
parameter_list = @parameters.map {|k,v| "#{k}=#{v}" }
parameter_list.empty? ? '' : "?#{parameter_list.join('&')}"
end

def format
@format ||= "json" # TODO Support other formats?
# @format ||= "json" # TODO Support other formats?
end

def uri
Expand All @@ -36,7 +36,7 @@ def send_request
oauth_user_request
end

raise ClientError, response.body.inspect unless response.kind_of?(Net::HTTPSuccess)
raise ClientError, response.body.inspect unless response.kind_of?(Net::HTTPSuccess) || (response.respond_to?(:status) && response.status == 200)
response
end

Expand Down
34 changes: 32 additions & 2 deletions test/unit/garb/request/data_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class DataTest < MiniTest::Unit::TestCase
assert_received(data_request, :single_user_request)
end

should "be able to send a request for an oauth user" do
should "be able to send a request for an oauth user (Net::HTTP)" do
@session.stubs(:single_user?).returns(false)
@session.stubs(:oauth_user?).returns(true)
response = mock('Net::HTTPOK') do |m|
Expand All @@ -65,7 +65,21 @@ class DataTest < MiniTest::Unit::TestCase
assert_received(data_request, :oauth_user_request)
end

should "raise if the request is unauthorized" do
should "be able to send a request for an oauth user (OAuth2)" do
@session.stubs(:single_user?).returns(false)
@session.stubs(:oauth_user?).returns(true)
response = mock('OAuth2::Response') do |m|
m.expects(:status).returns(200)
end

data_request = Request::Data.new(@session, 'https://example.com/data', 'key' => 'value')
data_request.stubs(:oauth_user_request).returns(response)
data_request.send_request

assert_received(data_request, :oauth_user_request)
end

should "raise if the request is unauthorized (Net::HTTP)" do
@session.stubs(:single_user?).returns(false)
@session.stubs(:oauth_user?).returns(true)
response = mock('Net::HTTPUnauthorized', :body => 'Error')
Expand All @@ -78,6 +92,22 @@ class DataTest < MiniTest::Unit::TestCase
end
end

should "raise if the request is unauthorized (OAuth2)" do
@session.stubs(:single_user?).returns(false)
@session.stubs(:oauth_user?).returns(true)
response = mock('OAuth2::Response') do |m|
m.expects(:status).returns(401)
m.expects(:body).returns('Error')
end

data_request = Request::Data.new(@session, 'https://example.com/data', 'key' => 'value')
data_request.stubs(:oauth_user_request).returns(response)

assert_raises(Garb::Request::Data::ClientError) do
data_request.send_request
end
end

should "be able to request via the ouath access token" do
access_token = stub(:get => "responseobject")
@session.stubs(:access_token).returns(access_token)
Expand Down