Class: GoogleAnalytics::Client
- Inherits:
-
Object
- Object
- GoogleAnalytics::Client
- Defined in:
- lib/google_analytics/client.rb
Instance Method Summary collapse
- #api_request(path, method, args) ⇒ Object
- #get(path, args = nil) ⇒ Object
-
#initialize(username, password) ⇒ Client
constructor
A new instance of Client.
- #login! ⇒ Object
- #post(path, args = nil) ⇒ Object
- #request(url, method = :get, data = nil) ⇒ Object
Constructor Details
#initialize(username, password) ⇒ Client
Returns a new instance of Client.
16 17 18 |
# File 'lib/google_analytics/client.rb', line 16 def initialize(username, password) @username, @password = username, password end |
Instance Method Details
#api_request(path, method, args) ⇒ Object
49 50 51 52 53 54 55 56 |
# File 'lib/google_analytics/client.rb', line 49 def api_request(path, method, args) login! unless @auth url = URI.parse([BASE_URL, path].join) resp = request(url, method, args) Nokogiri::XML(resp) end |
#get(path, args = nil) ⇒ Object
41 42 43 |
# File 'lib/google_analytics/client.rb', line 41 def get(path, args=nil) api_request(path, :get, args) end |
#login! ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/google_analytics/client.rb', line 20 def login! data = { :accountType => 'GOOGLE_OR_HOSTED', :service => 'analytics', :Email => @username, :Passwd => @password } begin resp = request(URI.parse(AUTH_URL), :post, data) if resp =~ /^Auth\=(.+)$/ @auth = $1 else raise NotAuthorized, 'you cannot access this google account' end rescue GoogleAnalytics::ResponseError raise NotAuthorized, 'you cannot access this google account' end end |
#post(path, args = nil) ⇒ Object
45 46 47 |
# File 'lib/google_analytics/client.rb', line 45 def post(path, args=nil) api_request(path, :post, args) end |
#request(url, method = :get, data = nil) ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/google_analytics/client.rb', line 58 def request(url, method=:get, data=nil) case method when :get url.query = encode(data) if data req = Net::HTTP::Get.new(url.request_uri) when :post req = Net::HTTP::Post.new(url.request_uri) req.body = encode(data) if data req.content_type = 'application/x-www-form-urlencoded' end req['Authorization'] = "GoogleLogin auth=#{@auth}" if @auth puts url.to_s http = Net::HTTP.new(url.host, url.port) http.use_ssl = (url.port == 443) res = http.start() { |conn| conn.request(req) } raise ResponseError, "#{res.code} #{res.}: #{res.body}" unless res.code == '200' res.body end |