From d491dea6be5e93d34b5754e96c5a9d33ca4717c1 Mon Sep 17 00:00:00 2001 From: dmitrytrager Date: Wed, 25 Dec 2024 13:24:59 +0100 Subject: [PATCH 1/7] Add token_info method, bump version --- CHANGELOG.md | 3 +++ lib/panda/client.rb | 12 ++++++++++++ lib/panda/version.rb | 2 +- spec/cases/client_spec.rb | 10 ++++++++++ 4 files changed, 26 insertions(+), 1 deletion(-) create mode 100644 CHANGELOG.md diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..fe05627 --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,3 @@ +## 0.5.0 + +* Add `token_info` [method](https://business-api.tiktok.com/portal/docs?id=1765927978092545) to validate TikTok profiles \ No newline at end of file diff --git a/lib/panda/client.rb b/lib/panda/client.rb index 5ccb444..640daa9 100644 --- a/lib/panda/client.rb +++ b/lib/panda/client.rb @@ -52,6 +52,18 @@ def report(advertiser_id, report_type, dimensions, params = {}) ) end + def token_info(app_id) + get_token('tt_user/token_info/get/', app_id: app_id, access_token: access_token) + end + + private + + def get_token(path, params = {}) + request = Panda::HTTPRequest.new('GET', path, params) + response = Panda.make_get_request(request) + response.status == 200 ? response.parsed_body : nil + end + def get_collection(path, params = {}) request = Panda::HTTPRequest.new('GET', path, params, 'Access-Token' => access_token) Panda::Collection.new(Panda.make_get_request(request), self) diff --git a/lib/panda/version.rb b/lib/panda/version.rb index a8b2399..a0d50c5 100644 --- a/lib/panda/version.rb +++ b/lib/panda/version.rb @@ -1,5 +1,5 @@ # frozen_string_literal: true module Panda - VERSION = '0.4.0' + VERSION = '0.5.0' end diff --git a/spec/cases/client_spec.rb b/spec/cases/client_spec.rb index 6bd4007..c4a101a 100644 --- a/spec/cases/client_spec.rb +++ b/spec/cases/client_spec.rb @@ -39,4 +39,14 @@ subject.advertiser_info(ids, fields: fields) end end + + describe '#token_info' do + let(:app_id) { SecureRandom.hex } + + it 'calls #get_token' do + expect(subject).to receive(:get_token).with('tt_user/token_info/get/', { app_id: app_id, access_token: token }) + + subject.token_info(app_id) + end + end end From d54c47ed941d624136fe0780e5237eddb58c9609 Mon Sep 17 00:00:00 2001 From: dmitrytrager Date: Thu, 26 Dec 2024 16:47:03 +0100 Subject: [PATCH 2/7] Use app_id from config --- lib/panda/client.rb | 8 ++++++-- spec/cases/client_spec.rb | 8 ++++---- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/lib/panda/client.rb b/lib/panda/client.rb index 640daa9..11f5c7c 100644 --- a/lib/panda/client.rb +++ b/lib/panda/client.rb @@ -52,8 +52,12 @@ def report(advertiser_id, report_type, dimensions, params = {}) ) end - def token_info(app_id) - get_token('tt_user/token_info/get/', app_id: app_id, access_token: access_token) + def token_info + get_token( + 'tt_user/token_info/get/', + app_id: Panda.config.app_id, + access_token: access_token, + ) end private diff --git a/spec/cases/client_spec.rb b/spec/cases/client_spec.rb index c4a101a..76ae289 100644 --- a/spec/cases/client_spec.rb +++ b/spec/cases/client_spec.rb @@ -41,12 +41,12 @@ end describe '#token_info' do - let(:app_id) { SecureRandom.hex } - it 'calls #get_token' do - expect(subject).to receive(:get_token).with('tt_user/token_info/get/', { app_id: app_id, access_token: token }) + expect(subject) + .to receive(:get_token) + .with('tt_user/token_info/get/', { app_id: Panda.config.app_id, access_token: token }) - subject.token_info(app_id) + subject.token_info end end end From ba1c60da0d1af9ebe695bd5bfb64bbe028c8ffc0 Mon Sep 17 00:00:00 2001 From: dmitrytrager Date: Thu, 26 Dec 2024 16:59:53 +0100 Subject: [PATCH 3/7] Do not check response status --- lib/panda/client.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/panda/client.rb b/lib/panda/client.rb index 11f5c7c..9e8fbc4 100644 --- a/lib/panda/client.rb +++ b/lib/panda/client.rb @@ -65,7 +65,7 @@ def token_info def get_token(path, params = {}) request = Panda::HTTPRequest.new('GET', path, params) response = Panda.make_get_request(request) - response.status == 200 ? response.parsed_body : nil + response.parsed_body.fetch('data', {}) end def get_collection(path, params = {}) From d31dde5ff335f35ab7f2321c6854181f1ebcbce0 Mon Sep 17 00:00:00 2001 From: dmitrytrager Date: Thu, 26 Dec 2024 17:16:23 +0100 Subject: [PATCH 4/7] Extract token response parsing into separate class --- lib/panda/client.rb | 3 +-- lib/panda/item.rb | 9 +++++++++ 2 files changed, 10 insertions(+), 2 deletions(-) create mode 100644 lib/panda/item.rb diff --git a/lib/panda/client.rb b/lib/panda/client.rb index 9e8fbc4..c2c9305 100644 --- a/lib/panda/client.rb +++ b/lib/panda/client.rb @@ -64,8 +64,7 @@ def token_info def get_token(path, params = {}) request = Panda::HTTPRequest.new('GET', path, params) - response = Panda.make_get_request(request) - response.parsed_body.fetch('data', {}) + Panda::Item.new(Panda.make_get_request(request)) end def get_collection(path, params = {}) diff --git a/lib/panda/item.rb b/lib/panda/item.rb new file mode 100644 index 0000000..d93c370 --- /dev/null +++ b/lib/panda/item.rb @@ -0,0 +1,9 @@ +# frozen_string_literal: true + +module Panda + class Item + def initialize(response) + response.parsed_body.fetch('data', {}) + end + end +end From 3ca8cd2d4ced32fdcfc17e9a3369ac3bd882a7d6 Mon Sep 17 00:00:00 2001 From: dmitrytrager Date: Thu, 26 Dec 2024 17:08:27 +0100 Subject: [PATCH 5/7] Try to change Ruby version matrix --- .github/workflows/ci.yml | 7 +++++-- lib/panda/client.rb | 2 +- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 9cf0688..e25008a 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -12,8 +12,11 @@ jobs: strategy: matrix: ruby-version: - - 2.7 - - 3.0 + - '2.7' + - '3.0' + - '3.1' + - '3.2' + - '3.3' steps: - uses: actions/checkout@v2 diff --git a/lib/panda/client.rb b/lib/panda/client.rb index c2c9305..7f4af28 100644 --- a/lib/panda/client.rb +++ b/lib/panda/client.rb @@ -56,7 +56,7 @@ def token_info get_token( 'tt_user/token_info/get/', app_id: Panda.config.app_id, - access_token: access_token, + access_token: access_token ) end From 53f8b9eaeed95e23fe7112b9448db7258beb9b4c Mon Sep 17 00:00:00 2001 From: dmitrytrager Date: Fri, 27 Dec 2024 12:20:48 +0100 Subject: [PATCH 6/7] Use TokenInfo class to wrap results --- lib/panda/client.rb | 3 ++- lib/panda/item.rb | 9 --------- lib/panda/token_info.rb | 15 +++++++++++++++ spec/cases/collection_spec.rb | 2 +- spec/cases/configuration_spec.rb | 2 +- spec/cases/token_info_spec.rb | 29 +++++++++++++++++++++++++++++ 6 files changed, 48 insertions(+), 12 deletions(-) delete mode 100644 lib/panda/item.rb create mode 100644 lib/panda/token_info.rb create mode 100644 spec/cases/token_info_spec.rb diff --git a/lib/panda/client.rb b/lib/panda/client.rb index 7f4af28..fc22a9c 100644 --- a/lib/panda/client.rb +++ b/lib/panda/client.rb @@ -1,6 +1,7 @@ # frozen_string_literal: true require 'panda/collection' +require 'panda/token_info' require 'panda/error_middleware' require 'panda/errors' require 'panda/http_request' @@ -64,7 +65,7 @@ def token_info def get_token(path, params = {}) request = Panda::HTTPRequest.new('GET', path, params) - Panda::Item.new(Panda.make_get_request(request)) + Panda::TokenInfo.new(Panda.make_get_request(request)) end def get_collection(path, params = {}) diff --git a/lib/panda/item.rb b/lib/panda/item.rb deleted file mode 100644 index d93c370..0000000 --- a/lib/panda/item.rb +++ /dev/null @@ -1,9 +0,0 @@ -# frozen_string_literal: true - -module Panda - class Item - def initialize(response) - response.parsed_body.fetch('data', {}) - end - end -end diff --git a/lib/panda/token_info.rb b/lib/panda/token_info.rb new file mode 100644 index 0000000..b5e30b5 --- /dev/null +++ b/lib/panda/token_info.rb @@ -0,0 +1,15 @@ +# frozen_string_literal: true + +module Panda + class TokenInfo + attr_reader :data + + def initialize(response) + @data = response.parsed_body.fetch('data', {}) + end + + def [](key) + @data[key] + end + end +end diff --git a/spec/cases/collection_spec.rb b/spec/cases/collection_spec.rb index f41e60b..18c0afb 100644 --- a/spec/cases/collection_spec.rb +++ b/spec/cases/collection_spec.rb @@ -66,7 +66,7 @@ end it 'gets result list' do - collection = Panda::Collection.new(response, nil) + collection = described_class.new(response, nil) expect(collection).to contain_exactly( { 'id' => 'test_id_1', 'name' => 'test_name_1' }, diff --git a/spec/cases/configuration_spec.rb b/spec/cases/configuration_spec.rb index 1adae59..203158b 100644 --- a/spec/cases/configuration_spec.rb +++ b/spec/cases/configuration_spec.rb @@ -6,6 +6,6 @@ subject { described_class.new } it 'sets default API base url' do - expect(subject.api_base_url).to eq(Panda::Configuration::API_BASE_URL) + expect(subject.api_base_url).to eq(described_class::API_BASE_URL) end end diff --git a/spec/cases/token_info_spec.rb b/spec/cases/token_info_spec.rb new file mode 100644 index 0000000..1505504 --- /dev/null +++ b/spec/cases/token_info_spec.rb @@ -0,0 +1,29 @@ +# frozen_string_literal: true + +require 'spec_helper' + +describe Panda::TokenInfo do + let(:response) { Panda::HTTPResponse.new(200, {}, token_response_body) } + + context 'response with result list field in data field' do + let(:token_response_body) do + { + 'message' => 'OK', + 'code' => 0, + 'data' => { + 'app_id' => Panda.config.app_id, + 'creator_id' => 'test_creator_id', + 'scope' => 'user.info.basic' + }, + 'request_id': '2020031009181201018904922342087A16' + } + end + + it 'gets token info' do + token_info = described_class.new(response) + + expect(token_info['creator_id']).to eq('test_creator_id') + expect(token_info['scope']).to eq('user.info.basic') + end + end +end From 333e0e7512a23e604453f57bf640fb12e2fea5f5 Mon Sep 17 00:00:00 2001 From: dmitrytrager Date: Sat, 28 Dec 2024 15:42:59 +0100 Subject: [PATCH 7/7] Keep client imports sorted --- lib/panda/client.rb | 2 +- lib/panda/token_info.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/panda/client.rb b/lib/panda/client.rb index fc22a9c..19f563f 100644 --- a/lib/panda/client.rb +++ b/lib/panda/client.rb @@ -1,11 +1,11 @@ # frozen_string_literal: true require 'panda/collection' -require 'panda/token_info' require 'panda/error_middleware' require 'panda/errors' require 'panda/http_request' require 'panda/http_response' +require 'panda/token_info' module Panda class Client diff --git a/lib/panda/token_info.rb b/lib/panda/token_info.rb index b5e30b5..24433ee 100644 --- a/lib/panda/token_info.rb +++ b/lib/panda/token_info.rb @@ -9,7 +9,7 @@ def initialize(response) end def [](key) - @data[key] + data[key] end end end