Class: GAShikomi::Api
- Inherits:
-
Object
- Object
- GAShikomi::Api
- Defined in:
- lib/ga_shikomi/api.rb
Defined Under Namespace
Classes: InexpectantResponse
Constant Summary collapse
- GOOGLE_API_KEYS =
%w(GOOGLE_API_CREDENTIAL GOOGLE_API_SECRETS)
Instance Attribute Summary collapse
-
#analytics ⇒ Object
readonly
Returns the value of attribute analytics.
-
#client ⇒ Object
readonly
Returns the value of attribute client.
-
#last_request ⇒ Object
readonly
Returns the value of attribute last_request.
Instance Method Summary collapse
-
#execute(method, params = {}) ⇒ Object
- param
-
Object method [return] Hash.
-
#flow(secrets) ⇒ Object
- param
-
Google::APIClient::ClientSecrets [return] Google::APIClient::InstalledAppFlow.
- #init_and_auth_analytics(store) ⇒ Object
- #init_and_auth_with_env ⇒ Object
-
#init_and_auth_with_file_storage(store) ⇒ Object
- param
-
String store.
- #init_client ⇒ Object
-
#initialize(store) ⇒ Api
constructor
- param
-
String store.
Constructor Details
#initialize(store) ⇒ Api
- param
-
String store
17 18 19 20 21 22 23 |
# File 'lib/ga_shikomi/api.rb', line 17 def initialize(store) @client = nil @analytics = nil @last_request = nil init_and_auth_analytics(store) end |
Instance Attribute Details
#analytics ⇒ Object (readonly)
Returns the value of attribute analytics.
24 25 26 |
# File 'lib/ga_shikomi/api.rb', line 24 def analytics @analytics end |
#client ⇒ Object (readonly)
Returns the value of attribute client.
24 25 26 |
# File 'lib/ga_shikomi/api.rb', line 24 def client @client end |
#last_request ⇒ Object (readonly)
Returns the value of attribute last_request.
24 25 26 |
# File 'lib/ga_shikomi/api.rb', line 24 def last_request @last_request end |
Instance Method Details
#execute(method, params = {}) ⇒ Object
- param
-
Object method
- return
-
Hash
85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/ga_shikomi/api.rb', line 85 def execute(method, params = {}) @last_request = client.execute( :api_method => method, :parameters => params) result = JSON.parse(last_request.response.body) if result['error'] raise InexpectantResponse, result['error'] else result end end |
#flow(secrets) ⇒ Object
- param
-
Google::APIClient::ClientSecrets
- return
-
Google::APIClient::InstalledAppFlow
73 74 75 76 77 78 79 |
# File 'lib/ga_shikomi/api.rb', line 73 def flow(secrets) ::Google::APIClient::InstalledAppFlow.new( :client_id => secrets.client_id, :client_secret => secrets.client_secret, :scope => ['https://www.googleapis.com/auth/analytics', 'https://www.googleapis.com/auth/analytics.edit']) end |
#init_and_auth_analytics(store) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/ga_shikomi/api.rb', line 26 def init_and_auth_analytics(store) init_client if GOOGLE_API_KEYS.all? {|e| ENV.has_key? e} init_and_auth_with_env else init_and_auth_with_file_storage(store) end @analytics = client.discovered_api('analytics', 'v3') end |
#init_and_auth_with_env ⇒ Object
38 39 40 41 42 43 44 |
# File 'lib/ga_shikomi/api.rb', line 38 def init_and_auth_with_env credential = ::Google::APIClient::Storage.new(EnvStore.new(ENV)) credential. secrets = ::Google::APIClient::ClientSecrets.new(JSON.parse(ENV['GOOGLE_API_SECRETS'])) client. = credential. end |
#init_and_auth_with_file_storage(store) ⇒ Object
- param
-
String store
49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/ga_shikomi/api.rb', line 49 def init_and_auth_with_file_storage(store) credential = ::Google::APIClient::FileStorage.new(store) secrets = ::Google::APIClient::ClientSecrets.load(File.dirname(store)) if credential..nil? flow = flow(secrets) client. = flow. credential.write_credentials(client.) else client. = credential. end end |
#init_client ⇒ Object
63 64 65 66 67 |
# File 'lib/ga_shikomi/api.rb', line 63 def init_client @client = ::Google::APIClient.new( :application_name => :gacli, :application_version => VERSION) end |