Skip to content

Commit b8556f6

Browse files
authored
Merge pull request #22 from vikdotdev/v0.3.5-better-response-api
delegate Response instance self to params via method_missing
2 parents b840b40 + 8bbe8c7 commit b8556f6

6 files changed

Lines changed: 48 additions & 4 deletions

File tree

CHANGELOG.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,19 @@ recommendations of [keepachangelog.com](http://keepachangelog.com/).
2828
### Deprecated
2929
- None
3030

31+
## v0.3.5
32+
33+
### Added
34+
- `Response` instance now delegates to params via `method_missing`.
35+
```
36+
params = { foo: { bar: :baz }}
37+
response = Viberroo::Response.new(params)
38+
# Previously, those could only be accessed through params:
39+
puts response.params.foo.bar
40+
# Now those can be accessed directly:
41+
puts response.foo.bar
42+
```
43+
3144
## v0.3.4
3245
Start a changelog.
3346

Gemfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ source 'https://rubygems.org'
33
gemspec
44

55
group :development, :test do
6-
gem 'byebug'
6+
gem 'pry-byebug'
77
gem 'rake', '~> 12.0'
88
gem 'rspec', '~> 3.0'
99
gem 'webmock'

Gemfile.lock

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
PATH
22
remote: .
33
specs:
4-
viberroo (0.3.4)
4+
viberroo (0.3.5)
55
recursive-open-struct (~> 1.1.1)
66

77
GEM
@@ -10,10 +10,18 @@ GEM
1010
addressable (2.8.0)
1111
public_suffix (>= 2.0.2, < 5.0)
1212
byebug (11.1.3)
13+
coderay (1.1.3)
1314
crack (0.4.5)
1415
rexml
1516
diff-lcs (1.4.4)
1617
hashdiff (1.0.1)
18+
method_source (1.0.0)
19+
pry (0.13.1)
20+
coderay (~> 1.1)
21+
method_source (~> 1.0)
22+
pry-byebug (3.9.0)
23+
byebug (~> 11.0)
24+
pry (~> 0.13.0)
1725
public_suffix (4.0.6)
1826
rake (12.3.3)
1927
recursive-open-struct (1.1.3)
@@ -42,7 +50,7 @@ PLATFORMS
4250
ruby
4351

4452
DEPENDENCIES
45-
byebug
53+
pry-byebug
4654
rake (~> 12.0)
4755
redcarpet
4856
rspec (~> 3.0)

lib/viberroo/response.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,15 @@ class Response
1616
# skip_before_action :verify_authenticity_token
1717
#
1818
# def callback
19+
# # For example, params contain the following:
20+
# # { foo: { bar: { baz: :boo }}}
1921
# @response = Viberroo::Response.new(params.permit!)
22+
# # Those can be accessed through params:
23+
# puts @response.params.foo
24+
# # Or directly:
25+
# puts @response.foo
26+
# puts @response.foo.bar
27+
# puts @response.foo.bar.baz
2028
# @bot = Viberroo::Bot.new(response: @response)
2129
#
2230
# head :ok
@@ -47,5 +55,9 @@ def user_id
4755
@params.dig(:user, :id)
4856
end
4957
end
58+
59+
def method_missing(method)
60+
params.public_send(method)
61+
end
5062
end
5163
end

lib/viberroo/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
module Viberroo
2-
VERSION = '0.3.4'.freeze
2+
VERSION = '0.3.5'.freeze
33
end

spec/response_spec.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,4 +112,15 @@
112112
it { is_expected.to eq(params[:sender][:id]) }
113113
end
114114
end
115+
116+
describe 'method_missing' do
117+
let(:params) { { foo: { bar: :baz } } }
118+
119+
subject { Viberroo::Response.new(params) }
120+
121+
it 'self delegated methods to params' do
122+
expect(subject.foo.to_h).to eq(bar: :baz)
123+
expect(subject.foo.bar).to eq(:baz)
124+
end
125+
end
115126
end

0 commit comments

Comments
 (0)