Permalink
Browse files

Update example applications

  • Loading branch information...
1 parent 3937af9 commit ca1a516749b4ca120511ea0ef595849b0b42b664 @remiprev committed May 9, 2012
Showing with 12 additions and 7 deletions.
  1. +2 −0 README.md
  2. +5 −4 examples/twitter-oauth/app.rb
  3. +5 −3 examples/twitter-search/app.rb
View
@@ -60,6 +60,8 @@ User.find(1)
# PUT https://api.example.com/users/1 with the data and return+update the User object
```
+You can look into the `examples` directory for sample applications using Her.
+
## Middleware
Since Her relies on [Faraday](https://github.com/technoweenie/faraday) to send HTTP requests, you can add additional middleware to handle requests and responses. Using the block in the `setup` call, you have access to Faraday’s `builder` object and are able to customize the middleware stack used on each request and response.
@@ -1,7 +1,5 @@
# Create custom parser
class TwitterParser < Faraday::Response::Middleware
- METADATA_KEYS = [:completed_in, :max_id, :max_id_str, :next_page, :page, :query, :refresh_url, :results_per_page, :since_id, :since_id_str]
-
def on_complete(env)
json = MultiJson.load(env[:body], :symbolize_keys => true)
errors = [json.delete(:error)]
@@ -13,6 +11,7 @@ def on_complete(env)
end
end
+# See https://dev.twitter.com/apps
TWITTER_CREDENTIALS = {
:consumer_key => "",
:consumer_secret => "",
@@ -22,8 +21,10 @@ def on_complete(env)
# Initialize API
Her::API.setup :base_uri => "https://api.twitter.com/1/" do |builder|
- builder.insert 0, FaradayMiddleware::OAuth, TWITTER_CREDENTIALS
- builder.swap Her::Middleware::DefaultParseJSON, TwitterParser
+ builder.use FaradayMiddleware::OAuth, TWITTER_CREDENTIALS
+ builder.use Faraday::Request::UrlEncoded
+ builder.use TwitterParser
+ builder.use Faraday::Adapter::NetHttp
end
# Define classes
@@ -4,10 +4,12 @@ class TwitterSearchParser < Faraday::Response::Middleware
def on_complete(env)
json = MultiJson.load(env[:body], :symbolize_keys => true)
+ data = json.delete(:results)
+ errors = [json.delete(:error)].compact
env[:body] = {
- :data => json[:results],
- :errors => [json[:error]],
- :metadata => json.select { |key, value| METADATA_KEYS.include?(key) }
+ :data => data,
+ :errors => errors,
+ :metadata => json
}
end
end

0 comments on commit ca1a516

Please sign in to comment.