JSON and the APInauts
- 5. A lot of API wrappers!
I write API wrappers
- 15. {"chart":{
"issueDate":2006-03-04,
"description":"Chart",
"chartItems":{
"firstPosition":1,
"totalReturned":15,
"totalRecords":25663,
"chartItem":[{
"songName":"Lonely Runs Both Ways",
"artistName":"Alison Krauss + Union Station",
"peek":1,
"catalogNo":"610525",
"rank":1,
"exrank":1,
"weeksOn":65,
"albumId":655684,
...
}}
- 16. {"chart":{
"issueDate":2006-03-04,
"description":"Chart",
"chartItems":{
"firstPosition":1,
"totalReturned":15,
"totalRecords":25663,
"chartItem":[{
"songName":"Lonely Runs Both Ways",
"artistName":"Alison Krauss + Union Station",
"peek":1,
"catalogNo":"610525",
"rank":1,
"exrank":1,
"weeksOn":65,
"albumId":655684,
...
}}
- 18. {"chart":{
"issueDate":2006-03-04,
"description":"Chart",
"chartItems":{
"firstPosition":1,
"totalReturned":15,
"totalRecords":25663,
"chartItem":[{
"songName":"Lonely Runs Both Ways",
"artistName":"Alison Krauss + Union Station",
"peek":1,
"catalogNo":"610525",
"rank":1,
"exrank":1,
"weeksOn":65,
"albumId":655684,
...
}}
- 19. {"chart":{
"issue_date":2006-03-04,
"description":"Chart",
"chart_items":{
"first_position":1,
"total_returned":15,
"total_records":25663,
"chart_item":[{
"song_name":"Lonely Runs Both Ways",
"artist_name":"Alison Krauss + Union Station",
"peek":1,
"catalog_no":"610525",
"rank":1,
"exrank":1,
"weeks_on":65,
"album_id":655684,
...
}}
- 21. # Retrieve the details about a user by email
#
# +email+ (Required)
# The email of the user to look within. To run getInfoByEmail on
multiple addresses, simply pass a comma-separated list of valid email
addresses.
#
def self.info_by_email(email)
email = email.join(',') if email.is_a?(Array)
Mash.new(self.get('/',
! ! :query => {
! ! ! :method => 'user.getInfoByEmail',
! ! ! :email => email
}.merge(Upcoming.default_options))).rsp.user
end
- 22. # Retrieve the details about a user by email
#
# +email+ (Required)
# The email of the user to look within. To run getInfoByEmail on
multiple addresses, simply pass a comma-separated list of valid email
addresses.
#
More Ruby like th an
def self.info_by_email(email)
email = email.join(',') if email.is_a?(Array)
Mash.new(self.get('/',
! ! :query => {
! ! ! :method => 'user.getInfoByEmail',
! ! ! :email => email
}.merge(Upcoming.default_options))).rsp.user
end
- 27. Method chaining
stores = client.stores({:area => ['76227', 50]}).products({:salePrice => {'$gt' => 3000}}).fetch.stores
- 29. Method chaining Bang for POST
client.statuses.update.json! :status => 'this status is from grackle'
- 32. SOME TWITTER EXAMPLES
Twitter Auth from @mbleigh
Wrapping
user.twitter.post(
'/statuses/update.json',
'status' => 'Tweet, tweet!'
)
Wrapping... with style
Grackle from @hayesdavis
client.statuses.update.json! :status => 'Tweet, tweet!'
Abstraction
Twitter from @jnunemaker
client.update('Tweet, tweet!')
- 53. HTTParty
- Ruby module
- GET, POST, PUT, DELETE
- basic_auth, base_uri, default_params, etc.
- Net::HTTP for transport
- Crack parses JSON and XML
- 54. HTTParty
class Delicious
include HTTParty
base_uri 'https://api.del.icio.us/v1'
def initialize(u, p)
@auth = {:username => u, :password => p}
end
...
def recent(options={})
options.merge!({:basic_auth => @auth})
self.class.get('/posts/recent', options)
end
...
- 57. RestClient
- Simple DSL
- ActiveResource support
- Built-in shell
RestClient.post(
! 'http://example.com/resource',
:param1 => 'one',
:nested => { :param2 => 'two' }
)
- 60. Weary
declare "foo" do |r|
r.url = "path/to/foo"
r.via = :post
r.requires = [:id, :bar]
r.with = [:blah] becomes
r.authenticates = false
r.follows = false
r.headers = {'Accept' => 'text/html'}
end
client.foo :id => 123, :bar => 'baz'
- 65. Faraday
url = 'http://api.twitter.com/1'
conn = Faraday::Connection.new(:url => url ) do |builder|
builder.adapter Faraday.default_adapter
builder.use Faraday::Response::MultiJson
builder.use Faraday::Response::Mashify
end
resp = conn.get do |req|
req.url '/users/show.json', :screen_name => 'pengwynn'
end
u = resp.body
u.name
# => "Wynn Netherland"
- 68. My current stack
- Faraday
- Faraday Middleware
- Hashie
- Multi JSON
- OAuth, OAuth2 as needed
- 72. If you have an iOS app, you have an API ;-)
Charles Proxy
- 76. Artifice
http://github.com/wycats/artifice
Artifice.activate_with(rack_endpoint) do
# make some requests using Net::HTTP
end
a @wycats joint
- 79. ShamRack Rack 'em up!
ShamRack.at("rackup.xyz").rackup do
use Some::Middleware
use Some::Other::Middleware
run MyApp.new
end