nineflats-api 0.0.8 → 0.0.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -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
- link = array.select{ |link|
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
@@ -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 => WEB_BASE_URI,
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
@@ -1,3 +1,4 @@
1
1
  module Nineflats
2
+ class Error < StandardError; end
2
3
  class NotAuthenticatedException < StandardError; end
3
4
  end
@@ -40,6 +40,8 @@ module Nineflats
40
40
  html += "</div>"
41
41
  end
42
42
  html += "</div>"
43
+ elsif object.is_a?(Nineflats::Base)
44
+ html = ruby_to_html(html, object.raw_data)
43
45
  else
44
46
  html += "<div class=\"string\">#{object.to_s}</div>"
45
47
  end
@@ -4,7 +4,7 @@ module Nineflats
4
4
  :self_url, :full_url, :next_page_url
5
5
 
6
6
  def next_page
7
- Place.search_result(next_page_url)
7
+ Nineflats::Client.places(:url => next_page_url)
8
8
  end
9
9
  end
10
10
  end
@@ -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
@@ -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, :self_url, :full_url, :photos_url, :prices_url, :reviews_url,
11
- :calendar_current_month_url, :calendar_next_month_url,
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
- def self.search(params = nil)
60
- params ||= {}
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.search_result(search_url)
72
- json = Helpers.get_data(search_url)
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
- Place.new(Helpers.get_data(Place.api_call(slug, lang)))
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
- Calendar.new(json) if json && json["calendar"]
131
- end
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
@@ -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(json)
8
- prices = json.first[1]
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 # user
99
+ Nineflats::User.new(JSON.parse(response.body))
100
+ end
18
101
 
19
- def bookings(user_id)
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
- end # bookings
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