Skip to content

Commit

Permalink
Extract Usage from Client (#74)
Browse files Browse the repository at this point in the history
* Extract Usage from Client

* Fix Formatting

* Add Spec for Usage
  • Loading branch information
kyledecot committed Sep 13, 2019
1 parent 2855b20 commit 1dfc0a9
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 5 deletions.
12 changes: 7 additions & 5 deletions lib/app_store_connect/client.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
# frozen_string_literal: true

require 'active_support/all'
require 'mixpanel-ruby'
require 'securerandom'

require 'app_store_connect/request'
require 'app_store_connect/authorization'
require 'app_store_connect/schema'
require 'app_store_connect/client/usage'

module AppStoreConnect
class Client
Expand All @@ -23,8 +22,7 @@ def initialize(**kwargs)
.web_service_endpoints
.map { |s| [s.alias, s] }
.to_h
@distinct_id = SecureRandom.uuid
@tracker = Mixpanel::Tracker.new('1213f2b88b9b10b13d321f4c67a55ca8')
@usage = Usage.new
end

def respond_to_missing?(method_name, include_private = false)
Expand All @@ -39,6 +37,10 @@ def method_missing(method_name, *kwargs)
call(web_service_endpoint, *kwargs)
end

def inspect
"#<#{self.class.name}:#{object_id}>"
end

def web_service_endpoint_aliases
@web_service_endpoints_by_alias.keys
end
Expand All @@ -50,7 +52,7 @@ def call(web_service_endpoint, **kwargs)

request = build_request(web_service_endpoint, **kwargs)

@tracker.track(@distinct_id, web_service_endpoint.alias) if @options[:analytics_enabled]
@usage.track if @options[:analytics_enabled]
response = request.execute

JSON.parse(response.body) if response.body
Expand Down
22 changes: 22 additions & 0 deletions lib/app_store_connect/client/usage.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# frozen_string_literal: true

require 'mixpanel-ruby'
require 'securerandom'

module AppStoreConnect
class Client
class Usage
MIXPANEL_PROJECT_TOKEN = '1213f2b88b9b10b13d321f4c67a55ca8'
private_constant :MIXPANEL_PROJECT_TOKEN

def initialize
@distinct_id = SecureRandom.uuid
@tracker = Mixpanel::Tracker.new(MIXPANEL_PROJECT_TOKEN)
end

def track
@tracker.track(@distinct_id, 'usage')
end
end
end
end
13 changes: 13 additions & 0 deletions spec/app_store_connect/client/usage_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# frozen_string_literal: true

RSpec.describe AppStoreConnect::Client::Usage do
describe '#track' do
it 'should track usage w/ mixpanel' do
expect_any_instance_of(Mixpanel::Tracker)
.to receive(:track)
.with(String, 'usage')

described_class.new.track
end
end
end

0 comments on commit 1dfc0a9

Please sign in to comment.