Skip to content

Commit

Permalink
Use TokenInfo class to wrap results
Browse files Browse the repository at this point in the history
  • Loading branch information
dmitrytrager committed Dec 27, 2024
1 parent 3ca8cd2 commit 53f8b9e
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 12 deletions.
3 changes: 2 additions & 1 deletion lib/panda/client.rb
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -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 = {})
Expand Down
9 changes: 0 additions & 9 deletions lib/panda/item.rb

This file was deleted.

15 changes: 15 additions & 0 deletions lib/panda/token_info.rb
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion spec/cases/collection_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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' },
Expand Down
2 changes: 1 addition & 1 deletion spec/cases/configuration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
29 changes: 29 additions & 0 deletions spec/cases/token_info_spec.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 53f8b9e

Please sign in to comment.