Skip to content

Commit 448326c

Browse files
committed
v0.3: switched to resource based approch
1 parent 4d76c80 commit 448326c

32 files changed

+459
-181
lines changed

README.md

+7-3
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,21 @@
33
# Ruby Client for the Sms77.io SMS Gateway API
44

55
## Installation
6+
67
```gem install sms77```
78

89
### Usage
10+
911
```ruby
1012
require 'sms77'
1113

12-
client = Sms77::Client.new(ENV['SMS77_API_KEY'])
13-
14-
puts "Balance: #{client.balance}"
14+
puts Sms77::Client.new(Sms77::Resource(ENV['SMS77_API_KEY'])).Balance.retrieve
15+
# or
16+
puts Sms77::Resources::Balance.new(ENV['SMS77_API_KEY']).retrieve
1517
```
1618

1719
#### Testing
20+
1821
```shell
1922
SMS77_API_KEY=MySms77ApiKey bundle exec rspec
2023
```
@@ -26,4 +29,5 @@ Setting ```SMS77_DEBUG=1``` prints details to stdout.
2629
Setting ```SMS77_TEST_HTTP=1``` enables live testing with actual API requests.
2730

2831
##### Support
32+
2933
Need help? Feel free to send us an <a href='mailto: support@sms77.io'>email</a>.

lib/sms77.rb

-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
# frozen_string_literal: true
22

33
require 'sms77/version'
4-
require 'sms77/client'
54

65
module Sms77
76
end

lib/sms77/client.rb

+20-55
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,29 @@
11
# frozen_string_literal: true
22

3-
require 'cgi'
4-
require 'json'
5-
require 'faraday'
6-
require 'sms77/endpoint'
7-
require 'sms77/contacts'
8-
require 'sms77/base'
3+
require 'sms77/resources/analytics'
4+
require 'sms77/resources/balance'
5+
require 'sms77/resources/contacts'
6+
require 'sms77/resources/hooks'
7+
require 'sms77/resources/journal'
8+
require 'sms77/resources/lookup'
9+
require 'sms77/resources/pricing'
10+
require 'sms77/resources/sms'
11+
require 'sms77/resources/status'
12+
require 'sms77/resources/validate_for_voice'
13+
require 'sms77/resources/voice'
14+
require 'sms77/util'
915

1016
module Sms77
11-
class Client < Sms77::Base
12-
def analytics(params = {})
13-
get(Sms77::Endpoint::ANALYTICS, params)
14-
end
15-
16-
def balance
17-
get(Sms77::Endpoint::BALANCE)
18-
end
19-
20-
def contacts(params)
21-
get_or_post(Sms77::Contacts::Action::READ == params[:action], Sms77::Endpoint::CONTACTS, params)
22-
end
23-
24-
def hooks(params)
25-
Sms77::Hooks::Validator::validate(params)
26-
27-
get_or_post(Sms77::Hooks::Action::READ == params[:action], Sms77::Endpoint::HOOKS, params)
28-
end
29-
30-
def journal(params)
31-
get(Sms77::Endpoint::JOURNAL, params)
32-
end
33-
34-
def lookup(params)
35-
post(Sms77::Endpoint::LOOKUP, params)
36-
end
37-
38-
def pricing(params = {})
39-
get(Sms77::Endpoint::PRICING, params)
40-
end
41-
42-
def sms(params)
43-
post(Sms77::Endpoint::SMS, params)
44-
end
45-
46-
def status(params)
47-
get(Sms77::Endpoint::STATUS, params)
48-
end
49-
50-
def validate_for_voice(params)
51-
post(Sms77::Endpoint::VALIDATE_FOR_VOICE, params)
52-
end
53-
54-
def voice(params)
55-
post(Sms77::Endpoint::VOICE, params)
56-
end
17+
class Client
18+
# @param resource [Sms77::Resource]
19+
def initialize(resource)
20+
Sms77::Util::get_namespace_classes(Sms77::Resources).each do |cls|
21+
name = cls.name.split('::').last
5722

58-
private
23+
instance_variable_set("@#{name}", cls.new(resource))
5924

60-
def get_or_post(bool, *args)
61-
method(bool ? :get : :post).call(*args)
25+
singleton_class.instance_eval("attr_reader :#{name}")
26+
end
6227
end
6328
end
6429
end

lib/sms77/hooks.rb

+5-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ def self.validate(params)
2727
if Sms77::Hooks::Action::SUBSCRIBE == action
2828
raise 'Parameter validation failed' unless Sms77::Hooks::Validator::subscribe(params)
2929
elsif Sms77::Hooks::Action::UNSUBSCRIBE == action
30-
raise 'ID must be a positive integer' unless Sms77::Util::is_positive_integer?(params[:id])
30+
raise 'ID must be a positive integer' unless Sms77::Hooks::Validator::unsubscribe(params)
3131
end
3232
end
3333

@@ -39,6 +39,10 @@ def self.subscribe(params)
3939
self.target_url?(params[:target_url])
4040
end
4141

42+
def self.unsubscribe(params)
43+
Sms77::Util::is_positive_integer?(params[:id])
44+
end
45+
4246
def self.is_action?(str)
4347
Sms77::Util::in_module_constants?(str, Sms77::Hooks::Action)
4448
end

lib/sms77/base.rb renamed to lib/sms77/resource.rb

+38-20
Original file line numberDiff line numberDiff line change
@@ -6,51 +6,59 @@
66
require 'sms77/endpoint'
77

88
module Sms77
9-
class Base
9+
class Resource
10+
attr_reader :api_key, :endpoint, :sent_with, :http_methods, :request_methods, :builder, :conn
11+
1012
BASE_PATH = '/api/'
11-
CONN = Faraday.new("https://gateway.sms77.io#{BASE_PATH}")
12-
HTTP_GET = CONN.method(:get).freeze
13-
HTTP_POST = CONN.method(:post).freeze
14-
CONN.freeze
15-
HTTP_METHODS = [HTTP_GET, HTTP_POST].freeze
16-
BUILDER = CONN.builder
1713

1814
def initialize(api_key, sent_with = 'ruby')
1915
raise 'missing api_key in config' if api_key.to_s.empty?
2016
raise 'missing sent_with in config' if sent_with.to_s.empty?
2117

2218
@api_key = api_key
2319
@sent_with = sent_with
24-
25-
HTTP_METHODS.each do |method|
26-
define_singleton_method(method.name) { |*args| request(method, *args) }
27-
end
20+
@endpoint = self.class.get_endpoint
21+
@http_methods = self.class.get_http_methods
22+
@conn = Faraday.new("https://gateway.sms77.io#{BASE_PATH}")
2823
end
2924

30-
attr_reader :api_key, :sent_with
31-
3225
protected
3326

34-
def request(method, path, payload = {})
35-
if !payload.empty? && HTTP_GET == method
36-
path = "#{path}?#{URI.encode_www_form(payload)}"
27+
def request(payload = {}, query = {})
28+
path = @endpoint
29+
http_method = @http_methods[caller_locations.first.label.to_sym]
30+
31+
if :get == http_method
32+
query = payload
3733

3834
payload = {}
3935
end
4036

41-
method = method.name
37+
query.each do |key, val|
38+
query.store(key, Sms77::Util::to_numbered_bool(val))
39+
end
40+
41+
payload.each do |key, val|
42+
payload.store(key, Sms77::Util::to_numbered_bool(val))
43+
end
44+
45+
unless query.empty?
46+
path = "#{path}?#{URI.encode_www_form(query)}"
47+
end
48+
4249
headers = Hash[
4350
Faraday::Request::Authorization::KEY, "Bearer #{@api_key}",
4451
'sentWith', @sent_with
4552
]
4653

47-
res = CONN.run_request(method, path, payload, headers)
54+
res = @conn.run_request(http_method, path, payload, headers)
4855

4956
puts JSON.pretty_generate(res.to_hash.merge({
50-
:method => method,
57+
:method => http_method,
5158
:path => path,
5259
:payload => payload,
53-
:req_headers => headers
60+
:req_headers => headers,
61+
:query => query,
5462
}).compact) if ENV['SMS77_DEBUG']
5563

5664
raise "Error requesting (#{self.class.name}) with code #{res.status}" unless 200 == res.status
@@ -71,5 +79,15 @@ def request(method, path, payload = {})
7179

7280
body
7381
end
82+
83+
class << self
84+
def get_http_methods
85+
@http_methods
86+
end
87+
88+
def get_endpoint
89+
@endpoint
90+
end
91+
end
7492
end
7593
end

lib/sms77/resources/analytics.rb

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# frozen_string_literal: true
2+
3+
require 'sms77/resource'
4+
5+
module Sms77::Resources
6+
class Analytics < Sms77::Resource
7+
@endpoint = Sms77::Endpoint::ANALYTICS
8+
@http_methods = {
9+
:retrieve => :get,
10+
}
11+
12+
def retrieve(params = {})
13+
request(params)
14+
end
15+
end
16+
end

lib/sms77/resources/balance.rb

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# frozen_string_literal: true
2+
3+
require 'sms77/resource'
4+
5+
module Sms77::Resources
6+
class Balance < Sms77::Resource
7+
@endpoint = Sms77::Endpoint::BALANCE
8+
@http_methods = {
9+
:retrieve => :get,
10+
}
11+
12+
def retrieve
13+
request
14+
end
15+
end
16+
end

lib/sms77/resources/contacts.rb

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# frozen_string_literal: true
2+
3+
require 'sms77/resource'
4+
5+
module Sms77::Resources
6+
class Contacts < Sms77::Resource
7+
@endpoint = Sms77::Endpoint::CONTACTS
8+
@http_methods = {
9+
:delete => :post,
10+
:read => :get,
11+
:write => :post,
12+
}
13+
14+
def read(params = {})
15+
request(params.merge({ :action => Sms77::Contacts::Action::READ }))
16+
end
17+
18+
def delete(params)
19+
request({}, params.merge({ :action => Sms77::Contacts::Action::DEL }))
20+
end
21+
22+
def write(params)
23+
request({}, params.merge({ :action => Sms77::Contacts::Action::WRITE }))
24+
end
25+
end
26+
end

lib/sms77/resources/hooks.rb

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# frozen_string_literal: true
2+
3+
require 'sms77/resource'
4+
5+
module Sms77::Resources
6+
class Hooks < Sms77::Resource
7+
@endpoint = Sms77::Endpoint::HOOKS
8+
@http_methods = {
9+
:read => :get,
10+
:subscribe => :post,
11+
:unsubscribe => :post,
12+
}
13+
14+
def read(params = {})
15+
request(params.merge({ :action => Sms77::Hooks::Action::READ }))
16+
end
17+
18+
def subscribe(params)
19+
Sms77::Hooks::Validator::subscribe(params)
20+
21+
request(params.merge({ :action => Sms77::Hooks::Action::SUBSCRIBE }))
22+
end
23+
24+
def unsubscribe(params)
25+
Sms77::Hooks::Validator::unsubscribe(params)
26+
27+
request(params.merge({ :action => Sms77::Hooks::Action::UNSUBSCRIBE }))
28+
end
29+
end
30+
end

lib/sms77/resources/journal.rb

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# frozen_string_literal: true
2+
3+
require 'sms77/resource'
4+
5+
module Sms77::Resources
6+
class Journal < Sms77::Resource
7+
@endpoint = Sms77::Endpoint::JOURNAL
8+
@http_methods = {
9+
:retrieve => :get,
10+
}
11+
12+
def retrieve(params)
13+
request(params)
14+
end
15+
end
16+
end

lib/sms77/resources/lookup.rb

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# frozen_string_literal: true
2+
3+
require 'sms77/resource'
4+
module Sms77::Resources
5+
class Lookup < Sms77::Resource
6+
@endpoint = Sms77::Endpoint::LOOKUP
7+
@http_methods = {
8+
:cnam => :post,
9+
:format => :post,
10+
:hlr => :post,
11+
:mnp => :post,
12+
}
13+
14+
def cnam(params)
15+
request(params.merge({ :type => Sms77::Lookup::Type::CNAM }))
16+
end
17+
18+
def format(params)
19+
request(params.merge({ :type => Sms77::Lookup::Type::FORMAT }))
20+
end
21+
22+
def hlr(params)
23+
request(params.merge({ :type => Sms77::Lookup::Type::HLR }))
24+
end
25+
26+
def mnp(params)
27+
request(params.merge({ :type => Sms77::Lookup::Type::MNP }))
28+
end
29+
end
30+
end

0 commit comments

Comments
 (0)