Created
May 12, 2014 19:11
-
-
Save raystorm/72a0736d8a3059ce8639 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module RubyStats | |
require 'google/api_client' | |
require 'google/api_client/client_secrets' | |
require 'google/api_client/auth/installed_app' | |
require 'openssl' | |
class GitHub_Archive | |
OpenSSL::SSL::VERIFY_PEER = OpenSSL::SSL::VERIFY_NONE | |
@client | |
@bigQuery | |
@project_id = "#############" | |
def initialize | |
@client = Google::APIClient.new(:application_name => 'RubyStats', | |
:application_version => '0.0.1' ) | |
key = Google::APIClient::KeyUtils.load_from_pkcs12('client.p12', 'ZZZZZZZZZZ') | |
@client.authorization = Signet::OAuth2::Client.new( | |
:token_credential_uri => 'https://accounts.google.com/o/oauth2/token', | |
:audience => 'https://accounts.google.com/o/oauth2/token', | |
:scope => 'https://www.googleapis.com/auth/bigquery.readonly', | |
:issuer => '#############-ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ@developer.gserviceaccount.com', | |
:signing_key => key) | |
@client.authorization.fetch_access_token! | |
# Initialize BigQuery API. Note this will make a request to the | |
# discovery service every time, so be sure to use serialization | |
# in your production code. Check the samples for more details. | |
#@bigQuery = @client.discovered_api('bigquery', 'v2') | |
doc = File.read('bigQueryAPI.json') | |
@bigQuery = @client.register_discovery_document('bigquery', 'v2', doc) | |
end | |
def get_averages | |
initialize | |
begin | |
result = @client.execute!( | |
#:api_method => @bigQuery.query, | |
#:api_method => @bigQuery.queries, | |
#:api_method => @bigQuery.Jobs.query, | |
#:api_method => "queries", #@bigQuery.batch_path.query, | |
#:api_method => @bigQuery.jobs.query, | |
#:api_method => @bigQuery.resources.jobs.query, | |
#:api_method => @bigQuery.Resources.jobs.query, | |
#:api_method => @bigQuery.Jobs.query, | |
:api_method => @bigQuery.jobs.query, | |
:body_object => { "query" => "SELECT count(DISTINCT repository_name) as repository_total, " + | |
"count(payload_commit) as commits_total, " + | |
"count(DISTINCT repository_name) / count(payload_commit) as average, " + | |
"FROM [githubarchive:github.timeline]" }, #, | |
:parameters => { "projectId" => @project_id }) | |
#:parameters => { "projects" => @project_id }) | |
rescue NoMethodError => nme | |
#puts 'Error getting averagees: ' + nme.message | |
#puts 'dir_uri: ' + (nil != @client.directory_uri ? @client.directory_uri : 'NIL') | |
puts '----------- BEGIN CLIENT INSPECTION --------------' | |
puts @client.inspect | |
puts '----------- BEGIN Big Query INSPECTION --------------' | |
#puts @bigQuery.inspect | |
#puts '----- LIST PUBLIC METHODS --------' | |
#puts @bigQuery.public_methods | |
puts '----- LIST Instance variables ----' | |
#puts @bigQuery.pretty_print_instance_variables | |
puts '----- ' | |
#puts @bigQuery.pretty_print_inspect | |
#p @bigQuery | |
puts '#------#' | |
puts @bigQuery.pretty_inspect | |
puts '----- ' | |
#puts 'methods: ' + (nil != @bigQuery.discovered_methods ? | |
# '[' + @bigQuery.discovered_methods.join(',') + ']' : 'NIL') | |
#puts 'Base: ' + (nil != @bigQuery.method_base ? | |
# @bigQuery.method_base : 'NIL') | |
puts '----------- END of INSPECTION --------------' | |
raise nme | |
end | |
return result | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment