iwannagothere 0.0.3 → 1.0.0

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.
data/.rvmrc ADDED
@@ -0,0 +1 @@
1
+ rvm --create use 1.9.2-p290@iwannagothere
data/Gemfile ADDED
@@ -0,0 +1,11 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Add dependencies to develop your gem here.
4
+ # Include everything needed to run rake, tests, features, etc.
5
+ gem "json"
6
+
7
+ group :development do
8
+ gem "bundler", "~> 1.0.0"
9
+ gem "jeweler", "~> 1.6.4"
10
+ gem "rest-client"
11
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,22 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ git (1.2.5)
5
+ jeweler (1.6.4)
6
+ bundler (~> 1.0)
7
+ git (>= 1.2.5)
8
+ rake
9
+ json (1.6.5)
10
+ mime-types (1.17.2)
11
+ rake (0.9.2.2)
12
+ rest-client (1.6.7)
13
+ mime-types (>= 1.16)
14
+
15
+ PLATFORMS
16
+ ruby
17
+
18
+ DEPENDENCIES
19
+ bundler (~> 1.0.0)
20
+ jeweler (~> 1.6.4)
21
+ json
22
+ rest-client
data/README.md CHANGED
@@ -1,10 +1,10 @@
1
- # iwannagothere #
2
- Ruby wrapper for accessing places and guides information from the [iwannagothere](http://iwannagothere.com/ "iwannagothere") API.
3
-
4
- ## ToDo ##
5
- * Problems whit single photo
6
- * Implement some tests
7
- * Make more DRY
8
-
9
- ## Copyright ##
1
+ # iwannagothere #
2
+ Ruby wrapper for accessing places and guides information from the [iwannagothere](http://iwannagothere.com/ "iwannagothere") API.
3
+
4
+ ## ToDo ##
5
+ * Item search problems with API
6
+ * Item photos problems with API
7
+ * Place photos problems with API
8
+
9
+ ## Copyright ##
10
10
  Copyright (c) 2009 Jordi Villar, released under MIT license
data/Rakefile ADDED
@@ -0,0 +1,22 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gemspec|
7
+ gemspec.name = "iwannagothere"
8
+ gemspec.version = "1.0.0"
9
+ gemspec.summary = "Ruby wrapper for iwannagothere API "
10
+ gemspec.description = "Ruby wrapper for iwannagothere API "
11
+ gemspec.email = "jrdi.villar@gmail.com"
12
+ gemspec.homepage = "https://github.com/jrdi/iwannagothere"
13
+ gemspec.authors = ["Jordi Villar"]
14
+ gemspec.add_dependency("rest-client")
15
+ gemspec.add_dependency("json")
16
+ end
17
+ Jeweler::GemcutterTasks.new
18
+ rescue LoadError
19
+ puts "Jeweler not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
20
+ end
21
+
22
+ Dir["#{File.dirname(__FILE__)}/tasks/*.rake"].sort.each { |ext| load ext }
@@ -1,14 +1,9 @@
1
1
  require '../lib/iwannagothere'
2
2
 
3
- puts c = IWannaGoThere::Client.new('2a1d90dd2aafcae3a1ba3dd0a6cde6a8')
4
- puts p = c.place_information('Barcelona')
5
- puts p.items
6
- puts p.photos
7
- puts p.guides
3
+ # Configure Client
4
+ IWannaGoThere::Client.new("2a1d90dd2aafcae3a1ba3dd0a6cde6a8")
8
5
 
9
- puts c.user_information(1)
10
- puts c.guide_information(120)
11
- puts i = c.item_information(120)
12
- puts i.photos
13
- puts r = c.item_information(6426)
14
- puts r.photos
6
+ puts IWannaGoThere::Photo.search(3128760)
7
+ puts IWannaGoThere::Place.search(3128760)
8
+
9
+ puts IWannaGoThere::Place.search(312876)
@@ -0,0 +1,83 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{iwannagothere}
8
+ s.version = "1.0.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = [%q{Jordi Villar}]
12
+ s.date = %q{2012-02-05}
13
+ s.description = %q{Ruby wrapper for iwannagothere API }
14
+ s.email = %q{jrdi.villar@gmail.com}
15
+ s.extra_rdoc_files = [
16
+ "README.md"
17
+ ]
18
+ s.files = [
19
+ ".rvmrc",
20
+ "Gemfile",
21
+ "Gemfile.lock",
22
+ "README.md",
23
+ "Rakefile",
24
+ "examples/all_methods.rb",
25
+ "iwannagothere.gemspec",
26
+ "lib/core_ext/hash.rb",
27
+ "lib/core_ext/string.rb",
28
+ "lib/iwannagothere.rb",
29
+ "lib/iwannagothere/client.rb",
30
+ "lib/iwannagothere/configuration.rb",
31
+ "lib/iwannagothere/guide.rb",
32
+ "lib/iwannagothere/item.rb",
33
+ "lib/iwannagothere/photo.rb",
34
+ "lib/iwannagothere/place.rb",
35
+ "lib/iwannagothere/urls.rb",
36
+ "test/fixtures/3128760.json",
37
+ "test/fixtures/3128760_guides.json",
38
+ "test/fixtures/3128760_items.json",
39
+ "test/fixtures/3128760_photos.json",
40
+ "test/fixtures/item_1659.json",
41
+ "test/fixtures/route_215.json",
42
+ "test/fixtures/route_215_items.json",
43
+ "test/fixtures/routes_215_items.json",
44
+ "test/guide_test.rb",
45
+ "test/integration/smoke_test.rb",
46
+ "test/item_test.rb",
47
+ "test/photo_test.rb",
48
+ "test/place_test.rb",
49
+ "test/test_helper.rb"
50
+ ]
51
+ s.homepage = %q{https://github.com/jrdi/iwannagothere}
52
+ s.require_paths = [%q{lib}]
53
+ s.rubygems_version = %q{1.8.6}
54
+ s.summary = %q{Ruby wrapper for iwannagothere API}
55
+
56
+ if s.respond_to? :specification_version then
57
+ s.specification_version = 3
58
+
59
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
60
+ s.add_runtime_dependency(%q<json>, [">= 0"])
61
+ s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
62
+ s.add_development_dependency(%q<jeweler>, ["~> 1.6.4"])
63
+ s.add_development_dependency(%q<rest-client>, [">= 0"])
64
+ s.add_runtime_dependency(%q<rest-client>, [">= 0"])
65
+ s.add_runtime_dependency(%q<json>, [">= 0"])
66
+ else
67
+ s.add_dependency(%q<json>, [">= 0"])
68
+ s.add_dependency(%q<bundler>, ["~> 1.0.0"])
69
+ s.add_dependency(%q<jeweler>, ["~> 1.6.4"])
70
+ s.add_dependency(%q<rest-client>, [">= 0"])
71
+ s.add_dependency(%q<rest-client>, [">= 0"])
72
+ s.add_dependency(%q<json>, [">= 0"])
73
+ end
74
+ else
75
+ s.add_dependency(%q<json>, [">= 0"])
76
+ s.add_dependency(%q<bundler>, ["~> 1.0.0"])
77
+ s.add_dependency(%q<jeweler>, ["~> 1.6.4"])
78
+ s.add_dependency(%q<rest-client>, [">= 0"])
79
+ s.add_dependency(%q<rest-client>, [">= 0"])
80
+ s.add_dependency(%q<json>, [">= 0"])
81
+ end
82
+ end
83
+
@@ -0,0 +1,5 @@
1
+ class Hash
2
+ def method_missing(m, *args, &block)
3
+ self.has_key?(m.to_s) ? self[m.to_s] : super
4
+ end
5
+ end
data/lib/iwannagothere.rb CHANGED
@@ -1,14 +1,34 @@
1
- %w{rubygems httparty}.each { |x| require x }
2
- %w{string}.each{ |file| require File.join(File.dirname(__FILE__), 'core_ext', file) }
3
- %w{element urls place photo item_route item user client}.each{ |file| require File.join(File.dirname(__FILE__), 'iwannagothere', file) }
1
+ %w{rubygems rest-client json}.each { |x| require x }
2
+ %w{string hash}.each{ |file| require File.join(File.dirname(__FILE__), 'core_ext', file) }
3
+ %w{client configuration guide item photo place urls}.each{ |file| require File.join(File.dirname(__FILE__), 'iwannagothere', file) }
4
4
 
5
5
  module IWannaGoThere
6
- def self.get(url, options = nil, new_options = nil)
7
- options[:query].merge! new_options unless new_options.blank?
8
- begin
9
- HTTParty.get(url, options)
10
- rescue Exception => e
11
- puts e
6
+
7
+ @@base_url = IWannaGoThere::Urls::BASE_URL
8
+
9
+ @@resource = RestClient::Resource.new @@base_url, :timeout => 20, :open_timeout => 20
10
+
11
+ def self.get(uri, uri_params = nil)
12
+ uri = IWannaGoThere::Urls[uri.to_sym].params(uri_params)
13
+ @@resource[uri].get :params => IWannaGoThere::Configuration.to_json do |response, request, result|
14
+ case response.code
15
+ when 200
16
+ filter_results JSON.parse(response)
17
+ else
18
+ raise "Response status: #{response.code}"
19
+ end
20
+ end
21
+ end
22
+
23
+ protected
24
+ def self.filter_results(response = {})
25
+ keys = response.keys
26
+ if keys.include? "404"
27
+ raise "Response status: 404: #{response["404"]}"
28
+ elsif keys.include? "401"
29
+ raise "Response status: 401: #{response["401"]}"
30
+ else
31
+ response
12
32
  end
13
33
  end
14
34
  end
@@ -1,43 +1,7 @@
1
1
  module IWannaGoThere
2
- class Client
3
- attr_reader :api_key, :urls, :options
4
-
2
+ class Client
5
3
  def initialize(api_key)
6
- @api_key = api_key
7
- @urls = Urls.new
8
- @options = { :query => { :key => @api_key } }
9
- end
10
-
11
- def user_information(id)
12
- r = IWannaGoThere.get(@urls[:user_information].params({ :user_id => id }), @options)
13
- User.new r['user'], @urls, @options
14
- end
15
-
16
- def place_information(q)
17
- r = IWannaGoThere.get(@urls[:place_information].params({ :geoname_id => geoname_id(q) }), @options)
18
- Place.new r['place'], @urls, @options
19
- end
20
-
21
- def item_information(id)
22
- r = IWannaGoThere.get(@urls[:item_information].params({ :item_id => id }), @options)
23
- Item.new r['item'], @urls, @options
24
- end
25
-
26
- def guide_information(id)
27
- r = IWannaGoThere.get(@urls[:guide_information].params({ :route_id => id }), @options)
28
- ItemRoute.new r['itemroute'], @urls, @options
29
- end
30
-
31
- private
32
- def geoname_id(q)
33
- if q.is_a? String
34
- geo_options = { :query => { :name => q, :maxRows => "1" }}
35
- r = IWannaGoThere.get(Urls::GEO_URL, geo_options)
36
- r["geonames"]["geoname"]["geonameId"]
37
- elsif q.is_a? Integer
38
- q
39
- end
40
- end
4
+ IWannaGoThere::Configuration.key = api_key
5
+ end
41
6
  end
42
-
43
7
  end
@@ -0,0 +1,37 @@
1
+ module IWannaGoThere
2
+ class Configuration
3
+
4
+ def self.options_and_defaults
5
+ [
6
+ # API key for IWannaGoThere service
7
+ [:key, nil],
8
+ [:size, 'square'],
9
+ [:limit, 10]
10
+ ]
11
+ end
12
+
13
+ def self.to_json(options = {})
14
+ self.options_and_defaults.inject({}) do |obj, (option, default)|
15
+ obj[option.to_sym] = eval("@@#{option}")
16
+ obj
17
+ end
18
+ end
19
+
20
+ # define getters and setters for all configuration settings
21
+ self.options_and_defaults.each do |option, default|
22
+ class_eval(<<-END, __FILE__, __LINE__ + 1)
23
+
24
+ @@#{option} = default unless defined? @@#{option}
25
+
26
+ def self.#{option}
27
+ @@#{option}
28
+ end
29
+
30
+ def self.#{option}=(obj)
31
+ @@#{option} = obj
32
+ end
33
+
34
+ END
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,23 @@
1
+ module IWannaGoThere
2
+ class Guide < Hash
3
+ def initialize(params)
4
+ merge! params
5
+ end
6
+
7
+ def self.search(route_id)
8
+ result = IWannaGoThere.get(:guide_information, :route_id => route_id)
9
+ result['itemroute'] and new(result['itemroute'])
10
+ end
11
+
12
+ def self.search_by_geoname_id(geoname_id)
13
+ result = IWannaGoThere.get(:place_guides, :geoname_id => geoname_id)
14
+ result['itemroutes'] and result['itemroutes'].collect() do |itemroute|
15
+ new(itemroute)
16
+ end
17
+ end
18
+
19
+ def items
20
+ @items ||= IWannaGoThere::Item.search_by_route_id(id)
21
+ end
22
+ end
23
+ end
@@ -1,17 +1,25 @@
1
1
  module IWannaGoThere
2
- class Item < Element
3
- attr_accessor :address, :created_at, :place_id, :popularity, :coolness, :delta, :title, :published,
4
- :saved_count, :summercontest09, :updated_at, :highlighted_date, :url, :id, :reviewed,
5
- :long_guid, :category_id, :user_id, :nicetitle, :description, :lat, :keywords, :long,
6
- :map_id, :country_id, :itemroute_id, :item_id, :phone_number
7
-
8
- def photos(size = 'square', limit = 10)
9
- r = IWannaGoThere.get(@urls[:item_photos].params({ :item_id => @id || @item_id }), @options, { :limit => limit, :size => size })
10
-
11
- if r['pictures']['picture'].is_a? Array
12
- r['pictures']['picture'].collect{ |p| Photo.new( p ) }
13
- elsif r['pictures']['picture'].is_a? Hash
14
- r['pictures']['picture'].collect{ |p| Photo.new( r['pictures']['picture'] ) }
2
+ class Item < Hash
3
+ def initialize(params)
4
+ merge! params
5
+ end
6
+
7
+ def self.search(item_id)
8
+ result = IWannaGoThere.get(:item_information, :item_id => item_id)
9
+ result['item'] and new(result['item'])
10
+ end
11
+
12
+ def self.search_by_geoname_id(geoname_id)
13
+ result = IWannaGoThere.get(:place_items, :geoname_id => geoname_id)
14
+ result['items'] and result['items'].collect() do |item|
15
+ new(item)
16
+ end
17
+ end
18
+
19
+ def self.search_by_route_id(route_id)
20
+ result = IWannaGoThere.get(:guide_items, :route_id => route_id)
21
+ result['items'] and result['items'].collect() do |item|
22
+ new(item)
15
23
  end
16
24
  end
17
25
  end
@@ -1,9 +1,14 @@
1
1
  module IWannaGoThere
2
- class Photo < Element
3
- attr_accessor :content_type, :url, :height, :width
4
-
5
- def initialize(hash)
6
- super(hash, nil, nil)
7
- end
2
+ class Photo < Hash
3
+ def initialize(params)
4
+ merge! params
5
+ end
6
+
7
+ def self.search(geoname_id)
8
+ result = IWannaGoThere.get(:place_photos, :geoname_id => geoname_id)
9
+ result['pictures'] and result['pictures']['picture'] and result['pictures']['picture'].collect() do |picture|
10
+ new(picture)
11
+ end
12
+ end
8
13
  end
9
14
  end
@@ -1,22 +1,25 @@
1
1
  module IWannaGoThere
2
- class Place < Element
3
- attr_accessor :country_id, :created_at, :delta, :geoname_id, :id, :items_count,
4
- :lat, :long, :map_id, :name, :nicename, :updated_at, :long_guid,
5
- :hierarchy, :alternative_names, :users_count, :meta, :main_place
6
-
7
- def photos(size = 'square', limit = 10)
8
- r = IWannaGoThere.get(@urls[:place_photos].params({ :geoname_id => @geoname_id }), @options, { :limit => limit, :size => size })
9
- r['pictures']['picture'].collect{ |p| Photo.new( p ) }
10
- end
11
-
12
- def guides(limit = 10)
13
- r = IWannaGoThere.get(@urls[:place_guides].params({ :geoname_id => @geoname_id }), @options, { :limit => limit })
14
- r['itemroutes'].collect{ |p| ItemRoute.new( p, @urls, @options ) }
15
- end
16
-
17
- def items(limit = 10)
18
- r = IWannaGoThere.get(@urls[:place_items].params({ :geoname_id => @geoname_id }), @options, { :limit => limit })
19
- r['items'].collect{ |p| Item.new( p, @urls, @options ) }
2
+ class Place < Hash
3
+
4
+ def initialize(params)
5
+ merge! params
6
+ end
7
+
8
+ def self.search(geoname_id)
9
+ result = IWannaGoThere.get(:place_information, :geoname_id => geoname_id)
10
+ result['place'] and new(result['place'])
11
+ end
12
+
13
+ def photos
14
+ @photos ||= IWannaGoThere::Photo.search(geoname_id)
15
+ end
16
+
17
+ def guides
18
+ @guides ||= IWannaGoThere::Guide.search_by_geoname_id(geoname_id)
19
+ end
20
+
21
+ def items
22
+ @items ||= IWannaGoThere::Item.search_by_geoname_id(geoname_id)
20
23
  end
21
24
  end
22
25
  end