Skip to content

Commit

Permalink
Replace Faraday with Typhoeus. [#3]
Browse files Browse the repository at this point in the history
  • Loading branch information
marnen committed Apr 4, 2020
1 parent 2d1e465 commit 90837a9
Show file tree
Hide file tree
Showing 7 changed files with 18 additions and 19 deletions.
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ gem 'jbuilder', '~> 2.7'
gem 'bootsnap', '>= 1.4.2', require: false

gem "haml-rails", "~> 2.0"
gem 'faraday', '~> 1.0.1'
gem 'moneta'
gem 'redis'
gem 'typhoeus'

group :development, :test do
# Call 'byebug' anywhere in the code to stop execution and get a debugger console
Expand Down
9 changes: 5 additions & 4 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,10 @@ GEM
diff-lcs (1.3)
erubi (1.9.0)
erubis (2.7.0)
ethon (0.12.0)
ffi (>= 1.3.0)
faker (2.11.0)
i18n (>= 1.6, < 2)
faraday (1.0.1)
multipart-post (>= 1.2, < 3)
ffi (1.12.2)
formatador (0.2.5)
gherkin (5.1.0)
Expand Down Expand Up @@ -177,7 +177,6 @@ GEM
msgpack (1.3.3)
multi_json (1.14.1)
multi_test (0.1.2)
multipart-post (2.1.1)
nenv (0.3.0)
nio4r (2.5.2)
nokogiri (1.10.9)
Expand Down Expand Up @@ -283,6 +282,8 @@ GEM
turbolinks (5.2.1)
turbolinks-source (~> 5.2)
turbolinks-source (5.2.0)
typhoeus (1.3.1)
ethon (>= 0.9.0)
tzinfo (1.2.7)
thread_safe (~> 0.1)
vcr (5.1.0)
Expand Down Expand Up @@ -314,7 +315,6 @@ DEPENDENCIES
byebug
cucumber-rails
faker
faraday (~> 1.0.1)
guard-cucumber!
guard-rspec
haml-rails (~> 2.0)
Expand All @@ -329,6 +329,7 @@ DEPENDENCIES
spring
spring-watcher-listen (~> 2.0.0)
turbolinks (~> 5)
typhoeus
tzinfo-data
vcr (~> 5.1.0)
web-console (>= 3.3.0)
Expand Down
2 changes: 1 addition & 1 deletion app/models/chart.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def url
params = {
cht: chart_type, chd: "a:#{data.join ','}", chs: size, chxt: axes, chxl: "0:#{axis_labels.map {|label| label.prepend '|' }.join }", chdl: @legend, chdlp: position, chls: line_thickness
}
url.query = Faraday::Utils.build_query params
url.query = URI.encode_www_form params
end
end

Expand Down
14 changes: 6 additions & 8 deletions app/models/state_daily.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,14 @@ def initialize(state:, date:)
end

def url
@url ||= if date_range?
individuals.map &:url
else
URI('https://covidtracking.com/api/states/daily').tap do |url|
url.query = Faraday::Utils.build_query state: @state, date: @date.to_s(:number)
end
end
@url ||= date_range? ? individuals.map(&:url) : request.url
end

def fetch!
if date_range?
individuals.map(&:fetch!).reject {|response| response['date'].nil? }
else
JSON.parse Faraday.get(url).body
JSON.parse request.run.body
end
end

Expand All @@ -31,4 +25,8 @@ def date_range?
def individuals
@individuals ||= @date.map {|date| self.class.new state: @state, date: date }
end

def request
@request ||= Typhoeus::Request.new('https://covidtracking.com/api/states/daily', params: {state: @state, date: @date.to_s(:number)})
end
end
2 changes: 1 addition & 1 deletion features/step_definitions/web_steps.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
date_range = start_date..end_date

page.find 'img[src^="https://image-charts.com/chart?"]' do |img|
params = Faraday::Utils.parse_query URI(img['src']).query
params = Hash[URI.decode_www_form URI(img['src']).query]
expect(params).to include(
'cht' => 'lc', # line chart
'chd' => a_string_starting_with('a:'), # data
Expand Down
2 changes: 1 addition & 1 deletion spec/models/chart_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
end

describe '#url' do
let(:params) { Faraday::Utils.parse_query subject.query }
let(:params) { Hash[URI.decode_www_form URI(subject).query] }

subject { chart.url }

Expand Down
6 changes: 3 additions & 3 deletions spec/models/state_daily_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@
subject { super().url }

context 'single date' do
it { is_expected.to be_a_kind_of URI }
it { is_expected.to be_a_kind_of String }

it 'points to the state daily data endpoint' do
expect(subject.to_s).to match %r{^https://covidtracking.com/api/states/daily\b([^/]|$)}
expect(subject).to match %r{^https://covidtracking.com/api/states/daily\b([^/]|$)}
end

context 'query string' do
let(:parsed_query) { Faraday::Utils.parse_query subject.query }
let(:parsed_query) { Hash[URI.decode_www_form URI(subject).query] }

it 'contains the state' do
expect(parsed_query['state']).to be == state
Expand Down

0 comments on commit 90837a9

Please sign in to comment.