nineflats-api 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/.rvmrc ADDED
@@ -0,0 +1 @@
1
+ rvm use --create ruby-1.9.2-p180-patched@lysbon
data/Gemfile CHANGED
@@ -1,4 +1,6 @@
1
1
  source "http://rubygems.org"
2
2
 
3
- # Specify your gem's dependencies in nineflats-api.gemspec
4
3
  gemspec
4
+
5
+ gem 'rspec'
6
+ gem 'fakeweb'
data/Rakefile CHANGED
@@ -1 +1,5 @@
1
1
  require "bundler/gem_tasks"
2
+ require 'rspec/core/rake_task'
3
+
4
+ desc "Run all RSpec tests"
5
+ RSpec::Core::RakeTask.new(:spec)
@@ -1,52 +1 @@
1
- require "nineflats-api/version"
2
- require "open-uri"
3
- require "json"
4
-
5
- module Nineflats
6
- module API
7
- module Helpers
8
- def self.get_data(url)
9
- data = ""
10
- begin
11
- open(url) do |f|
12
- data = JSON.parse(f.read)
13
- end
14
- rescue => e
15
- data = "Something went wrong: #{e} --- This means either the data you requested is simply null, the data you entered was wrong, or the API call is broken."
16
- end
17
- data
18
- end
19
-
20
- def self.pretty_print(data)
21
- self.ruby_to_html(nil, data) + "</html>"
22
- end
23
-
24
- private
25
-
26
- def self.ruby_to_html(html, object)
27
- html ||= "<div class=\"object\">"
28
- if object.is_a?(Hash)
29
- html += "<div class=\"hash\">"
30
- object.each do |key, value|
31
- html += "<div class=\"key\">#{key}</div>"
32
- html += "<div class=\"value\">"
33
- html = ruby_to_html(html, value)
34
- html += "</div>"
35
- end
36
- html += "</div>"
37
- elsif object.is_a?(Array)
38
- html += "<div class=\"array\">"
39
- object.each do |element|
40
- html += "<div class=\"element\">"
41
- html = ruby_to_html(html, element)
42
- html += "</div>"
43
- end
44
- html += "</div>"
45
- else
46
- html += "<div class=\"string\">#{object.to_s}</div>"
47
- end
48
- html
49
- end
50
- end
51
- end
52
- end
1
+ Dir[File.dirname(__FILE__) + "/nineflats-api/**/*.rb"].sort.each { |f| require File.expand_path(f) }
@@ -0,0 +1,19 @@
1
+ module Nineflats
2
+ class Base
3
+ def self.client_app_key
4
+ @@client_app_key
5
+ end
6
+
7
+ def self.client_app_key=(key)
8
+ @@client_app_key = key
9
+ end
10
+
11
+ def self.base_url
12
+ "http://api.9flats.com/api"
13
+ end
14
+
15
+ def self.api_call
16
+ raise "override me!"
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,49 @@
1
+ require "open-uri"
2
+ require "json"
3
+
4
+ module Nineflats
5
+ module Helpers
6
+ def self.get_data(url)
7
+ data = ""
8
+ begin
9
+ open(url) do |f|
10
+ data = JSON.parse(f.read)
11
+ end
12
+ rescue => e
13
+ data = "Something went wrong: #{e} --- This means either the data you requested is simply null, the data you entered was wrong, or the API call is broken."
14
+ end
15
+ data
16
+ end
17
+
18
+ def self.pretty_print(data)
19
+ self.ruby_to_html(nil, data) + "</html>"
20
+ end
21
+
22
+ private
23
+
24
+ def self.ruby_to_html(html, object)
25
+ html ||= "<div class=\"object\">"
26
+ if object.is_a?(Hash)
27
+ html += "<div class=\"hash\">"
28
+ object.each do |key, value|
29
+ html += "<div class=\"key\">#{key}</div>"
30
+ html += "<div class=\"value\">"
31
+ html = ruby_to_html(html, value)
32
+ html += "</div>"
33
+ end
34
+ html += "</div>"
35
+ elsif object.is_a?(Array)
36
+ html += "<div class=\"array\">"
37
+ object.each do |element|
38
+ html += "<div class=\"element\">"
39
+ html = ruby_to_html(html, element)
40
+ html += "</div>"
41
+ end
42
+ html += "</div>"
43
+ else
44
+ html += "<div class=\"string\">#{object.to_s}</div>"
45
+ end
46
+ html
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,63 @@
1
+ module Nineflats
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,
5
+ :minimum_nights, :maximum_nights, :bed_type, :content_language,
6
+ :size, :house_rules, :pets_around, :bathroom_type, :cleaning_fee,
7
+ :charge_per_extra_person_limit, :favorites_count, :amenities_list,
8
+ :featured_photo_url, :price, :charge_per_extra_person, :country,
9
+ :category, :place_type, :bed_type, :bathroom_type,
10
+ :host, :self_url, :full_url, :prices
11
+
12
+ def initialize(json)
13
+ place = json.first[1]
14
+
15
+ @name = place["name"]
16
+ @city = place["city"]
17
+ @currency = place["currency"]
18
+ @slug = place["slug"]
19
+ @zipcode = place["zipcode"]
20
+ @number_of_beds = place["number_of_beds"]
21
+ @number_of_bedrooms = place["number_of_bedrooms"]
22
+ @number_of_bathrooms = place["number_of_bathrooms"]
23
+ @charge_per_extra_person = place["charge_per_extra_person"]
24
+ @content_language = place["content_language"]
25
+ @minimum_nights = place["minimum_nights"]
26
+ @maximum_nights = place["maximum_nights"]
27
+ @bed_type = place["bed_type"]
28
+ @size = place["size"]
29
+ @house_rules = place["house_rules"]
30
+ @pets_around = place["pets_around"]
31
+ @bathroom_type = place["bathroom_type"]
32
+ @cleaning_fee = place["cleaning_fee"]
33
+ @charge_per_extra_person_limit = place["charge_per_extra_person_limit"]
34
+ @favorites_count = place["favorites_count"]
35
+ @amenities_list = place["amenities_list"]
36
+ @featured_photo_url = place["featured_photo_url"]
37
+ @price = place["price"]
38
+ @country = place["country"]
39
+ @category = place["category"]
40
+ @place_type = place["place_type"]
41
+ @lat = place["lat"]
42
+ @lng = place["lng"]
43
+ @district = place["district"]
44
+
45
+ @host = User.new({"user"=>place["host"]})
46
+
47
+ @self_url = place["links"].first.select{|link| link["rel"] == "self"}["href"]
48
+ @full_url = place["links"].first.select{|link| link["rel"] == "full"}["href"]
49
+ end
50
+
51
+ def self.fetch(slug, lang)
52
+ Place.new(Helpers.get_data(Place.api_call(slug, lang)))
53
+ end
54
+
55
+ def prices
56
+ @prices = Prices.new(Helpers.get_data(Prices.api_call(slug)))
57
+ end
58
+
59
+ def self.api_call(slug, lang)
60
+ base_url + "/places/#{slug}.json?client_id=#{Nineflats::Base.client_app_key}&language=#{lang}"
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,26 @@
1
+ module Nineflats
2
+ class Prices < Base
3
+ attr_accessor :currency, :default_price, :weekend_night_price,
4
+ :weekly_discount_in_percent, :monthly_discount_in_percent,
5
+ :seasons
6
+
7
+ def initialize(json)
8
+ prices = json.first[1]
9
+
10
+ @currency = prices["currency"]
11
+ @default_price = prices["default_price"]
12
+ @weekend_night_price = prices["weekend_night_price"]
13
+ @weekly_discount_in_percent = prices["weekly_discount_in_percent"]
14
+ @monthly_discount_in_percent = prices["monthly_discount_in_percent"]
15
+
16
+ @seasons = []
17
+ prices["seasons"].each do |season|
18
+ @seasons << Season.new(season)
19
+ end
20
+ end
21
+
22
+ def self.api_call(slug)
23
+ base_url + "/places/#{slug}/prices.json?client_id=#{Nineflats::Base.client_app_key}"
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,15 @@
1
+ module Nineflats
2
+ class Season
3
+ attr_accessor :from, :to, :price, :weekend_night_price
4
+
5
+ def initialize(json)
6
+ season = json.first[1]
7
+
8
+ @from = season["from"]
9
+ @to = season["to"]
10
+ @price = season["price"]
11
+ @weekend_night_price = season["weekend_night_price"]
12
+ end
13
+
14
+ end
15
+ end
@@ -0,0 +1,26 @@
1
+ module Nineflats
2
+ class User < Base
3
+ attr_accessor :name, :slug, :user_photo_url, :self_url, :favorites_url
4
+
5
+ def initialize(json)
6
+ user = json.first[1]
7
+
8
+ @name = user["name"]
9
+ @slug = user["slug"]
10
+ @user_photo_url = user["user_photo_url"]
11
+
12
+ if user["links"]
13
+ @self_url = user["links"].first.select{|link| link["rel"] == "self"}["href"]
14
+ @favorites_url = user["links"].first.select{|link| link["rel"] == "favorites"}["href"]
15
+ end
16
+ end
17
+
18
+ def self.fetch(slug, lang)
19
+ User.new(Helpers.get_data(User.api_call(slug, lang)))
20
+ end
21
+
22
+ def self.api_call(slug, lang)
23
+ base_url + "/users/#{slug}.json?client_id=#{Nineflats::Base.client_app_key}&language=#{lang}"
24
+ end
25
+ end
26
+ end
@@ -1,5 +1,5 @@
1
1
  module Nineflats
2
2
  module Api
3
- VERSION = "0.0.2"
3
+ VERSION = "0.0.3"
4
4
  end
5
5
  end
@@ -0,0 +1,47 @@
1
+ def place_fixture
2
+ '{
3
+ "place": {
4
+ "name": "Great river view, Colorful and Cozy",
5
+ "city": "Lisboa",
6
+ "slug": "apt-no-centro-histrico-de-lisboa",
7
+ "zipcode": "1100-188",
8
+ "number_of_beds": 4,
9
+ "number_of_bedrooms": 1,
10
+ "currency": "EUR",
11
+ "lat": 38.7173993,
12
+ "lng": -9.1204621,
13
+ "district": "",
14
+ "number_of_bathrooms": 1,
15
+ "minimum_nights": 2,
16
+ "maximum_nights": 365,
17
+ "bed_type": "Real bed",
18
+ "size": 70.0,
19
+ "house_rules": "",
20
+ "pets_around": false,
21
+ "bathroom_type": "Private",
22
+ "cleaning_fee": null,
23
+ "charge_per_extra_person_limit": 4,
24
+ "favorites_count": 2,
25
+ "amenities_list": ["Internet", "Wifi (Wireless LAN)", "TV", "Air Conditioning", "Kitchen", "Washer", "Smoking allowed", "Pets allowed", "Balcony", "Dishwasher", "Fridge/freezer", "Shower/bathtub", "Baby crib", "Panoramic view"],
26
+ "featured_photo_url": "/data/place_photos/photos/62387-1311446301/medium.jpg",
27
+ "price": 90.0,
28
+ "charge_per_extra_person": 20.0,
29
+ "content_language": "en",
30
+ "country": "Portugal",
31
+ "category": "Apartment",
32
+ "place_type": "Entire place",
33
+ "links": [{
34
+ "rel": "self",
35
+ "href": "http://www.9flats.com/api/places/apt-no-centro-histrico-de-lisboa?client_id=WfKWrPywnEbMhlifGlrsLu2ULfvTwxrKQji5eg0S"
36
+ },
37
+ {
38
+ "rel": "full",
39
+ "href": "http://www.9flats.com/places/apt-no-centro-histrico-de-lisboa"
40
+ }],
41
+ "host": {
42
+ "name": "Paulo M.",
43
+ "slug": "paulo-m"
44
+ }
45
+ }
46
+ }'
47
+ end
@@ -0,0 +1,27 @@
1
+ def place_prices_fixture
2
+ '{
3
+ "place_prices": {
4
+ "currency": "EUR",
5
+ "default_price": 90.0,
6
+ "weekend_night_price": 90.0,
7
+ "weekly_discount_in_percent": 5.55,
8
+ "monthly_discount_in_percent": 11.11,
9
+ "seasons": [{
10
+ "season": {
11
+ "from": "2011-09-05",
12
+ "to": "2011-09-30",
13
+ "price": 100.0,
14
+ "weekend_night_price": 110.0
15
+ }
16
+ },
17
+ {
18
+ "season": {
19
+ "from": "2011-10-01",
20
+ "to": "2011-10-31",
21
+ "price": 75.0,
22
+ "weekend_night_price": 75.0
23
+ }
24
+ }]
25
+ }
26
+ }'
27
+ end
@@ -0,0 +1,17 @@
1
+ def user_fixture
2
+ '{
3
+ "user": {
4
+ "name": "Jana K.",
5
+ "slug": "jana-k-1",
6
+ "user_photo_url": "http://img2.9flats.com/users/photos/11405-1311181665/medium.jpg",
7
+ "links": [{
8
+ "rel": "self",
9
+ "href": "http://www.9flats.com/api/users/jana-k-1"
10
+ },
11
+ {
12
+ "rel": "favorites",
13
+ "href": "http://www.9flats.com/api/users/jana-k-1/favorites"
14
+ }]
15
+ }
16
+ }'
17
+ end
@@ -0,0 +1,94 @@
1
+ require 'spec_helper'
2
+
3
+ describe Nineflats::Place do
4
+ before(:each) do
5
+ Nineflats::Base.stub!(:client_app_key).and_return("WfKWrPywnEbMhlifGlrsLu2ULfvTwxrKQji5eg0S")
6
+ FakeWeb.register_uri(:get,
7
+ "http://api.9flats.com/api/places/apt-no-centro-histrico-de-lisboa.json?client_id=#{Nineflats::Base.client_app_key}&language=en",
8
+ :body => place_fixture
9
+ )
10
+ FakeWeb.register_uri(:get,
11
+ "http://api.9flats.com/api/places/apt-no-centro-histrico-de-lisboa/prices.json?client_id=#{Nineflats::Base.client_app_key}",
12
+ :body => place_prices_fixture
13
+ )
14
+ end
15
+
16
+ describe "fetch" do
17
+ before(:each) do
18
+ @place = Nineflats::Place.fetch('apt-no-centro-histrico-de-lisboa', 'en')
19
+ end
20
+
21
+ it "should return a place" do
22
+ @place.class.should == Nineflats::Place
23
+ end
24
+
25
+ it "should set all the attributes" do
26
+ @place.name.should == "Great river view, Colorful and Cozy"
27
+ @place.city.should == "Lisboa"
28
+ @place.slug.should == "apt-no-centro-histrico-de-lisboa"
29
+ @place.zipcode.should == "1100-188"
30
+ @place.number_of_beds.should == 4
31
+ @place.number_of_bedrooms.should == 1
32
+ @place.currency.should == "EUR"
33
+ @place.lat.should == 38.7173993
34
+ @place.lng.should == -9.1204621
35
+ @place.district.should == ""
36
+ @place.number_of_bathrooms.should == 1
37
+ @place.charge_per_extra_person.should == 20.0
38
+ @place.minimum_nights.should == 2
39
+ @place.maximum_nights.should == 365
40
+ @place.bed_type.should == "Real bed"
41
+ @place.content_language == "en"
42
+ @place.size.should == 70.0
43
+ @place.house_rules.should == ""
44
+ @place.pets_around.should == false
45
+ @place.bathroom_type.should == "Private"
46
+ @place.cleaning_fee.should == nil
47
+ @place.charge_per_extra_person_limit.should == 4
48
+ @place.favorites_count.should == 2
49
+ @place.amenities_list.should == ["Internet", "Wifi (Wireless LAN)", "TV", "Air Conditioning", "Kitchen", "Washer", "Smoking allowed", "Pets allowed", "Balcony", "Dishwasher", "Fridge/freezer", "Shower/bathtub", "Baby crib", "Panoramic view"]
50
+ @place.featured_photo_url.should == "/data/place_photos/photos/62387-1311446301/medium.jpg"
51
+ @place.price.should == 90.0
52
+ @place.country.should == "Portugal"
53
+ @place.category.should == "Apartment"
54
+ @place.place_type.should == "Entire place"
55
+ end
56
+
57
+ it "should set the links" do
58
+ @place.self_url = "http://www.9flats.com/api/places/apt-no-centro-histrico-de-lisboa?client_id=WfKWrPywnEbMhlifGlrsLu2ULfvTwxrKQji5eg0S"
59
+ @place.full_url = "http://www.9flats.com/places/apt-no-centro-histrico-de-lisboa"
60
+ end
61
+
62
+ it "should set the host" do
63
+ @place.host.class.should == Nineflats::User
64
+ @place.host.name.should == "Paulo M."
65
+ @place.host.slug.should == "paulo-m"
66
+ end
67
+ end
68
+
69
+ describe "prices" do
70
+ before(:each) do
71
+ @place = Nineflats::Place.fetch('apt-no-centro-histrico-de-lisboa', 'en')
72
+ end
73
+
74
+ it "should add the basic prices to the place" do
75
+ @place.prices.currency.should == "EUR"
76
+ @place.prices.default_price.should == 90.0
77
+ @place.prices.weekend_night_price.should == 90.0
78
+ @place.prices.weekly_discount_in_percent.should == 5.55
79
+ @place.prices.monthly_discount_in_percent.should == 11.11
80
+ end
81
+
82
+ it "should add the seasons" do
83
+ @place.prices.seasons.length.should == 2
84
+ @place.prices.seasons[0].from.should == "2011-09-05"
85
+ @place.prices.seasons[0].to.should == "2011-09-30"
86
+ @place.prices.seasons[0].price.should == 100.0
87
+ @place.prices.seasons[0].weekend_night_price.should == 110.0
88
+ @place.prices.seasons[1].from.should == "2011-10-01"
89
+ @place.prices.seasons[1].to.should == "2011-10-31"
90
+ @place.prices.seasons[1].price.should == 75.0
91
+ @place.prices.seasons[1].weekend_night_price.should == 75.0
92
+ end
93
+ end
94
+ end
@@ -0,0 +1,10 @@
1
+ require 'rubygems'
2
+ require 'bundler/setup'
3
+ require 'fakeweb'
4
+ require 'nineflats-api'
5
+
6
+ Dir["spec/fixtures/*.rb"].each {|f| require File.expand_path(f) }
7
+
8
+ RSpec.configure do |config|
9
+ #
10
+ end
@@ -0,0 +1,32 @@
1
+ require 'spec_helper'
2
+
3
+ describe Nineflats::User do
4
+ before(:each) do
5
+ Nineflats::Base.stub!(:client_app_key).and_return("WfKWrPywnEbMhlifGlrsLu2ULfvTwxrKQji5eg0S")
6
+ FakeWeb.register_uri(:get,
7
+ "http://api.9flats.com/api/users/jana-k-1.json?client_id=#{Nineflats::Base.client_app_key}&language=en",
8
+ :body => user_fixture
9
+ )
10
+ end
11
+
12
+ describe "fetch" do
13
+ before(:each) do
14
+ @user = Nineflats::User.fetch('jana-k-1', 'en')
15
+ end
16
+
17
+ it "should return a user" do
18
+ @user.class.should == Nineflats::User
19
+ end
20
+
21
+ it "should set all the attributes" do
22
+ @user.name.should == "Jana K."
23
+ @user.slug.should == "jana-k-1"
24
+ @user.user_photo_url.should == "http://img2.9flats.com/users/photos/11405-1311181665/medium.jpg"
25
+ end
26
+
27
+ it "should set the links" do
28
+ @user.self_url = "http://www.9flats.com/places/jana-k-1"
29
+ @user.favorites_url = "http://www.9flats.com/places/jana-k-1/favorites"
30
+ end
31
+ end
32
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nineflats-api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-10-10 00:00:00.000000000Z
12
+ date: 2011-10-12 00:00:00.000000000Z
13
13
  dependencies: []
14
14
  description: Let's you use the 9flats API'
15
15
  email:
@@ -19,11 +19,25 @@ extensions: []
19
19
  extra_rdoc_files: []
20
20
  files:
21
21
  - .gitignore
22
+ - .rspec
23
+ - .rvmrc
22
24
  - Gemfile
23
25
  - Rakefile
24
26
  - lib/nineflats-api.rb
27
+ - lib/nineflats-api/base.rb
28
+ - lib/nineflats-api/helpers.rb
29
+ - lib/nineflats-api/place.rb
30
+ - lib/nineflats-api/prices.rb
31
+ - lib/nineflats-api/season.rb
32
+ - lib/nineflats-api/user.rb
25
33
  - lib/nineflats-api/version.rb
26
34
  - nineflats-api.gemspec
35
+ - spec/fixtures/place.rb
36
+ - spec/fixtures/place_prices.rb
37
+ - spec/fixtures/user.rb
38
+ - spec/place_spec.rb
39
+ - spec/spec_helper.rb
40
+ - spec/user_spec.rb
27
41
  homepage: http://9flats.github.com/api_docs/index.html
28
42
  licenses: []
29
43
  post_install_message:
@@ -48,4 +62,10 @@ rubygems_version: 1.8.10
48
62
  signing_key:
49
63
  specification_version: 3
50
64
  summary: 9flats API
51
- test_files: []
65
+ test_files:
66
+ - spec/fixtures/place.rb
67
+ - spec/fixtures/place_prices.rb
68
+ - spec/fixtures/user.rb
69
+ - spec/place_spec.rb
70
+ - spec/spec_helper.rb
71
+ - spec/user_spec.rb