Module and Request class to help building RequestObject testing framework
Add this line to your application's Gemfile:
gem 'apir'
And then execute:
$ bundle
Or install it yourself as:
$ gem install apir
Lock the gem version strictly to be safe from my destructive actions.
request = Apir::Request.new
request.query = { param: 'value' }
request.get!
request.cookies = { basic: 'usage' }
request.redo!
request.cookie_jar # assert
request.cookie_jar << HTTP::Cookie # modify
class GetWeather
include Apir::Request
def initialize(**args)
@url = 'https://weather.com'
super(@url, args)
end
def post_initialize
# hook to gain some control
# after the request execution
puts response # if JSON
puts raw_response.code # RestClient response object
@property = result
end
def result
# bind desired response data
response[:result] || {}
end
def sunny?(city)
self.query.merge!(cityName: city)
get!
sun_factor > 3
end
def sun_factor
result[:sunFactor]
end
end
request = GetWeather.new
request.sunny?('London')
Bug reports and pull requests are welcome on GitHub at https://github.com/xenjke/apir. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.
After checking out the repo, run rake spec
or rake
to run the tests.
The gem is available as open source under the terms of the MIT License.