Skip to content

Commit

Permalink
Merge pull request #22 from vikdotdev/v0.3.5-better-response-api
Browse files Browse the repository at this point in the history
delegate Response instance self to params via method_missing
  • Loading branch information
vikdotdev authored Jul 8, 2022
2 parents b840b40 + 8bbe8c7 commit b8556f6
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 4 deletions.
13 changes: 13 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,19 @@ recommendations of [keepachangelog.com](http://keepachangelog.com/).
### Deprecated
- None

## v0.3.5

### Added
- `Response` instance now delegates to params via `method_missing`.
```
params = { foo: { bar: :baz }}
response = Viberroo::Response.new(params)
# Previously, those could only be accessed through params:
puts response.params.foo.bar
# Now those can be accessed directly:
puts response.foo.bar
```

## v0.3.4
Start a changelog.

Expand Down
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ source 'https://rubygems.org'
gemspec

group :development, :test do
gem 'byebug'
gem 'pry-byebug'
gem 'rake', '~> 12.0'
gem 'rspec', '~> 3.0'
gem 'webmock'
Expand Down
12 changes: 10 additions & 2 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
viberroo (0.3.4)
viberroo (0.3.5)
recursive-open-struct (~> 1.1.1)

GEM
Expand All @@ -10,10 +10,18 @@ GEM
addressable (2.8.0)
public_suffix (>= 2.0.2, < 5.0)
byebug (11.1.3)
coderay (1.1.3)
crack (0.4.5)
rexml
diff-lcs (1.4.4)
hashdiff (1.0.1)
method_source (1.0.0)
pry (0.13.1)
coderay (~> 1.1)
method_source (~> 1.0)
pry-byebug (3.9.0)
byebug (~> 11.0)
pry (~> 0.13.0)
public_suffix (4.0.6)
rake (12.3.3)
recursive-open-struct (1.1.3)
Expand Down Expand Up @@ -42,7 +50,7 @@ PLATFORMS
ruby

DEPENDENCIES
byebug
pry-byebug
rake (~> 12.0)
redcarpet
rspec (~> 3.0)
Expand Down
12 changes: 12 additions & 0 deletions lib/viberroo/response.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,15 @@ class Response
# skip_before_action :verify_authenticity_token
#
# def callback
# # For example, params contain the following:
# # { foo: { bar: { baz: :boo }}}
# @response = Viberroo::Response.new(params.permit!)
# # Those can be accessed through params:
# puts @response.params.foo
# # Or directly:
# puts @response.foo
# puts @response.foo.bar
# puts @response.foo.bar.baz
# @bot = Viberroo::Bot.new(response: @response)
#
# head :ok
Expand Down Expand Up @@ -47,5 +55,9 @@ def user_id
@params.dig(:user, :id)
end
end

def method_missing(method)
params.public_send(method)
end
end
end
2 changes: 1 addition & 1 deletion lib/viberroo/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Viberroo
VERSION = '0.3.4'.freeze
VERSION = '0.3.5'.freeze
end
11 changes: 11 additions & 0 deletions spec/response_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -112,4 +112,15 @@
it { is_expected.to eq(params[:sender][:id]) }
end
end

describe 'method_missing' do
let(:params) { { foo: { bar: :baz } } }

subject { Viberroo::Response.new(params) }

it 'self delegated methods to params' do
expect(subject.foo.to_h).to eq(bar: :baz)
expect(subject.foo.bar).to eq(:baz)
end
end
end

0 comments on commit b8556f6

Please sign in to comment.