Skip to content

Commit 2950544

Browse files
committed
Set user agent header for oembed requests
Certain services (like facebook) require a user agent for oembed requests, now we can easily set one via config 🎉
1 parent 94a5030 commit 2950544

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

lib/article_json/utils/o_embed_resolver/base.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,11 @@ def parsed_api_response
2828

2929
# @return [Hash]
3030
def http_headers
31-
{ 'Content-Type' => 'application/json' }
31+
headers = { 'Content-Type' => 'application/json' }
32+
unless ArticleJSON.configuration.oembed_user_agent.nil?
33+
headers['User-Agent'] = ArticleJSON.configuration.oembed_user_agent
34+
end
35+
headers
3236
end
3337

3438
class << self

spec/article_json/utlis/o_embed_resolver/vimeo_video_spec.rb

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,22 @@
2222

2323
let(:oembed_response) { File.read('spec/fixtures/vimeo_video_oembed.json') }
2424
let(:expected_headers) { { 'Content-Type' => 'application/json' } }
25+
let(:expected_response) { JSON.parse(oembed_response, symbolize_names: 1) }
2526

2627
before do
2728
stub_request(:get, expected_oembed_url)
2829
.with(headers: expected_headers)
2930
.to_return(body: oembed_response)
3031
end
3132

32-
it { should eq JSON.parse(oembed_response, symbolize_names: true) }
33+
context 'with no additional headers' do
34+
it { should eq expected_response }
35+
end
36+
37+
context 'with additional headers' do
38+
before { ArticleJSON.configure { |c| c.oembed_user_agent = 'foobar' } }
39+
let(:expected_headers) { super().merge('User-Agent' => 'foobar') }
40+
it { should eq expected_response }
41+
end
3342
end
3443
end

0 commit comments

Comments
 (0)