Skip to content

Instantly share code, notes, and snippets.

@elrikdante
Forked from jeroenr/Accommodation.rb
Created April 12, 2016 04:47
Show Gist options
  • Save elrikdante/45b0459a28f4bf06651f17eb880e0d7b to your computer and use it in GitHub Desktop.
Save elrikdante/45b0459a28f4bf06651f17eb880e0d7b 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