nineflats-api 0.0.8 → 0.0.9
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/nineflats-api/base.rb +4 -10
- data/lib/nineflats-api/booking.rb +27 -0
- data/lib/nineflats-api/calendar.rb +3 -7
- data/lib/nineflats-api/client.rb +9 -8
- data/lib/nineflats-api/errors.rb +1 -0
- data/lib/nineflats-api/helpers.rb +2 -0
- data/lib/nineflats-api/paginated_array.rb +1 -1
- data/lib/nineflats-api/photo.rb +1 -4
- data/lib/nineflats-api/place.rb +25 -78
- data/lib/nineflats-api/prices.rb +6 -10
- data/lib/nineflats-api/requests.rb +107 -5
- data/lib/nineflats-api/review.rb +1 -4
- data/lib/nineflats-api/user.rb +18 -22
- data/lib/nineflats-api/version.rb +1 -1
- data/spec/client_spec.rb +25 -0
- data/spec/fixtures/place.json +6 -6
- data/spec/fixtures/place_calendar.json +2 -2
- data/spec/fixtures/place_photos.json +1 -1
- data/spec/fixtures/place_prices.json +1 -1
- data/spec/fixtures/place_reviews.json +1 -1
- data/spec/fixtures/search_result.json +56 -56
- data/spec/fixtures/user.json +2 -2
- data/spec/fixtures/user_bookings.json +107 -0
- data/spec/fixtures/user_favorites.json +44 -44
- data/spec/place_calendar_spec.rb +13 -12
- data/spec/place_photos_spec.rb +19 -20
- data/spec/place_prices_spec.rb +22 -22
- data/spec/place_reviews_spec.rb +20 -21
- data/spec/place_spec.rb +14 -14
- data/spec/search_spec.rb +12 -21
- data/spec/user_spec.rb +60 -51
- metadata +17 -12
data/lib/nineflats-api/base.rb
CHANGED
@@ -1,23 +1,17 @@
|
|
1
1
|
module Nineflats
|
2
2
|
class Base
|
3
|
+
attr_accessor :raw_data
|
4
|
+
|
3
5
|
def self.client_app_key
|
4
6
|
@@client_app_key
|
5
7
|
end
|
6
|
-
|
8
|
+
|
7
9
|
def self.client_app_key=(key)
|
8
10
|
@@client_app_key = key
|
9
11
|
end
|
10
12
|
|
11
|
-
def self.base_url
|
12
|
-
"http://api.9flats.com/api/v1"
|
13
|
-
end
|
14
|
-
|
15
|
-
def self.api_call
|
16
|
-
raise "override me!"
|
17
|
-
end
|
18
|
-
|
19
13
|
def self.object_link(name, array)
|
20
|
-
|
14
|
+
array.select{ |link|
|
21
15
|
link["rel"] == name
|
22
16
|
}.first["href"]
|
23
17
|
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
module Nineflats
|
2
|
+
class Booking < Base
|
3
|
+
|
4
|
+
attr_accessor :checkin_date, :checkout_date, :nights, :number_of_guests, :status
|
5
|
+
attr_accessor :host, :links, :place
|
6
|
+
|
7
|
+
def initialize(json)
|
8
|
+
@raw_data = json
|
9
|
+
booking = json.first[1]
|
10
|
+
|
11
|
+
@checkin_date = booking["checkin_date"]
|
12
|
+
@checkout_date = booking["checkout_date"]
|
13
|
+
@nights = booking["nights"]
|
14
|
+
@number_of_guests = booking["number_of_guests"]
|
15
|
+
@status = booking["status"]
|
16
|
+
|
17
|
+
@host = User.new({"user" => booking["host"]})
|
18
|
+
@place = Place.new({"place" => booking["place"]})
|
19
|
+
|
20
|
+
@links = {}
|
21
|
+
booking['links'].each do |link|
|
22
|
+
@links[link['rel']] = link['href']
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
end
|
27
|
+
end
|
@@ -1,17 +1,13 @@
|
|
1
1
|
module Nineflats
|
2
2
|
class Calendar < Base
|
3
3
|
attr_accessor :year, :month, :days
|
4
|
-
|
5
|
-
def initialize(json)
|
6
|
-
calendar = json.first[1]
|
7
4
|
|
5
|
+
def initialize(calendar)
|
6
|
+
@raw_data = calendar
|
8
7
|
@year = calendar["year"]
|
9
8
|
@month = calendar["month"]
|
10
9
|
@days = calendar["days"]
|
11
10
|
end
|
12
|
-
|
13
|
-
def self.api_call(slug, year, month)
|
14
|
-
base_url + "/places/#{slug}/calendar/#{year}/#{month}?client_id=#{Nineflats::Base.client_app_key}"
|
15
|
-
end
|
11
|
+
|
16
12
|
end
|
17
13
|
end
|
data/lib/nineflats-api/client.rb
CHANGED
@@ -1,12 +1,6 @@
|
|
1
1
|
require 'nineflats-api/requests'
|
2
2
|
module Nineflats
|
3
3
|
class Client < Base
|
4
|
-
# API_BASE_URI = "api.9flats.com/api/v1"
|
5
|
-
# API_BASE_URI = "http://localhost.com:3000/api/v1"
|
6
|
-
WEB_BASE_URI = "http://www.9flats.com"
|
7
|
-
# WEB_BASE_URI = "http://localhost.com:3000"
|
8
|
-
|
9
|
-
attr_accessor :request_token
|
10
4
|
attr_accessor :access_token
|
11
5
|
attr_reader :consumer
|
12
6
|
|
@@ -20,6 +14,14 @@ module Nineflats
|
|
20
14
|
@@client
|
21
15
|
end
|
22
16
|
|
17
|
+
def self.api_site_url
|
18
|
+
@@api_site_url ||= "http://www.9flats.com"
|
19
|
+
end
|
20
|
+
|
21
|
+
def self.api_site_url=(url)
|
22
|
+
@@api_site_url = url
|
23
|
+
end
|
24
|
+
|
23
25
|
def self.client
|
24
26
|
@@client
|
25
27
|
end
|
@@ -44,12 +46,11 @@ module Nineflats
|
|
44
46
|
|
45
47
|
def initialize(api_key, api_secret, options={})
|
46
48
|
@consumer = ::OAuth::Consumer.new(api_key, api_secret, {
|
47
|
-
:site =>
|
49
|
+
:site => Nineflats::Client.api_site_url,
|
48
50
|
:scheme => :header,
|
49
51
|
:http_method => :post
|
50
52
|
})
|
51
53
|
@access_token = options[:access_token]
|
52
|
-
# Client.default_options[:simple_oauth] = { :consumer => @consumer, :method => 'HMAC-SHA1' }.merge(options)
|
53
54
|
end
|
54
55
|
|
55
56
|
end
|
data/lib/nineflats-api/errors.rb
CHANGED
data/lib/nineflats-api/photo.rb
CHANGED
@@ -3,15 +3,12 @@ module Nineflats
|
|
3
3
|
attr_accessor :title, :caption, :url
|
4
4
|
|
5
5
|
def initialize(json)
|
6
|
+
@raw_data = json
|
6
7
|
photo = json.first[1]
|
7
8
|
|
8
9
|
@title = photo["title"]
|
9
10
|
@caption = photo["caption"]
|
10
11
|
@url = photo["url"]
|
11
12
|
end
|
12
|
-
|
13
|
-
def self.api_call(slug)
|
14
|
-
base_url + "/places/#{slug}/photos?client_id=#{Nineflats::Base.client_app_key}"
|
15
|
-
end
|
16
13
|
end
|
17
14
|
end
|
data/lib/nineflats-api/place.rb
CHANGED
@@ -1,19 +1,18 @@
|
|
1
1
|
module Nineflats
|
2
2
|
class Place < Base
|
3
|
-
attr_accessor :name, :city, :slug, :zipcode, :number_of_beds, :number_of_bedrooms,
|
4
|
-
:currency, :lat, :lng, :district, :number_of_bathrooms,
|
3
|
+
attr_accessor :name, :city, :slug, :zipcode, :number_of_beds, :number_of_bedrooms,
|
4
|
+
:currency, :lat, :lng, :district, :number_of_bathrooms,
|
5
5
|
:minimum_nights, :maximum_nights, :bed_type, :content_language,
|
6
|
-
:size, :house_rules, :pets_around, :bathroom_type, :cleaning_fee,
|
6
|
+
:size, :house_rules, :pets_around, :bathroom_type, :cleaning_fee,
|
7
7
|
:charge_per_extra_person_limit, :favorites_count, :amenities_list,
|
8
8
|
:featured_photo_url, :price, :charge_per_extra_person, :country,
|
9
9
|
:category, :place_type, :bed_type, :bathroom_type, :description,
|
10
|
-
:host, :
|
11
|
-
|
12
|
-
:prices, :reviews, :photos
|
13
|
-
|
10
|
+
:host, :links, :prices, :reviews, :photos
|
11
|
+
|
14
12
|
def initialize(json)
|
13
|
+
@raw_data = json
|
15
14
|
place = json.first[1]
|
16
|
-
|
15
|
+
|
17
16
|
@name = place["name"]
|
18
17
|
@city = place["city"]
|
19
18
|
@currency = place["currency"]
|
@@ -45,93 +44,41 @@ module Nineflats
|
|
45
44
|
@district = place["district"]
|
46
45
|
@description = place["description"]
|
47
46
|
|
48
|
-
@host = User.new({"user" => place["host"]})
|
49
|
-
|
50
|
-
@self_url = Nineflats::Base.object_link("self", place["links"])
|
51
|
-
@full_url = Nineflats::Base.object_link("full", place["links"])
|
52
|
-
@photos_url = Nineflats::Base.object_link("photos", place["links"])
|
53
|
-
@prices_url = Nineflats::Base.object_link("prices", place["links"])
|
54
|
-
@reviews_url = Nineflats::Base.object_link("reviews", place["links"])
|
55
|
-
@calendar_current_month_url = Nineflats::Base.object_link("calendar: current month", place["links"])
|
56
|
-
@calendar_next_month_url = Nineflats::Base.object_link("calendar: next month", place["links"])
|
57
|
-
end
|
47
|
+
@host = User.new({"user" => place["host"]}) if place["host"]
|
58
48
|
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
queries = params.collect do |key, value|
|
63
|
-
"&search[#{key}]=#{value}"
|
49
|
+
@links = {}
|
50
|
+
place['links'].each do |link|
|
51
|
+
@links[link['rel']] = link['href']
|
64
52
|
end
|
65
|
-
|
66
|
-
search_url = base_url + "/places?client_id=#{Nineflats::Base.client_app_key}" + queries.join
|
67
|
-
|
68
|
-
Place.search_result(search_url)
|
69
53
|
end
|
70
54
|
|
71
|
-
def self.
|
72
|
-
|
73
|
-
|
74
|
-
places = json["places"].collect do |place_json|
|
75
|
-
Place.new(place_json)
|
76
|
-
end
|
77
|
-
|
78
|
-
result = Nineflats::PaginatedArray.new(places)
|
79
|
-
|
80
|
-
result.total_entries = json["total_entries"]
|
81
|
-
result.total_pages = json["total_pages"]
|
82
|
-
result.current_page = json["current_page"]
|
83
|
-
result.per_page = json["per_page"]
|
84
|
-
|
85
|
-
result.self_url = Nineflats::Base.object_link("self", json["links"])
|
86
|
-
result.full_url = Nineflats::Base.object_link("full", json["links"])
|
87
|
-
result.next_page_url = Nineflats::Base.object_link("next_page", json["links"])
|
88
|
-
result
|
55
|
+
def self.search(params={})
|
56
|
+
Nineflats::Client.places(params)
|
89
57
|
end
|
90
58
|
|
59
|
+
|
91
60
|
def self.fetch(slug, lang)
|
92
|
-
|
61
|
+
Nineflats::Client.place(slug, :language => lang)
|
93
62
|
end
|
94
|
-
|
63
|
+
|
95
64
|
def prices
|
96
65
|
return @prices if @prices
|
97
|
-
|
98
|
-
json = Helpers.get_data(Prices.api_call(slug))
|
99
|
-
|
100
|
-
@prices = Prices.new(json) if json && json["place_prices"]
|
66
|
+
@prices = Nineflats::Client.place_prices(@slug)
|
101
67
|
end
|
102
|
-
|
68
|
+
|
103
69
|
def reviews
|
104
70
|
return @reviews if @reviews
|
105
|
-
|
106
|
-
json = Helpers.get_data(Review.api_call(slug))
|
107
|
-
|
108
|
-
if json && json["reviews"]
|
109
|
-
@reviews = json["reviews"].collect do |review_hash|
|
110
|
-
Review.new(review_hash)
|
111
|
-
end
|
112
|
-
end
|
71
|
+
@reviews = Nineflats::Client.place_reviews(@slug)
|
113
72
|
end
|
114
|
-
|
73
|
+
|
115
74
|
def photos
|
116
75
|
return @photos if @photos
|
117
|
-
|
118
|
-
json = Helpers.get_data(Photo.api_call(slug))
|
119
|
-
|
120
|
-
if json && json["place_photos"]
|
121
|
-
@photos = json["place_photos"].collect do |photo_hash|
|
122
|
-
Photo.new(photo_hash)
|
123
|
-
end
|
124
|
-
end
|
76
|
+
@photos = Nineflats::Client.place_photos(@slug)
|
125
77
|
end
|
126
|
-
|
127
|
-
def calendar(year, month)
|
128
|
-
json = Helpers.get_data(Calendar.api_call(slug, year, month))
|
129
78
|
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
def self.api_call(slug, lang)
|
134
|
-
base_url + "/places/#{slug}?client_id=#{Nineflats::Base.client_app_key}&lang=#{lang}"
|
79
|
+
def calendar(year, month)
|
80
|
+
Nineflats::Client.place_calendar(@slug, year, month)
|
135
81
|
end
|
82
|
+
|
136
83
|
end
|
137
|
-
end
|
84
|
+
end
|
data/lib/nineflats-api/prices.rb
CHANGED
@@ -1,25 +1,21 @@
|
|
1
1
|
module Nineflats
|
2
2
|
class Prices < Base
|
3
|
-
attr_accessor :currency, :default_price, :weekend_night_price,
|
3
|
+
attr_accessor :currency, :default_price, :weekend_night_price,
|
4
4
|
:weekly_discount_in_percent, :monthly_discount_in_percent,
|
5
5
|
:seasons
|
6
|
-
|
7
|
-
def initialize(
|
8
|
-
|
9
|
-
|
6
|
+
|
7
|
+
def initialize(prices)
|
8
|
+
@raw_data = prices
|
10
9
|
@currency = prices["currency"]
|
11
10
|
@default_price = prices["default_price"]
|
12
11
|
@weekend_night_price = prices["weekend_night_price"]
|
13
12
|
@weekly_discount_in_percent = prices["weekly_discount_in_percent"]
|
14
13
|
@monthly_discount_in_percent = prices["monthly_discount_in_percent"]
|
15
|
-
|
14
|
+
|
16
15
|
@seasons = prices["seasons"].collect do |season|
|
17
16
|
Season.new(season)
|
18
17
|
end
|
19
18
|
end
|
20
|
-
|
21
|
-
def self.api_call(slug)
|
22
|
-
base_url + "/places/#{slug}/prices?client_id=#{Nineflats::Base.client_app_key}"
|
23
|
-
end
|
19
|
+
|
24
20
|
end
|
25
21
|
end
|
@@ -6,6 +6,89 @@ module Nineflats
|
|
6
6
|
|
7
7
|
module ClassMethods
|
8
8
|
|
9
|
+
def places(search_options)
|
10
|
+
response = if search_options[:url]
|
11
|
+
consumer.request(:get, search_options[:url])
|
12
|
+
else
|
13
|
+
consumer.request(:get, "/api/v1/places?#{Nineflats::QueryStringNormalizer.normalize({:client_id => consumer.key, :search => search_options})}")
|
14
|
+
end
|
15
|
+
json = JSON.parse(response.body)
|
16
|
+
if json["places"]
|
17
|
+
places = json["places"].collect do |place_hash|
|
18
|
+
Place.new(place_hash)
|
19
|
+
end
|
20
|
+
result = Nineflats::PaginatedArray.new(places)
|
21
|
+
|
22
|
+
result.total_entries = json["total_entries"]
|
23
|
+
result.total_pages = json["total_pages"]
|
24
|
+
result.current_page = json["current_page"]
|
25
|
+
result.per_page = json["per_page"]
|
26
|
+
|
27
|
+
result.self_url = Nineflats::Base.object_link("self", json["links"])
|
28
|
+
result.full_url = Nineflats::Base.object_link("full", json["links"])
|
29
|
+
result.next_page_url = Nineflats::Base.object_link("next_page", json["links"])
|
30
|
+
result
|
31
|
+
else
|
32
|
+
raise Nineflats::Error.new(json['error'])
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
def place(slug, options={})
|
37
|
+
params = {:client_id => consumer.key}
|
38
|
+
params[:lang] = options[:language] if options[:language]
|
39
|
+
url = "/api/v1/places/#{slug}?#{Nineflats::QueryStringNormalizer.normalize({:client_id => consumer.key})}"
|
40
|
+
response = if client.access_token
|
41
|
+
client.access_token.request(:get, url)
|
42
|
+
else
|
43
|
+
consumer.request(:get, url)
|
44
|
+
end
|
45
|
+
Nineflats::Place.new(JSON.parse(response.body))
|
46
|
+
end
|
47
|
+
|
48
|
+
def place_calendar(place_slug, year, month)
|
49
|
+
response = consumer.request(:get, "/api/v1/places/#{place_slug}/calendar/#{year}/#{month}?#{Nineflats::QueryStringNormalizer.normalize({:client_id => consumer.key})}")
|
50
|
+
json = JSON.parse(response.body)
|
51
|
+
if json["calendar"]
|
52
|
+
Calendar.new(json['calendar'])
|
53
|
+
else
|
54
|
+
raise Nineflats::Error.new(json['error'])
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
def place_prices(place_slug)
|
59
|
+
response = consumer.request(:get, "/api/v1/places/#{place_slug}/prices?#{Nineflats::QueryStringNormalizer.normalize({:client_id => consumer.key})}")
|
60
|
+
json = JSON.parse(response.body)
|
61
|
+
if json["place_prices"]
|
62
|
+
Prices.new(json['place_prices'])
|
63
|
+
else
|
64
|
+
raise Nineflats::Error.new(json['error'])
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
def place_reviews(place_slug)
|
69
|
+
response = consumer.request(:get, "/api/v1/places/#{place_slug}/reviews?#{Nineflats::QueryStringNormalizer.normalize({:client_id => consumer.key})}")
|
70
|
+
json = JSON.parse(response.body)
|
71
|
+
if json["reviews"]
|
72
|
+
json["reviews"].collect do |review_hash|
|
73
|
+
Review.new(review_hash)
|
74
|
+
end
|
75
|
+
else
|
76
|
+
raise Nineflats::Error.new(json['error'])
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
def place_photos(place_slug)
|
81
|
+
response = consumer.request(:get, "/api/v1/places/#{place_slug}/photos?#{Nineflats::QueryStringNormalizer.normalize({:client_id => consumer.key})}")
|
82
|
+
json = JSON.parse(response.body)
|
83
|
+
if json["place_photos"]
|
84
|
+
json["place_photos"].collect do |photo_hash|
|
85
|
+
Photo.new(photo_hash)
|
86
|
+
end
|
87
|
+
else
|
88
|
+
raise Nineflats::Error.new(json['error'])
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
9
92
|
def user(user_id)
|
10
93
|
url = "/api/v1/users/#{user_id}?#{Nineflats::QueryStringNormalizer.normalize({:client_id => consumer.key})}"
|
11
94
|
response = if client.access_token
|
@@ -13,16 +96,35 @@ module Nineflats
|
|
13
96
|
else
|
14
97
|
consumer.request(:get, url)
|
15
98
|
end
|
16
|
-
JSON.parse(response.body)
|
17
|
-
end
|
99
|
+
Nineflats::User.new(JSON.parse(response.body))
|
100
|
+
end
|
18
101
|
|
19
|
-
def
|
102
|
+
def user_favorites(user_id)
|
103
|
+
response = consumer.request(:get, "/api/v1/users/#{user_id}/favorites?#{Nineflats::QueryStringNormalizer.normalize({:client_id => consumer.key})}")
|
104
|
+
json = JSON.parse(response.body)
|
105
|
+
if json["places"]
|
106
|
+
json["places"].collect do |place_hash|
|
107
|
+
Place.new(place_hash)
|
108
|
+
end
|
109
|
+
else
|
110
|
+
raise Nineflats::Error.new(json['error'])
|
111
|
+
end
|
112
|
+
end
|
113
|
+
|
114
|
+
def user_bookings(user_id)
|
20
115
|
raise Nineflats::NotAuthenticatedException.new("User is not authenticated yet!") unless Client.client.authorized?
|
21
116
|
|
22
117
|
params = {:client_id => consumer.key}
|
23
118
|
response = client.access_token.request(:get, "/api/v1/users/#{user_id}/bookings?#{Nineflats::QueryStringNormalizer.normalize(params)}")
|
24
|
-
JSON.parse(response.body)
|
25
|
-
|
119
|
+
json = JSON.parse(response.body)
|
120
|
+
if json && json["bookings"]
|
121
|
+
json["bookings"].collect do |booking_hash|
|
122
|
+
Booking.new(booking_hash)
|
123
|
+
end
|
124
|
+
else
|
125
|
+
raise Nineflats::Error.new(json['error'])
|
126
|
+
end
|
127
|
+
end
|
26
128
|
|
27
129
|
end
|
28
130
|
end
|