Skip to content

Commit

Permalink
added associations to resources, addressed issue where .with_access_t…
Browse files Browse the repository at this point in the history
…oken would need to be called in a nested manner when loading associations
  • Loading branch information
Aaron Severs committed Sep 24, 2017
1 parent 0e7c849 commit 683c7cb
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 1 deletion.
8 changes: 8 additions & 0 deletions lib/frederick_api/v2/contact.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@ module V2
# /v2/locations/:location_id/contacts
class Contact < Resource
belongs_to :location
has_one :contact_type
has_one :parent, class_name: 'FrederickAPI::V2::Contact'
has_one :referred_by_contact, class_name: 'FrederickAPI::V2::Contact'
has_many :children, class_name: 'FrederickAPI::V2::Contact'
has_many :referred_contacts, class_name: 'FrederickAPI::V2::Contact'
has_many :contact_lists
has_many :interactions

self.read_only_attributes += [:location_id]
end
end
Expand Down
3 changes: 3 additions & 0 deletions lib/frederick_api/v2/contact_list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ module V2
# /v2/locations/:location_id/contact_lists
class ContactList < Resource
belongs_to :location
has_many :contacts
has_one :contact_type

self.read_only_attributes += [:location_id]
end
end
Expand Down
2 changes: 2 additions & 0 deletions lib/frederick_api/v2/contact_property.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ module V2
# /v2/locations/:location_id/contact_properties
class ContactProperty < Resource
belongs_to :location
has_one :contact_type

self.read_only_attributes += [:location_id]
end
end
Expand Down
3 changes: 3 additions & 0 deletions lib/frederick_api/v2/contact_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ module V2
# /v2/locations/:location_id/contact_types
class ContactType < Resource
belongs_to :location
has_many :contact_properties
has_many :contacts

self.read_only_attributes += [:location_id]
end
end
Expand Down
2 changes: 2 additions & 0 deletions lib/frederick_api/v2/interaction.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ module V2
# /v2/locations/:location_id/interactions
class Interaction < Resource
belongs_to :location
has_one :contact

self.read_only_attributes += [:location_id]
end
end
Expand Down
4 changes: 4 additions & 0 deletions lib/frederick_api/v2/resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ def self.with_access_token(token)
def self.custom_headers
super.merge(x_api_key: top_level_namespace.config.api_key)
end

def self._header_store
Thread.current['frederick_api_header_store'] ||= {}
end
end
end
end
2 changes: 1 addition & 1 deletion lib/frederick_api/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@

module FrederickAPI
# Current gem version
VERSION = '0.2.0'
VERSION = '0.3.0'
end
16 changes: 16 additions & 0 deletions spec/frederick_api/v2/resource_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@

it 'merges api key' do
expect(subclass.custom_headers[:x_api_key]).to eq('1234-5678-8765-4321')
expect(subclass.custom_headers[:existing]).to eq('headers')
end
end

Expand All @@ -80,4 +81,19 @@
end
end
end

describe '._header_store' do
it '{} by default' do
expect(described_class._header_store).to eq({})
end

context 'thread current present' do
before { Thread.current['frederick_api_header_store'] = { foo: 'bar' } }
after { Thread.current['frederick_api_header_store'] = nil }

it 'thread current' do
expect(described_class._header_store).to eq(foo: 'bar')
end
end
end
end

0 comments on commit 683c7cb

Please sign in to comment.