citygrid_api 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.1
1
+ 0.0.2
data/citygrid_api.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{citygrid_api}
8
- s.version = "0.0.1"
8
+ s.version = "0.0.2"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Elpizo Choi"]
12
- s.date = %q{2011-06-24}
12
+ s.date = %q{2011-07-14}
13
13
  s.description = %q{Ruby wrapper for CityGrid APIs}
14
14
  s.email = %q{fu7iin@gmail.com}
15
15
  s.extra_rdoc_files = [
@@ -26,6 +26,12 @@ Gem::Specification.new do |s|
26
26
  "VERSION",
27
27
  "citygrid_api.gemspec",
28
28
  "lib/citygrid.rb",
29
+ "lib/citygrid/abstraction.rb",
30
+ "lib/citygrid/abstraction/collection.rb",
31
+ "lib/citygrid/abstraction/item.rb",
32
+ "lib/citygrid/abstraction/requestable.rb",
33
+ "lib/citygrid/abstraction/super_array.rb",
34
+ "lib/citygrid/abstraction/super_hash.rb",
29
35
  "lib/citygrid/api.rb",
30
36
  "lib/citygrid/api/base.rb",
31
37
  "lib/citygrid/api/content.rb",
@@ -35,15 +41,11 @@ Gem::Specification.new do |s|
35
41
  "lib/citygrid/api/content/places/search.rb",
36
42
  "lib/citygrid/api/content/reviews.rb",
37
43
  "lib/citygrid/api/response.rb",
44
+ "lib/citygrid/details.rb",
38
45
  "lib/citygrid/listing.rb",
39
- "lib/citygrid/listing/details.rb",
40
- "lib/citygrid/listing/extend.rb",
41
- "lib/citygrid/listing/extend/attribute.rb",
42
- "lib/citygrid/listing/extend/attributes.rb",
43
- "lib/citygrid/listing/extend/requestable.rb",
44
- "lib/citygrid/listing/offers.rb",
45
- "lib/citygrid/listing/reviews.rb",
46
- "lib/citygrid/super_hash.rb",
46
+ "lib/citygrid/offers.rb",
47
+ "lib/citygrid/reviews.rb",
48
+ "lib/citygrid/search.rb",
47
49
  "lib/citygrid_api.rb",
48
50
  "test/helper.rb",
49
51
  "test/publisher_helper.rb.sample",
@@ -0,0 +1,15 @@
1
+ class CityGrid
2
+ module Abstraction
3
+ class Collection < SuperArray
4
+ include Requestable
5
+
6
+ def self.new opts = {}
7
+ self[].request_and_update opts
8
+ end
9
+
10
+ def update array
11
+ replace array
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,11 @@
1
+ class CityGrid
2
+ module Abstraction
3
+ class Item < SuperHash
4
+ include Requestable
5
+
6
+ def self.new opts = {}
7
+ self[{}].request_and_update opts
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,38 @@
1
+ class CityGrid
2
+ module Abstraction
3
+ module Requestable
4
+ def self.included base
5
+ base.instance_eval do
6
+ attr_reader :raw
7
+ end
8
+ end
9
+
10
+ def api
11
+ # set corresponding API here
12
+ end
13
+
14
+ def request opts = {}
15
+ api.request opts
16
+ end
17
+
18
+ # Run request, preprocess, and update.
19
+ # Write an update method if it doesn't already exist
20
+ #
21
+ # def update
22
+ #
23
+ # end
24
+ def request_and_update opts = {}
25
+ @raw = request opts
26
+ update preprocess(@raw)
27
+ end
28
+
29
+ private
30
+
31
+ # Preprocess request.
32
+ # Overwrite this method to preprocess request before updating.
33
+ def preprocess response
34
+ response
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,22 @@
1
+ # Extend Array to provide following features:
2
+ # - creates SuperHash for hashes
3
+ # - creates SuperArray for arrays
4
+ # ------------------------------- #
5
+ class CityGrid
6
+ module Abstraction
7
+ class SuperArray < Array
8
+ def self.new objs
9
+ array = self.[] objects
10
+ array.map! do |obj|
11
+ if obj.class == Hash
12
+ SuperHash.new obj
13
+ elsif obj.class == Array
14
+ new obj
15
+ else
16
+ obj
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,26 @@
1
+ # Extend Hash to provide following features:
2
+ # - create by SuperHash.new(<hash>)
3
+ # - can access values by method, instead of []
4
+ # -------------------------------------------- #
5
+ class CityGrid
6
+ module Abstraction
7
+ class SuperHash < Hash
8
+ def self.new attrs
9
+ self[attrs]
10
+ end
11
+
12
+ def method_missing sym, *args, &block
13
+ val = self[sym] || self[sym.to_s]
14
+
15
+ # create new SuperHash if value is hash
16
+ if val.is_a? Hash
17
+ SuperHash.new val
18
+ elsif keys.include?(sym) || keys.include?(sym.to_s)
19
+ val
20
+ else
21
+ super
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,3 @@
1
+ %w{super_hash super_array requestable item collection}.each do |abs|
2
+ require "citygrid/abstraction/#{abs}"
3
+ end
@@ -24,9 +24,14 @@ class CityGrid
24
24
  end
25
25
 
26
26
  def request options = {}
27
- query = options.merge :publisher => publisher, :format => "json"
27
+ query = options.merge :publisher => publisher, :format => "json"
28
28
  response = get endpoint, :query => query
29
- CityGrid::API::Response.new response
29
+
30
+ if response["errors"].empty?
31
+ CityGrid::API::Response.new response
32
+ else
33
+ raise Error.new response["errors"], response
34
+ end
30
35
  end
31
36
 
32
37
  private
@@ -40,5 +45,16 @@ class CityGrid
40
45
  end
41
46
  end
42
47
  end
48
+
49
+ # Throws error with message from API
50
+ # HTTParty response is available if this class is rescued
51
+ class Error < StandardError
52
+ attr_reader :httparty
53
+
54
+ def initialize errors, response = nil
55
+ @httparty = response
56
+ super errors.first["error"]
57
+ end
58
+ end
43
59
  end
44
60
  end
@@ -9,36 +9,6 @@ class CityGrid
9
9
  def endpoint
10
10
  "/places/v2/search/where"
11
11
  end
12
-
13
- # Overwrite to return Search::Response
14
- def request options
15
- query = options.merge :publisher => publisher, :format => "json"
16
- response = get endpoint, :query => query
17
- Response.new response
18
- end
19
- end
20
-
21
- # Holds array of Listings.
22
- # HTTParty response can be accessed by #response
23
- # ---------------------------------------------- #
24
- class Response < Array
25
- attr_reader :response, :errors
26
-
27
- def self.new httparty_response
28
- resp = httparty_response.parsed_response
29
-
30
- if resp["errors"]
31
- array = self.[]
32
- array.instance_variable_set "@errors", resp["errors"]
33
- else
34
- array = self.[] *resp["results"]["locations"]
35
- array.map! do |result|
36
- CityGrid::Listing.new result
37
- end
38
- end
39
- array.instance_variable_set "@response", httparty_response
40
- array
41
- end
42
12
  end
43
13
  end
44
14
  end
@@ -1,16 +1,14 @@
1
- require "citygrid/super_hash"
2
-
3
1
  class CityGrid
4
2
  module API
5
3
  # Creates SuperHash from parsed_response
6
4
  # Stores response object in @response.
7
5
  # ------------------------------------ #
8
- class Response < CityGrid::SuperHash
9
- attr_reader :response
6
+ class Response < CityGrid::Abstraction::SuperHash
7
+ attr_reader :httparty
10
8
 
11
9
  def self.new httparty_response
12
10
  resp = super httparty_response.parsed_response
13
- resp.instance_variable_set "@response", httparty_response
11
+ resp.instance_variable_set "@httparty", httparty_response
14
12
  resp
15
13
  end
16
14
  end
@@ -0,0 +1,17 @@
1
+ class CityGrid
2
+ class Details < Abstraction::Item
3
+ def api
4
+ CityGrid::API::Content::Places::Detail
5
+ end
6
+
7
+ def request opts = {}
8
+ api.request opts.merge(:client_ip => "192.168.0.1")
9
+ end
10
+
11
+ private
12
+
13
+ def preprocess response
14
+ response.locations.first
15
+ end
16
+ end
17
+ end
@@ -1,11 +1,5 @@
1
- require "citygrid/super_hash"
2
- require "citygrid/listing/extend"
3
- require "citygrid/listing/details"
4
- require "citygrid/listing/offers"
5
- require "citygrid/listing/reviews"
6
-
7
1
  class CityGrid
8
- class Listing < SuperHash
2
+ class Listing < Abstraction::SuperHash
9
3
 
10
4
  def method_missing meth, *args, &block
11
5
  load unless @loaded
@@ -18,6 +12,7 @@ class CityGrid
18
12
  extend LoadedMethods
19
13
  update_details
20
14
  @loaded = true
15
+ self
21
16
  end
22
17
 
23
18
  module LoadedMethods
@@ -44,7 +39,7 @@ class CityGrid
44
39
  end
45
40
 
46
41
  def categories
47
- @categories ||= extract_categories details["categories"]
42
+ @categories ||= extract_categories self["categories"]
48
43
  end
49
44
 
50
45
  def special_features
@@ -56,31 +51,33 @@ class CityGrid
56
51
  end
57
52
 
58
53
  def attributes
59
- @attributes ||= extract_attributes details["attributes"]
54
+ @attributes ||= extract_attributes self["attributes"]
60
55
  end
61
56
 
62
57
  # Details
63
58
  # ---------------- #
64
59
  def update_details
65
- update Details.new listing_id
60
+ update Details.new :listing_id => listing_id
66
61
  end
67
62
 
68
63
  # Offers
69
64
  # -------------- #
70
65
  def offers opts = {}
71
- @offers ||= Offers.new listing_id
72
- @offers.update_options opts
66
+ Offers.new listing_options(opts)
73
67
  end
74
68
 
75
69
  # Reviews
76
70
  # -------------- #
77
71
  def reviews opts = {}
78
- @reviews ||= Reviews.new listing_id
79
- @reviews.update_options opts
72
+ Reviews.new listing_options(opts)
80
73
  end
81
74
 
82
75
  private
83
76
 
77
+ def listing_options opts
78
+ {:listing_id => listing_id}.merge(opts)
79
+ end
80
+
84
81
  # Group categories by their group names
85
82
  def extract_categories cats
86
83
  cats.inject Hash.new do |hash, tag|
@@ -0,0 +1,13 @@
1
+ class CityGrid
2
+ class Offers < Abstraction::Collection
3
+ def api
4
+ CityGrid::API::Content::Offers
5
+ end
6
+
7
+ private
8
+
9
+ def preprocess response
10
+ response.results.offers
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,17 @@
1
+ class CityGrid
2
+ class Reviews < Abstraction::Collection
3
+ def api
4
+ CityGrid::API::Content::Reviews
5
+ end
6
+
7
+ def total_hits
8
+ raw["results"]["total_hits"]
9
+ end
10
+
11
+ private
12
+
13
+ def preprocess response
14
+ response.results.reviews
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,15 @@
1
+ class CityGrid
2
+ class Search < Abstraction::Collection
3
+ def api
4
+ CityGrid::API::Content::Places::Search
5
+ end
6
+
7
+ private
8
+
9
+ def preprocess response
10
+ response.results.locations.map do |attrs|
11
+ Listing.new attrs
12
+ end
13
+ end
14
+ end
15
+ end
data/lib/citygrid.rb CHANGED
@@ -1,6 +1,12 @@
1
- require "citygrid/listing"
1
+ require "citygrid/abstraction"
2
2
  require "citygrid/api"
3
3
 
4
+ require "citygrid/search"
5
+ require "citygrid/reviews"
6
+ require "citygrid/offers"
7
+ require "citygrid/details"
8
+ require "citygrid/listing"
9
+
4
10
  class CityGrid
5
11
  class << self
6
12
  def publisher= code
@@ -12,20 +18,20 @@ class CityGrid
12
18
  @@publisher
13
19
  end
14
20
 
15
- def places
16
- API::Content::Places
21
+ def search opts = {}
22
+ Search.new opts
17
23
  end
18
24
 
19
- def offers opts
20
- API::Content::Offers.request opts
25
+ def find listing_id
26
+ Listing.new("id" => listing_id).send(:load)
21
27
  end
22
28
 
23
- def reviews opts
24
- API::Content::Reviews.request opts
29
+ def offers opts
30
+ Offers.new opts
25
31
  end
26
32
 
27
- def search opts = {}
28
- places.search opts
33
+ def reviews opts
34
+ Reviews.new opts
29
35
  end
30
36
  end
31
37
 
@@ -5,7 +5,7 @@ context "A SuperHash" do
5
5
  # ------------------------ #
6
6
  context "created from #new" do
7
7
  context "and given a hash" do
8
- setup { CityGrid::SuperHash.new(:test => "wassup") }
8
+ setup { CityGrid::Abstraction::SuperHash.new(:test => "wassup") }
9
9
  should("return a hash") { topic.is_a? Hash }
10
10
  should("its value can be accessible through its key") { topic[:test] == "wassup"}
11
11
  end
@@ -15,19 +15,19 @@ context "A SuperHash" do
15
15
  # ------------------------ #
16
16
  context "that's single level" do
17
17
  context "with symbol as key" do
18
- setup { CityGrid::SuperHash.new(:test => "wassup") }
18
+ setup { CityGrid::Abstraction::SuperHash.new(:test => "wassup") }
19
19
  should("can access key as method") { topic.test }
20
20
  end
21
21
 
22
22
  context "with string as key" do
23
- setup { CityGrid::SuperHash.new("test" => "wasssup") }
24
- should("can access key as method"){ topic.test }
23
+ setup { CityGrid::Abstraction::SuperHash.new("test" => "wasssup") }
24
+ should("can access key as method"){ topic.test }
25
25
  end
26
26
  end
27
27
 
28
28
  context "that has a subhash" do
29
- setup { CityGrid::SuperHash.new(:test => {:wassup => "hows it going?"}) }
30
- should("return another SuperHash") { topic.test.is_a? CityGrid::SuperHash }
29
+ setup { CityGrid::Abstraction::SuperHash.new(:test => {:wassup => "hows it going?"}) }
30
+ should("return another SuperHash") { topic.test.is_a? CityGrid::Abstraction::SuperHash }
31
31
  should("return correct hash") { topic.test.wassup == "hows it going?" }
32
32
  end
33
33
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: citygrid_api
3
3
  version: !ruby/object:Gem::Version
4
- hash: 29
4
+ hash: 27
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 1
10
- version: 0.0.1
9
+ - 2
10
+ version: 0.0.2
11
11
  platform: ruby
12
12
  authors:
13
13
  - Elpizo Choi
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-06-24 00:00:00 Z
18
+ date: 2011-07-14 00:00:00 Z
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
21
21
  type: :runtime
@@ -130,6 +130,12 @@ files:
130
130
  - VERSION
131
131
  - citygrid_api.gemspec
132
132
  - lib/citygrid.rb
133
+ - lib/citygrid/abstraction.rb
134
+ - lib/citygrid/abstraction/collection.rb
135
+ - lib/citygrid/abstraction/item.rb
136
+ - lib/citygrid/abstraction/requestable.rb
137
+ - lib/citygrid/abstraction/super_array.rb
138
+ - lib/citygrid/abstraction/super_hash.rb
133
139
  - lib/citygrid/api.rb
134
140
  - lib/citygrid/api/base.rb
135
141
  - lib/citygrid/api/content.rb
@@ -139,15 +145,11 @@ files:
139
145
  - lib/citygrid/api/content/places/search.rb
140
146
  - lib/citygrid/api/content/reviews.rb
141
147
  - lib/citygrid/api/response.rb
148
+ - lib/citygrid/details.rb
142
149
  - lib/citygrid/listing.rb
143
- - lib/citygrid/listing/details.rb
144
- - lib/citygrid/listing/extend.rb
145
- - lib/citygrid/listing/extend/attribute.rb
146
- - lib/citygrid/listing/extend/attributes.rb
147
- - lib/citygrid/listing/extend/requestable.rb
148
- - lib/citygrid/listing/offers.rb
149
- - lib/citygrid/listing/reviews.rb
150
- - lib/citygrid/super_hash.rb
150
+ - lib/citygrid/offers.rb
151
+ - lib/citygrid/reviews.rb
152
+ - lib/citygrid/search.rb
151
153
  - lib/citygrid_api.rb
152
154
  - test/helper.rb
153
155
  - test/publisher_helper.rb.sample
@@ -1,21 +0,0 @@
1
- require "citygrid/listing/extend"
2
-
3
- class CityGrid
4
- class Listing < SuperHash
5
- class Details < Extend::Attribute
6
- def api
7
- CityGrid::API::Content::Places::Detail
8
- end
9
-
10
- def request
11
- api.request listing_options.merge(:client_ip => "192.168.0.1")
12
- end
13
-
14
- private
15
-
16
- def preprocess req
17
- req.locations.first
18
- end
19
- end
20
- end
21
- end
@@ -1,18 +0,0 @@
1
- require "citygrid/listing/extend/requestable"
2
-
3
- class CityGrid
4
- class Listing < SuperHash
5
- module Extend
6
- class Attribute < CityGrid::SuperHash
7
- include Requestable
8
-
9
- def self.new listing_id, opts = {}
10
- hash = self[{}]
11
- hash.instance_variable_set "@listing_id", listing_id
12
- hash.instance_variable_set "@options", opts
13
- hash.request_and_update
14
- end
15
- end
16
- end
17
- end
18
- end
@@ -1,22 +0,0 @@
1
- require "citygrid/listing/extend/requestable"
2
-
3
- class CityGrid
4
- class Listing < SuperHash
5
- module Extend
6
- class Attributes < Array
7
- include Requestable
8
-
9
- def self.new listing_id, opts = {}
10
- hash = self[]
11
- hash.instance_variable_set "@listing_id", listing_id
12
- hash.instance_variable_set "@options", opts
13
- hash.request_and_update
14
- end
15
-
16
- def update array
17
- replace array
18
- end
19
- end
20
- end
21
- end
22
- end
@@ -1,51 +0,0 @@
1
- class CityGrid
2
- class Listing < SuperHash
3
- module Extend
4
- module Requestable
5
- def self.included base
6
- base.instance_eval do
7
- attr_accessor :options
8
- attr_reader :listing_id, :raw
9
- end
10
- end
11
-
12
- def api
13
- # set corresponding API here
14
- end
15
-
16
- def request
17
- api.request listing_options
18
- end
19
-
20
- # Run request, preprocess, and update.
21
- # Write an update method if it doesn't already exist
22
- #
23
- # def update
24
- #
25
- # end
26
- def request_and_update
27
- @raw = request
28
- update preprocess(@raw)
29
- end
30
-
31
- def listing_options
32
- options.merge :listing_id => listing_id
33
- end
34
-
35
- def update_options opts
36
- return self if opts == @options
37
- @options = opts
38
- request_and_update
39
- end
40
-
41
- private
42
-
43
- # Preprocess request.
44
- # Overwrite this method to preprocess request before updating.
45
- def preprocess req
46
- req
47
- end
48
- end
49
- end
50
- end
51
- end
@@ -1,3 +0,0 @@
1
- require "citygrid/listing/extend/requestable"
2
- require "citygrid/listing/extend/attribute"
3
- require "citygrid/listing/extend/attributes"
@@ -1,17 +0,0 @@
1
- require "citygrid/listing/extend"
2
-
3
- class CityGrid
4
- class Listing < SuperHash
5
- class Offers < Extend::Attributes
6
- def api
7
- CityGrid::API::Content::Offers
8
- end
9
-
10
- private
11
-
12
- def preprocess req
13
- req.results.offers
14
- end
15
- end
16
- end
17
- end
@@ -1,21 +0,0 @@
1
- require "citygrid/listing/extend"
2
-
3
- class CityGrid
4
- class Listing < SuperHash
5
- class Reviews < Extend::Attributes
6
- def api
7
- CityGrid::API::Content::Reviews
8
- end
9
-
10
- def total_hits
11
- raw["results"]["total_hits"]
12
- end
13
-
14
- private
15
-
16
- def preprocess req
17
- req.results.reviews
18
- end
19
- end
20
- end
21
- end
@@ -1,23 +0,0 @@
1
- class CityGrid
2
-
3
- # Subset of Hash that provides following features:
4
- # - create by SuperHash.new(<hash>)
5
- # - can access values by method, instead of []
6
- # -------------------------------------------- #
7
- class SuperHash < Hash
8
- def self.new attrs
9
- self[attrs]
10
- end
11
-
12
- def method_missing sym, *args, &block
13
- val = self[sym] || self[sym.to_s]
14
-
15
- # create new SuperHash if value is hash
16
- if val.is_a? Hash
17
- SuperHash.new val
18
- else
19
- val || super
20
- end
21
- end
22
- end
23
- end