-
-
Save aaronmcadam/3303289 to your computer and use it in GitHub Desktop.
Use ActiveResource with non-Rails REST API
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
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