httparty/README.md

80 lines
2.3 KiB
Markdown
Raw Permalink Normal View History

2012-04-22 19:40:09 +00:00
# httparty
2008-07-27 15:52:18 +00:00
2022-12-22 14:26:14 +00:00
[![CI](https://github.com/jnunemaker/httparty/actions/workflows/ci.yml/badge.svg)](https://github.com/jnunemaker/httparty/actions/workflows/ci.yml)
2020-06-01 08:08:23 +00:00
2017-12-11 21:10:37 +00:00
Makes http fun again! Ain't no party like a httparty, because a httparty don't stop.
2008-07-27 15:52:18 +00:00
2012-04-22 19:40:09 +00:00
## Install
2008-07-27 15:52:18 +00:00
2012-04-22 19:40:09 +00:00
```
gem install httparty
```
2008-07-27 15:52:18 +00:00
2012-04-22 19:40:09 +00:00
## Requirements
2008-07-27 15:52:18 +00:00
2021-09-29 12:43:05 +00:00
* Ruby 2.3.0 or higher
* multi_xml
2012-04-22 19:40:09 +00:00
* You like to party!
2012-04-22 19:40:09 +00:00
## Examples
2012-04-16 01:53:59 +00:00
2012-04-22 19:44:45 +00:00
```ruby
# Use the class methods to get down to business quickly
response = HTTParty.get('http://api.stackexchange.com/2.2/questions?site=stackoverflow')
2012-04-22 19:44:45 +00:00
2014-03-11 22:31:22 +00:00
puts response.body, response.code, response.message, response.headers.inspect
2012-04-22 19:44:45 +00:00
# Or wrap things up in your own class
2014-03-11 22:31:22 +00:00
class StackExchange
2012-04-22 19:44:45 +00:00
include HTTParty
2014-03-11 22:31:22 +00:00
base_uri 'api.stackexchange.com'
2012-04-22 19:44:45 +00:00
2014-03-11 22:31:22 +00:00
def initialize(service, page)
2016-11-08 14:20:36 +00:00
@options = { query: { site: service, page: page } }
2012-04-22 19:44:45 +00:00
end
2014-03-11 22:31:22 +00:00
def questions
self.class.get("/2.2/questions", @options)
2012-04-22 19:44:45 +00:00
end
2014-03-11 22:31:22 +00:00
def users
self.class.get("/2.2/users", @options)
2012-04-22 19:44:45 +00:00
end
end
2014-03-11 22:31:22 +00:00
stack_exchange = StackExchange.new("stackoverflow", 1)
puts stack_exchange.questions
puts stack_exchange.users
2012-04-22 19:44:45 +00:00
```
See the [examples directory](http://github.com/jnunemaker/httparty/tree/master/examples) for even more goodies.
2012-04-22 19:40:09 +00:00
## Command Line Interface
2012-04-22 19:44:45 +00:00
httparty also includes the executable `httparty` which can be
used to query web services and examine the resulting output. By default
it will output the response as a pretty-printed Ruby object (useful for
grokking the structure of output). This can also be overridden to output
2012-04-22 19:44:45 +00:00
formatted XML or JSON. Execute `httparty --help` for all the
options. Below is an example of how easy it is.
2012-04-22 19:40:09 +00:00
```
2014-03-11 22:31:22 +00:00
httparty "https://api.stackexchange.com/2.2/questions?site=stackoverflow"
2012-04-22 19:40:09 +00:00
```
2008-07-27 15:52:18 +00:00
2012-04-22 19:40:09 +00:00
## Help and Docs
2009-04-30 11:58:49 +00:00
2016-09-12 09:56:41 +00:00
* [Docs](https://github.com/jnunemaker/httparty/tree/master/docs)
2021-04-29 16:01:00 +00:00
* https://github.com/jnunemaker/httparty/discussions
2018-08-15 03:02:04 +00:00
* https://www.rubydoc.info/github/jnunemaker/httparty
2012-04-22 19:40:09 +00:00
## Contributing
* Fork the project.
* Run `bundle`
* Run `bundle exec rake`
2012-04-22 19:40:09 +00:00
* Make your feature addition or bug fix.
* Add tests for it. This is important so I don't break it in a future version unintentionally.
* Run `bundle exec rake` (No, REALLY :))
2012-04-22 19:40:09 +00:00
* Commit, do not mess with rakefile, version, or history. (if you want to have your own version, that is fine but bump version in a commit by itself in another branch so I can ignore when I pull)
* Send me a pull request. Bonus points for topic branches.