Skip to content

Instantly share code, notes, and snippets.

@jeroenr
Created August 7, 2012 17:09
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save jeroenr/3287375 to your computer and use it in GitHub Desktop.
Save jeroenr/3287375 to your computer and use it in GitHub Desktop.
Use ActiveResource with non-Rails REST API
class Accommodation < ActiveResource::Base
class << self
def element_path(id, prefix_options = {}, query_options = nil)
prefix_options, query_options = split_options(prefix_options) if query_options.nil?
"#{prefix(prefix_options)}#{collection_name}/#{id}#{query_string(query_options)}"
end
def collection_path(prefix_options = {}, query_options = nil)
prefix_options, query_options = split_options(prefix_options) if query_options.nil?
"#{prefix(prefix_options)}#{collection_name}#{query_string(query_options)}"
end
def instantiate_collection(collection, prefix_options = {})
super collection['accommodations'], prefix_options
end
end
self.site = "http://api.accommmodations.com/"
def persisted?
false
end
end
{
totalNumberOfResults: 818,
accommodations: [
{
id: "bulgarije-burgas-sunny_beach-best_hotel_ever",
name: "Best Hotel Ever",
city: {
code: "sunny_beach",
name: "Sunny Beach"
},
region: {
code: "burgas",
name: "Burgas"
},
country: {
code: "bulgarije",
name: "Bulgarije"
}
},
{
id: "bulgarije-burgas-sunny_beach-hotel_awesome",
name: "Hotel Awesome",
city: {
code: "sunny_beach",
name: "Sunny Beach"
},
region: {
code: "burgas",
name: "Burgas"
},
country: {
code: "bulgarije",
name: "Bulgarije"
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment