Skip to content

Commit c7cf662

Browse files
committed
Fix async issue
1 parent ada8e8c commit c7cf662

7 files changed

Lines changed: 74 additions & 154 deletions

File tree

.rspec_status

Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,29 @@
1-
example_id | status | run_time |
2-
-------------------------------------- | ------ | --------------- |
3-
./spec/remote_ok/client_spec.rb[1:1] | passed | 0.00199 seconds |
4-
./spec/remote_ok/client_spec.rb[1:2:1] | passed | 0.00056 seconds |
5-
./spec/remote_ok/client_spec.rb[1:3:1] | passed | 0.00038 seconds |
6-
./spec/remote_ok/client_spec.rb[1:3:2] | passed | 0.00075 seconds |
7-
./spec/remote_ok/client_spec.rb[1:4:1] | passed | 0.00034 seconds |
8-
./spec/remote_ok/job_spec.rb[1:1] | passed | 0.00007 seconds |
9-
./spec/remote_ok/job_spec.rb[1:2:1] | passed | 0.00006 seconds |
10-
./spec/remote_ok/job_spec.rb[1:3:1] | passed | 0.00006 seconds |
11-
./spec/remote_ok/job_spec.rb[1:4:1] | passed | 0.00006 seconds |
12-
./spec/remote_ok/job_spec.rb[1:5:1] | passed | 0.00006 seconds |
13-
./spec/remote_ok/job_spec.rb[1:6:1] | passed | 0.00021 seconds |
14-
./spec/remote_ok/job_spec.rb[1:7:1] | passed | 0.00006 seconds |
15-
./spec/remote_ok/job_spec.rb[1:8:1] | passed | 0.00006 seconds |
16-
./spec/remote_ok/job_spec.rb[1:9:1] | passed | 0.00006 seconds |
17-
./spec/remote_ok/job_spec.rb[1:10:1] | passed | 0.00006 seconds |
18-
./spec/remote_ok/job_spec.rb[1:11:1] | passed | 0.00006 seconds |
19-
./spec/remote_ok/job_spec.rb[1:12:1] | passed | 0.00022 seconds |
20-
./spec/remote_ok/job_spec.rb[1:13:1] | passed | 0.00012 seconds |
21-
./spec/remote_ok/job_spec.rb[1:14:1] | passed | 0.00006 seconds |
22-
./spec/remote_ok/job_spec.rb[1:15:1] | passed | 0.00019 seconds |
23-
./spec/remote_ok/job_spec.rb[1:16:1] | passed | 0.00006 seconds |
24-
./spec/remote_ok/job_spec.rb[1:17:1] | passed | 0.00006 seconds |
25-
./spec/remote_ok_spec.rb[1:1] | passed | 0.00002 seconds |
1+
example_id | status | run_time |
2+
---------------------------------------- | ------ | --------------- |
3+
./spec/remote_ok/client_spec.rb[1:1] | passed | 0.00287 seconds |
4+
./spec/remote_ok/client_spec.rb[1:2:1] | passed | 0.00067 seconds |
5+
./spec/remote_ok/client_spec.rb[1:2:2:1] | passed | 0.0004 seconds |
6+
./spec/remote_ok/client_spec.rb[1:3:1] | passed | 0.0007 seconds |
7+
./spec/remote_ok/client_spec.rb[1:3:2] | passed | 0.00109 seconds |
8+
./spec/remote_ok/client_spec.rb[1:3:3] | passed | 0.00026 seconds |
9+
./spec/remote_ok/client_spec.rb[1:3:4] | passed | 0.0005 seconds |
10+
./spec/remote_ok/client_spec.rb[1:4:1] | passed | 0.00027 seconds |
11+
./spec/remote_ok/client_spec.rb[1:4:2] | passed | 0.00025 seconds |
12+
./spec/remote_ok/job_spec.rb[1:1] | passed | 0.00006 seconds |
13+
./spec/remote_ok/job_spec.rb[1:2:1] | passed | 0.00006 seconds |
14+
./spec/remote_ok/job_spec.rb[1:3:1] | passed | 0.00006 seconds |
15+
./spec/remote_ok/job_spec.rb[1:4:1] | passed | 0.00006 seconds |
16+
./spec/remote_ok/job_spec.rb[1:5:1] | passed | 0.00005 seconds |
17+
./spec/remote_ok/job_spec.rb[1:6:1] | passed | 0.00023 seconds |
18+
./spec/remote_ok/job_spec.rb[1:7:1] | passed | 0.00006 seconds |
19+
./spec/remote_ok/job_spec.rb[1:8:1] | passed | 0.00006 seconds |
20+
./spec/remote_ok/job_spec.rb[1:9:1] | passed | 0.00006 seconds |
21+
./spec/remote_ok/job_spec.rb[1:10:1] | passed | 0.00006 seconds |
22+
./spec/remote_ok/job_spec.rb[1:11:1] | passed | 0.00005 seconds |
23+
./spec/remote_ok/job_spec.rb[1:12:1] | passed | 0.00028 seconds |
24+
./spec/remote_ok/job_spec.rb[1:13:1] | passed | 0.00013 seconds |
25+
./spec/remote_ok/job_spec.rb[1:14:1] | passed | 0.00006 seconds |
26+
./spec/remote_ok/job_spec.rb[1:15:1] | passed | 0.00022 seconds |
27+
./spec/remote_ok/job_spec.rb[1:16:1] | passed | 0.00006 seconds |
28+
./spec/remote_ok/job_spec.rb[1:17:1] | passed | 0.00005 seconds |
29+
./spec/remote_ok_spec.rb[1:1] | passed | 0.00002 seconds |

Gemfile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,6 @@ group :development, :test do
1515
gem "rubocop-performance"
1616
gem "simplecov", "< 0.21"
1717
gem "standard", "~> 1.31"
18+
gem "pry", "~> 0.15"
19+
gem "binding_of_caller", "~> 1.0"
1820
end

Gemfile.lock

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,14 @@ GEM
2525
async-pool (0.10.2)
2626
async (>= 1.25)
2727
traces
28+
binding_of_caller (1.0.1)
29+
debug_inspector (>= 1.2.0)
30+
coderay (1.1.3)
2831
console (1.29.2)
2932
fiber-annotation
3033
fiber-local (~> 1.1)
3134
json
35+
debug_inspector (1.2.0)
3236
diff-lcs (1.5.1)
3337
docile (1.4.1)
3438
fiber-annotation (0.2.0)
@@ -41,6 +45,7 @@ GEM
4145
json (2.9.1)
4246
language_server-protocol (3.17.0.3)
4347
lint_roller (1.1.0)
48+
method_source (1.1.0)
4449
metrics (0.12.1)
4550
parallel (1.26.3)
4651
parser (3.3.6.0)
@@ -53,6 +58,9 @@ GEM
5358
protocol-http2 (0.22.0)
5459
protocol-hpack (~> 1.4)
5560
protocol-http (~> 0.18)
61+
pry (0.15.2)
62+
coderay (~> 1.1)
63+
method_source (~> 1.0)
5664
racc (1.8.1)
5765
rainbow (3.1.1)
5866
rake (13.2.1)
@@ -117,6 +125,8 @@ PLATFORMS
117125
x86_64-darwin-19
118126

119127
DEPENDENCIES
128+
binding_of_caller (~> 1.0)
129+
pry (~> 0.15)
120130
rake (~> 13.2)
121131
remoteok!
122132
rspec (~> 3.13)

README.md

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -74,14 +74,6 @@ The gem will send it's own default user agent but you can override it for your o
7474
client = RemoteOK::Client.new(user_agent: 'hello-there')
7575
```
7676

77-
#### `debug`
78-
79-
Turning on the debug flag will pass a request to HTTParty to print verbose debug output to the console, so that you can inspect whats happening on the network under the hood.
80-
81-
```ruby
82-
client = RemoteOK::Client.new(debug: true)
83-
```
84-
8577
### Client Methods
8678

8779
#### `.jobs`

lib/remoteok/client.rb

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@ class Client
88
require_relative "job"
99

1010
def initialize(**config)
11-
@base_url = config[:base_url] || "https://remoteok.io/api"
12-
@debug = config[:debug] || false
11+
@base_url = config[:base_url] || "https://remoteok.com/api"
1312
@user_agent = config[:user_agent] || default_user_agent
1413
end
1514

@@ -19,12 +18,12 @@ def with_fetch(params = {})
1918

2019
headers = {"User-Agent" => @user_agent}
2120

22-
response = Sync do
21+
@data = Sync do
2322
internet = Async::HTTP::Internet.new
24-
internet.get(uri, headers)
23+
response = internet.get(uri, headers)
24+
JSON.parse(response.read)
2525
end
2626

27-
@data = JSON.parse(response.body)
2827
self
2928
end
3029

spec/remote_ok/client_spec.rb

Lines changed: 26 additions & 116 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
before do
1010
allow(Async::HTTP::Internet).to receive(:new).and_return(internet)
1111
allow(internet).to receive(:get).and_return(http_response)
12+
allow(http_response).to receive(:read).and_return(job_data)
1213
end
1314

1415
it "can instantiate" do
@@ -29,6 +30,17 @@
2930
hash_including("User-Agent" => user_agent)
3031
)
3132
end
33+
34+
context "when providing a custom user agent" do
35+
before { described_class.new(user_agent: "lovely-user-agent").with_fetch }
36+
37+
it "sends the custom user agent instead of the default" do
38+
expect(internet).to have_received(:get).with(
39+
instance_of(URI::HTTPS),
40+
hash_including("User-Agent" => "lovely-user-agent")
41+
)
42+
end
43+
end
3244
end
3345

3446
# rubocop:disable RSpec/MultipleExpectations
@@ -48,6 +60,14 @@
4860
expect(uri.query).to include("tags=ruby%2Cdigital+nomad")
4961
end
5062
end
63+
64+
it "returns an array of Job objects" do
65+
expect(client.jobs.first).to be_a(RemoteOK::Job)
66+
end
67+
68+
it "returns the correct number of items" do
69+
expect(client.jobs.size).to eq(3)
70+
end
5171
end
5272
# rubocop:enable RSpec/MultipleExpectations
5373

@@ -57,121 +77,11 @@
5777

5878
expect(legal_info).to eq(JSON.parse(job_data).first["legal"])
5979
end
80+
81+
it "can be chained with fetch to force refresh" do
82+
legal_info = client.with_fetch.legal
83+
84+
expect(legal_info).to eq(JSON.parse(job_data).first["legal"])
85+
end
6086
end
61-
# let(:job_data) { File.open "spec/fixtures/jobs_data.json" }
62-
63-
# before do
64-
# instance_double("HTTParty", body: job_data.read)
65-
# allow(described_class).to receive(:get).and_return(httpclient)
66-
# end
67-
68-
# it "can instantiate" do
69-
# expect(described_class.new).not_to be_nil
70-
# end
71-
72-
# describe "User Agent" do
73-
# it "sends a default user agent with each request" do
74-
# httpclient = double("HTTParty", body: "{}")
75-
# exp_params = {
76-
# headers: {
77-
# "User-Agent" =>
78-
# "remote-ok-ruby/0.1.0 +http://github.com/RemoteCTO/remote-ok-ruby"
79-
# }
80-
# }
81-
82-
# expect(described_class).to(
83-
# receive(:get)
84-
# .with(anything, exp_params)
85-
# .and_return(httpclient)
86-
# )
87-
88-
# described_class.new.jobs
89-
# end
90-
91-
# context "when providing a custom user agent" do
92-
# it "sends the custom user agent instead of the default" do
93-
# httpclient = double("HTTParty", body: "{}")
94-
# exp_params = {headers: {"User-Agent" => "lovely-user-agent"}}
95-
96-
# expect(described_class).to(
97-
# receive(:get)
98-
# .with(anything, exp_params)
99-
# .and_return(httpclient)
100-
# )
101-
102-
# described_class.new(user_agent: "lovely-user-agent").jobs
103-
# end
104-
# end
105-
# end
106-
107-
# context "Debug flag on" do
108-
# it "Requests HTTP debug output into the console" do
109-
# httpclient = double("HTTParty", body: "{}")
110-
# exp_params = {
111-
# headers: {"User-Agent" => ""},
112-
# debug_output: $stdout
113-
# }
114-
115-
# expect(described_class).to(
116-
# receive(:get)
117-
# .with(anything, exp_params)
118-
# .and_return(httpclient)
119-
# )
120-
121-
# described_class.new(user_agent: "", debug: true).jobs
122-
# end
123-
# end
124-
125-
# describe "legal" do
126-
# it "outputs the RemoteOK legal text" do
127-
# expect(described_class.new.legal).to start_with "API Terms of Service"
128-
# end
129-
130-
# it "can be chained with fetch to force refresh" do
131-
# expect(described_class.new.with_fetch.legal).to start_with "API Terms of Service"
132-
# end
133-
# end
134-
135-
# describe "jobs" do
136-
# it "returns the correct number of items" do
137-
# expect(described_class.new.jobs.size).to eq 3
138-
# end
139-
140-
# it "returns an array of Job objects" do
141-
# expect(described_class.new.jobs.first).to be_a RemoteOK::Job
142-
# end
143-
144-
# context "when there are no jobs" do
145-
# before do
146-
# nj_data = File.open "spec/fixtures/no_jobs.json"
147-
# httpclient = double("HTTParty", body: nj_data.read)
148-
# allow(described_class).to receive(:get).and_return(httpclient)
149-
# end
150-
151-
# it "returns zero jobs" do
152-
# expect(described_class.new.jobs.size).to eq 0
153-
# end
154-
# end
155-
156-
# context "when providing tags" do
157-
# let(:data) { File.open "spec/fixtures/jobs_data.json" }
158-
# let(:http_client) { double("HTTParty", body: data.read) }
159-
160-
# it "provides the tags as parameters to the api" do
161-
# httpclient = double("HTTParty", body: data.read)
162-
# exp_params = {
163-
# headers: {"User-Agent" => ""},
164-
# query: {tags: "ruby,digital nomad"}
165-
# }
166-
167-
# expect(described_class).to(
168-
# receive(:get)
169-
# .with(anything, exp_params)
170-
# .and_return(httpclient)
171-
# )
172-
173-
# described_class.new(user_agent: "").jobs :ruby, :digital_nomad
174-
# end
175-
# end
176-
# end
17787
end

spec/spec_helper.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55

66
require "remoteok"
77

8+
require "pry"
9+
require "binding_of_caller"
10+
811
RSpec.configure do |config|
912
# Enable flags like --only-failures and --next-failure
1013
config.example_status_persistence_file_path = ".rspec_status"

0 commit comments

Comments
 (0)