diff --git a/CHANGELOG.md b/CHANGELOG.md index 8a19224..73f70f1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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. diff --git a/Gemfile b/Gemfile index d7a7321..2d9a399 100644 --- a/Gemfile +++ b/Gemfile @@ -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' diff --git a/Gemfile.lock b/Gemfile.lock index 1400bf1..856fc5e 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,7 +1,7 @@ PATH remote: . specs: - viberroo (0.3.4) + viberroo (0.3.5) recursive-open-struct (~> 1.1.1) GEM @@ -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) @@ -42,7 +50,7 @@ PLATFORMS ruby DEPENDENCIES - byebug + pry-byebug rake (~> 12.0) redcarpet rspec (~> 3.0) diff --git a/lib/viberroo/response.rb b/lib/viberroo/response.rb index 728f050..60d7165 100644 --- a/lib/viberroo/response.rb +++ b/lib/viberroo/response.rb @@ -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 @@ -47,5 +55,9 @@ def user_id @params.dig(:user, :id) end end + + def method_missing(method) + params.public_send(method) + end end end diff --git a/lib/viberroo/version.rb b/lib/viberroo/version.rb index 4a1fed4..362932b 100644 --- a/lib/viberroo/version.rb +++ b/lib/viberroo/version.rb @@ -1,3 +1,3 @@ module Viberroo - VERSION = '0.3.4'.freeze + VERSION = '0.3.5'.freeze end diff --git a/spec/response_spec.rb b/spec/response_spec.rb index 4853ead..be1e58a 100644 --- a/spec/response_spec.rb +++ b/spec/response_spec.rb @@ -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