Musashi

Musashi is a RestClient that follows relations or links.

Example

Protocol

curl -i http://foo.com/customers/1
{
  "id":    "1",
  "login": "foo",
  "name":  "Foo Bar",
  "phones": [
    {
      "number": "51156782323",
      "extension": ""
    },
    {
      "number": "51156782323",
      "extension": "2345"
    }
  ],
  "emails": ["[email protected]",
             "[email protected]",
             "[email protected]"],
  "address": {
    "street":      "No Where",
    "number":      "123",
    "complement":  "4-F",
    "district":    "Pinheiros",
    "city":        "São Paulo",
    "state":       "SP",
    "country":     "Brazil",
    "postal_code": "000000"
  },
  "link": [
    {
      "rel":  "items",
      "href": "http://bar.com/customers/1/items"
    }
  ]
}

curl -i http://bar.com/customer/1/items
{"entries" : 
  [ "http://bazz.com/items/1", "http://bazz.com/items/2" ]
}

curl -i http://bazz.com/items/1
{"name":"bazz item"}

Code

With class

require 'musashi'
class Customer
  include Musashi
  include Musashi::Resource::JSON
  attr_accessor :id
  endpoint = '"http://foo.com/customers/#{id}"'
end

customer.id = '1'
puts customer.
puts customer.phones.first.number
puts customer.email.first
puts customer.address.number
customer.items.entries.each do |i|
  puts i.name
end

Without class

require 'musashi'
customer = Musashi::Resource.new('http://foo.com/customer/1', :format => :json)

puts custome.id
puts customer.
puts customer.phones.first.number
puts customer.email.first
puts customer.address.number
customer.items.entries.each do |i|
  puts i.name
end