23 |
Install
24 |
$ sudo gem install httparty
25 |
26 |
Some Quick Examples
27 |
28 |
The following is a simple example of wrapping Twitter's API for posting updates.
29 |
30 |
class Twitter
31 | include HTTParty
32 | base_uri 'twitter.com'
33 | basic_auth 'username', 'password'
34 | end
35 |
36 | Twitter.post('/statuses/update.json', query: {status: "It's an HTTParty and everyone is invited!"})
37 |
38 |
That is really it! The object returned is a ruby hash that is decoded from Twitter's json response. JSON parsing is used because of the .json extension in the path of the request. You can also explicitly set a format (see the examples).
39 |
40 |
That works and all but what if you don't want to embed your username and password in the class? Below is an example to fix that:
41 |
42 |
class Twitter
43 | include HTTParty
44 | base_uri 'twitter.com'
45 |
46 | def initialize(u, p)
47 | @auth = {username: u, password: p}
48 | end
49 |
50 | def post(text)
51 | options = { query: {status: text}, basic_auth: @auth }
52 | self.class.post('/statuses/update.json', options)
53 | end
54 | end
55 |
56 | Twitter.new('username', 'password').post("It's an HTTParty and everyone is invited!")
57 |
58 |
More Examples: There are several examples in the gem itself.
59 |
60 |
Support
61 |
Conversations welcome in the google group and bugs/features over at Github.
62 |
63 |
64 |