Skip to content

Commit 64bbf64

Browse files
committed
♻️ Clean up the code a bit
Towards: SYS-320
1 parent bb97afe commit 64bbf64

File tree

7 files changed

+60
-62
lines changed

7 files changed

+60
-62
lines changed

CHANGELOG.md

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,15 @@
1-
## [0.1.0] - 2024-07-02
1+
## [0.1.0] - 2024-07-10
22

3-
- Initial release
3+
The initial release. Provides all basic Ruby wrapper functionality for JustCall's V2 API.
4+
5+
The V2 API currently includes the following endpoints:
6+
- Calls
7+
- [List all calls](https://developer.justcall.io/reference/call_list)
8+
- [Get a call](https://developer.justcall.io/reference/call_get)
9+
- [Update a call](https://developer.justcall.io/reference/call_update)
10+
- [Download a recording](https://developer.justcall.io/reference/call_recording_download)
11+
- SMS
12+
- [List all texts](https://developer.justcall.io/reference/texts_list)
13+
- [Get a text](https://developer.justcall.io/reference/texts_get)
14+
- [Check reply](https://developer.justcall.io/reference/texts_checkreply)
15+
- [Send SMS/MMS](https://developer.justcall.io/reference/texts_new)

lib/justcall_ruby/api.rb

Lines changed: 18 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,46 +5,39 @@ module API
55
ROOT_URL = 'https://api.justcall.io/v2'.freeze
66
USER_AGENT = "JustCallRubyGem/#{JustCall::VERSION}".freeze
77

8-
def get_request(client, endpoint)
8+
private
9+
10+
def get_request(client:, endpoint:)
911
request = Net::HTTP::Get.new(request_url(endpoint))
1012

11-
set_request_headers(client, request)
12-
submit_request(request)
13+
submit_request(client: client, request: request)
1314
end
1415

15-
def post_request(client, endpoint, data = {})
16+
def post_request(client:, endpoint:, data: {})
1617
request = Net::HTTP::Post.new(request_url(endpoint))
1718

18-
set_request_headers(client, request, 'application/json')
19-
submit_request(request, data)
19+
submit_request(client: client, request: request, data: data)
2020
end
2121

22-
def put_request(client, endpoint, data = {})
22+
def put_request(client:, endpoint:, data: {})
2323
request = Net::HTTP::Put.new(request_url(endpoint))
2424

25-
set_request_headers(client, request, 'application/json')
26-
submit_request(request, data)
25+
submit_request(client: client, request: request, data: data)
2726
end
2827

29-
def delete_request(client, endpoint)
28+
def delete_request(client:, endpoint:)
3029
request = Net::HTTP::Delete.new(request_url(endpoint))
3130

32-
set_request_headers(client, request)
33-
submit_request(request)
31+
submit_request(client: client, request: request)
3432
end
3533

36-
private
37-
38-
def submit_request(request, data = {})
39-
request.body = data.is_a?(Hash) ? data.to_json : data if data.any?
40-
41-
process_request(request)
42-
end
34+
def submit_request(client:, request:, data: {})
35+
set_request_headers(client: client, request: request)
4336

44-
def process_request(request)
4537
uri = URI(request.path)
4638
http = Net::HTTP.new(uri.host, uri.port)
4739

40+
request.body = data.is_a?(Hash) ? data.to_json : data if data.any?
4841
http.use_ssl = true
4942
http.set_debug_output($stdout) if JustCall.config.full_debug?
5043

@@ -53,7 +46,7 @@ def process_request(request)
5346
JustCall::Response.new(response)
5447
end
5548

56-
def set_request_headers(client, request)
49+
def set_request_headers(client:, request:)
5750
request['Accept'] = 'application/json'
5851
request['Authorization'] = [client.api_key, client.api_secret].join(':')
5952
request['Content-Type'] = 'application/json'
@@ -64,11 +57,11 @@ def request_url(endpoint)
6457
[ROOT_URL, endpoint].join('/')
6558
end
6659

67-
def standardize_body_data(submitted_data, permitted_data_attrs)
68-
submitted_data = submitted_data.deep_transform_keys(&:to_sym)
69-
permitted_data = submitted_data.select! { |k, _| permitted_data_attrs.include?(k) } || submitted_data
60+
def standardize_body_data(submitted_attrs:, permitted_attrs:)
61+
submitted_attrs = submitted_attrs.deep_transform_keys(&:to_sym)
62+
permitted_attrs = submitted_attrs.select! { |k, _| permitted_attrs.include?(k) } || submitted_attrs
7063

71-
permitted_data.deep_transform_keys { |k| k.to_s.camelize(:lower) }
64+
permitted_attrs.deep_transform_keys { |k| k.to_s.camelize(:lower) }
7265
end
7366

7467
def convert_params_to_request_query(params)

lib/justcall_ruby/call.rb

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,35 +36,35 @@ class Call < Base
3636
download_recording: "calls/%s/recording/download".freeze,
3737
}.freeze
3838

39-
def initialize(client)
39+
def initialize(client:)
4040
@client = client
4141
end
4242

4343
def list(params = {})
44-
params = standardize_body_data(params, LIST_ATTRS[:permitted])
44+
params = standardize_body_data(submitted_attrs: params, permitted_attrs: LIST_ATTRS[:permitted])
4545
endpoint = ENDPOINTS[:list] % convert_params_to_request_query(params)
4646

47-
get_request(@client, endpoint)
47+
get_request(client: @client, endpoint: endpoint)
4848
end
4949

5050
def fetch(call_id, params = {})
51-
params = standardize_body_data(params, FETCH_ATTRS[:permitted])
51+
params = standardize_body_data(submitted_attrs: params, permitted_attrs: FETCH_ATTRS[:permitted])
5252
endpoint = ENDPOINTS[:fetch] % [call_id, convert_params_to_request_query(params)] # rubocop:disable Style/FormatString
5353

54-
get_request(@client, endpoint)
54+
get_request(client: @client, endpoint: endpoint)
5555
end
5656

5757
def update(call_id, params)
5858
endpoint = ENDPOINTS[:update] % call_id
59-
params = standardize_body_data(params, UPDATE_ATTRS[:permitted])
59+
params = standardize_body_data(submitted_attrs: params, permitted_attrs: UPDATE_ATTRS[:permitted])
6060

61-
put_request(@client, endpoint, params)
61+
put_request(client: @client, endpoint: endpoint, data: params)
6262
end
6363

6464
def download_recording(call_id)
6565
endpoint = ENDPOINTS[:download_recording] % call_id
6666

67-
get_request(@client, endpoint)
67+
get_request(client: @client, endpoint: endpoint)
6868
end
6969
end
7070
end

lib/justcall_ruby/client.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@ def initialize(api_key:, api_secret:)
1414
end
1515

1616
def call
17-
@call ||= JustCall::Call.new(self)
17+
@call ||= JustCall::Call.new(client: self)
1818
end
1919

2020
def sms
21-
@sms ||= JustCall::SMS.new(self)
21+
@sms ||= JustCall::SMS.new(client: self)
2222
end
2323
end
2424
end

lib/justcall_ruby/error.rb

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
module JustCall
22
class Error < StandardError
3-
43
class NoContent < JustCall::Error; end
54
class NotAuthorized < JustCall::Error; end
65
class NotFound < JustCall::Error; end
76
class RequestError < JustCall::Error; end
87
class TimeoutError < JustCall::Error; end
9-
108
end
119
end

lib/justcall_ruby/response.rb

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module JustCall
22
class Response
3-
attr_accessor :success
3+
attr_reader :body, :response, :sucess
44

55
def initialize(response)
66
@response = response
@@ -11,14 +11,13 @@ def initialize(response)
1111
when Net::HTTPNotFound
1212
raise JustCall::Error::NotFound, @response.body
1313
when Net::HTTPOK, Net::HTTPSuccess, Net::HTTPNoContent, Net::HTTPCreated
14-
self.success = true
15-
data = (JSON.parse(@response.body) if @response.body.present?)
16-
17-
@data =
18-
if data.is_a?(Hash)
19-
data.deep_symbolize_keys
20-
elsif data.is_a?(Array)
21-
data.map(&:deep_symbolize_keys)
14+
@success = true
15+
body = (JSON.parse(@response.body) if @response.body.present?)
16+
@body =
17+
if body.is_a?(Hash)
18+
body.deep_symbolize_keys
19+
elsif body.is_a?(Array)
20+
body.map(&:deep_symbolize_keys)
2221
end
2322
else
2423
puts "-- DEBUG: #{self}: RequestError: #{@response.inspect}" if JustCall.config.full_debug?
@@ -34,15 +33,11 @@ def initialize(response)
3433
end
3534

3635
def [](key)
37-
@data[key]
38-
end
39-
40-
def body
41-
@data
36+
body[key]
4237
end
4338

4439
def fetch(key)
45-
@data.fetch(key)
40+
body.fetch(key)
4641
end
4742

4843
def success?

lib/justcall_ruby/sms.rb

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,39 +28,39 @@ class SMS < Base
2828
send: "texts/new".freeze,
2929
}.freeze
3030

31-
def initialize(client)
31+
def initialize(client:)
3232
@client = client
3333
end
3434

3535
def list(params = {})
36-
params = standardize_body_data(params, LIST_ATTRS[:permitted])
36+
params = standardize_body_data(submitted_attrs: params, permitted_attrs: LIST_ATTRS[:permitted])
3737
endpoint = ENDPOINTS[:list] % convert_params_to_request_query(params)
3838

39-
get_request(@client, endpoint)
39+
get_request(client: @client, endpoint: endpoint)
4040
end
4141

4242
def fetch(sms_id)
4343
endpoint = ENDPOINTS[:fetch] % sms_id
4444

45-
get_request(@client, endpoint)
45+
get_request(client: @client, endpoint: endpoint)
4646
end
4747

4848
def check_reply(params)
4949
requires!(params, *CHECK_REPLY_ATTRS[:required])
5050

5151
endpoint = ENDPOINTS[:check_reply]
52-
params = standardize_body_data(params, CHECK_REPLY_ATTRS[:permitted])
52+
params = standardize_body_data(submitted_attrs: params, permitted_attrs: CHECK_REPLY_ATTRS[:permitted])
5353

54-
post_request(@client, endpoint, params)
54+
post_request(client: @client, endpoint: endpoint, data: params)
5555
end
5656

5757
def send(params)
5858
requires!(params, *SEND_ATTRS[:required])
5959

6060
endpoint = ENDPOINTS[:send]
61-
params = standardize_body_data(params, SEND_ATTRS[:permitted])
61+
params = standardize_body_data(submitted_attrs: params, permitted_attrs: SEND_ATTRS[:permitted])
6262

63-
post_request(@client, endpoint, params)
63+
post_request(client: @client, endpoint: endpoint, data: params)
6464
end
6565
end
6666
end

0 commit comments

Comments
 (0)