citygrid_api 0.0.5.1 → 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (122) hide show
  1. data/.sass-cache/c6f2c767b164f077e5b9fb1f18a7886d7c755211/test.scssc +0 -0
  2. data/Gemfile +3 -1
  3. data/Gemfile.lock +9 -1
  4. data/VERSION +1 -1
  5. data/citygrid_api.gemspec +85 -45
  6. data/citygrid_api.yml.backup +35 -0
  7. data/citygrid_api.yml.sample +35 -23
  8. data/fixtures/vcr_cassettes/AdGroupCriterion_mutate.yml +30 -0
  9. data/fixtures/vcr_cassettes/AdGroupCriterion_search.yml +35 -0
  10. data/fixtures/vcr_cassettes/Adding_a_place.yml +33 -0
  11. data/fixtures/vcr_cassettes/Call_Detail_by_campaign.yml +79 -0
  12. data/fixtures/vcr_cassettes/Geo.yml +32 -0
  13. data/fixtures/vcr_cassettes/Getting_an_offer_by_listing_ID.yml +54 -0
  14. data/fixtures/vcr_cassettes/Import_a_cg_account.yml +54 -0
  15. data/fixtures/vcr_cassettes/Initiating_a_Details.yml +133 -0
  16. data/fixtures/vcr_cassettes/Initiating_new_MultiDetails.yml +133 -0
  17. data/fixtures/vcr_cassettes/Method_of_Payment_mutate.yml +33 -0
  18. data/fixtures/vcr_cassettes/Method_of_Payment_search.yml +59 -0
  19. data/fixtures/vcr_cassettes/Mutating_AdGroup_Ads.yml +30 -0
  20. data/fixtures/vcr_cassettes/Report_summary_daily.yml +60 -0
  21. data/fixtures/vcr_cassettes/Report_summary_user_actions.yml +36 -0
  22. data/fixtures/vcr_cassettes/Search_Ad_Group_by_Campaign_Id.yml +31 -0
  23. data/fixtures/vcr_cassettes/Search_Billing_by_Campaign_Id.yml +30 -0
  24. data/fixtures/vcr_cassettes/Search_account_manager_by_Campaign_Id.yml +30 -0
  25. data/fixtures/vcr_cassettes/Searching_AdGroup_Geo.yml +57 -0
  26. data/fixtures/vcr_cassettes/Searching_budgets.yml +30 -0
  27. data/fixtures/vcr_cassettes/Searching_categories_by_listing_id.yml +35 -0
  28. data/fixtures/vcr_cassettes/Searching_categories_by_query.yml +33 -0
  29. data/fixtures/vcr_cassettes/Upload_an_image_Add_a_preview.yml +31 -0
  30. data/fixtures/vcr_cassettes/create_campaign.yml +30 -0
  31. data/fixtures/vcr_cassettes/log_in_a_session.yml +24 -0
  32. data/fixtures/vcr_cassettes/log_in_a_session_mutate_places.yml +56 -0
  33. data/fixtures/vcr_cassettes/query_account_type_by_id.yml +30 -0
  34. data/fixtures/vcr_cassettes/search_for_an_account_by_id.yml +31 -0
  35. data/fixtures/vcr_cassettes/search_for_an_account_by_name.yml +55 -0
  36. data/fixtures/vcr_cassettes/searching_for_a_CityGrid_listing.yml +168 -0
  37. data/fixtures/vcr_cassettes/searching_for_a_special_CityGrid_listing.yml +67 -0
  38. data/lib/citygrid/abstraction/requestable.rb +1 -0
  39. data/lib/citygrid/api/accounts/account.rb +17 -0
  40. data/lib/citygrid/api/accounts/authentication.rb +3 -0
  41. data/lib/citygrid/api/accounts/method_of_payment.rb +10 -0
  42. data/lib/citygrid/api/accounts/user.rb +18 -0
  43. data/lib/citygrid/api/accounts.rb +8 -0
  44. data/lib/citygrid/api/advertising/account_manager.rb +9 -0
  45. data/lib/citygrid/api/advertising/ad_group.rb +10 -0
  46. data/lib/citygrid/api/advertising/ad_group_ad.rb +10 -0
  47. data/lib/citygrid/api/advertising/ad_group_criterion.rb +10 -0
  48. data/lib/citygrid/api/advertising/ad_group_geo.rb +10 -0
  49. data/lib/citygrid/api/advertising/billing.rb +9 -0
  50. data/lib/citygrid/api/advertising/budget.rb +10 -0
  51. data/lib/citygrid/api/advertising/call_detail.rb +10 -0
  52. data/lib/citygrid/api/advertising/campaign.rb +10 -0
  53. data/lib/citygrid/api/advertising/category.rb +10 -0
  54. data/lib/citygrid/api/advertising/geolocation.rb +10 -0
  55. data/lib/citygrid/api/{ad_center → advertising}/image.rb +2 -2
  56. data/lib/citygrid/api/{ad_center → advertising}/performance.rb +5 -2
  57. data/lib/citygrid/api/advertising/places.rb +10 -0
  58. data/lib/citygrid/api/advertising.rb +10 -0
  59. data/lib/citygrid/api/content/offers.rb +2 -12
  60. data/lib/citygrid/api/content/places.rb +4 -26
  61. data/lib/citygrid/api/content.rb +1 -5
  62. data/lib/citygrid/api/mutatable.rb +15 -0
  63. data/lib/citygrid/api/searchable.rb +15 -0
  64. data/lib/citygrid/api.rb +54 -52
  65. data/lib/citygrid/details.rb +3 -6
  66. data/lib/citygrid/offers.rb +1 -5
  67. data/lib/citygrid/search.rb +3 -6
  68. data/lib/citygrid/session.rb +65 -0
  69. data/lib/citygrid.rb +46 -75
  70. data/lib/request_ext.rb +4 -5
  71. data/lib/string_ext.rb +7 -0
  72. data/test/api/accounts/test_account.rb +111 -0
  73. data/test/api/accounts/test_method_of_payment.rb +50 -0
  74. data/test/api/advertising/test_account_manager.rb +14 -0
  75. data/test/api/advertising/test_ad_group.rb +13 -0
  76. data/test/api/advertising/test_ad_group_ad.rb +24 -0
  77. data/test/api/advertising/test_ad_group_criterion.rb +47 -0
  78. data/test/api/{ad_center → advertising}/test_ad_group_geo.rb +1 -1
  79. data/test/api/{ad_center → advertising}/test_billing.rb +4 -3
  80. data/test/api/{ad_center → advertising}/test_budget.rb +3 -3
  81. data/test/api/{ad_center → advertising}/test_call_detail.rb +5 -11
  82. data/test/api/{ad_center → advertising}/test_category.rb +9 -12
  83. data/test/api/advertising/test_create_campaign_and_ad_group.rb +56 -0
  84. data/test/api/{ad_center → advertising}/test_geolocation.rb +1 -1
  85. data/test/api/{ad_center → advertising}/test_image.rb +1 -3
  86. data/test/api/{ad_center/test_reports.rb → advertising/test_performance.rb} +6 -10
  87. data/test/api/advertising/test_places.rb +109 -0
  88. data/test/{test_details.rb → api/content/test_details.rb} +2 -3
  89. data/test/{test_search.rb → api/content/test_search.rb} +2 -2
  90. data/test/auth_token.rb +39 -0
  91. data/test/helper.rb +31 -31
  92. data/test/session_helper.rb +20 -0
  93. data/test/test_session.rb +53 -0
  94. metadata +115 -67
  95. data/lib/citygrid/api/ad_center/account.rb +0 -26
  96. data/lib/citygrid/api/ad_center/account_manager.rb +0 -8
  97. data/lib/citygrid/api/ad_center/ad_group.rb +0 -8
  98. data/lib/citygrid/api/ad_center/ad_group_ad.rb +0 -8
  99. data/lib/citygrid/api/ad_center/ad_group_criterion.rb +0 -8
  100. data/lib/citygrid/api/ad_center/ad_group_geo.rb +0 -8
  101. data/lib/citygrid/api/ad_center/authentication.rb +0 -20
  102. data/lib/citygrid/api/ad_center/billing.rb +0 -8
  103. data/lib/citygrid/api/ad_center/budget.rb +0 -9
  104. data/lib/citygrid/api/ad_center/call_detail.rb +0 -25
  105. data/lib/citygrid/api/ad_center/campaign.rb +0 -8
  106. data/lib/citygrid/api/ad_center/category.rb +0 -9
  107. data/lib/citygrid/api/ad_center/geolocation.rb +0 -9
  108. data/lib/citygrid/api/ad_center/method_of_payment.rb +0 -10
  109. data/lib/citygrid/api/ad_center/places.rb +0 -8
  110. data/lib/citygrid/api/ad_center/user.rb +0 -8
  111. data/lib/citygrid/api/ad_center.rb +0 -20
  112. data/lib/citygrid/api/content/places/detail.rb +0 -11
  113. data/lib/citygrid/api/content/places/search.rb +0 -11
  114. data/test/api/ad_center/test_account.rb +0 -120
  115. data/test/api/ad_center/test_ad_group.rb +0 -50
  116. data/test/api/ad_center/test_ad_group_ad.rb +0 -27
  117. data/test/api/ad_center/test_ad_group_criterion.rb +0 -37
  118. data/test/api/ad_center/test_authentication.rb +0 -22
  119. data/test/api/ad_center/test_campaign.rb +0 -27
  120. data/test/api/ad_center/test_method_of_payment.rb +0 -51
  121. data/test/api/ad_center/test_places.rb +0 -79
  122. data/test/api/content/test_places.rb +0 -66
@@ -0,0 +1,10 @@
1
+ class CityGrid
2
+ class API
3
+ class Advertising
4
+ class Places < Advertising
5
+ extend CityGrid::API::Mutatable
6
+ extend CityGrid::API::Searchable
7
+ end
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,10 @@
1
+ class CityGrid
2
+ class API
3
+ class Advertising < API
4
+ end
5
+ end
6
+ end
7
+
8
+ CityGrid::API::AdCenter = CityGrid::API::Advertising # backwards compat
9
+
10
+ Dir[File.dirname(__FILE__) + '/advertising/*.rb'].each {|file| require file }
@@ -2,18 +2,8 @@ class CityGrid
2
2
  class API
3
3
  class Content
4
4
  class Offers < Content
5
- @base_endpoint = "http://api.citygridmedia.com/content/offers/v2/search"
6
- endpoint @base_endpoint
7
-
8
- def self.where options = {}
9
- endpoint "#{@base_endpoint}/where"
10
- request_with_publisher options
11
- end
12
-
13
- def self.places options = {}
14
- endpoint "#{@base_endpoint}/places"
15
- request_with_publisher options
16
- end
5
+ define_action :where, :get, "search/where", :auth_token => false, :publisher => true, :client_ip => true
6
+ define_action :places, :get, "search/places", :auth_token => false, :publisher => true, :client_ip => true
17
7
  end
18
8
  end
19
9
  end
@@ -2,35 +2,13 @@ class CityGrid
2
2
  class API
3
3
  class Content
4
4
  class Places < Content
5
- base_uri "api.citygridmedia.com"
6
- endpoint "/content/places/v2"
5
+ extend CityGrid::API::Mutatable
7
6
 
8
- class << self
9
- def detail opts
10
- Detail.request opts
11
- end
12
-
13
- def search opts
14
- Search.request opts
15
- end
16
-
17
- def mutate options = {}
18
- token = extract_auth_token options
19
- request_and_handle :post,
20
- "#{endpoint}/mutate",
21
- :body => options.to_json,
22
- :headers => merge_headers("authToken" => token)
23
- end
24
-
25
- end
7
+ define_action :detail, :get, "detail", :auth_token => false, :publisher => true, :client_ip => true
8
+ define_action :search, :get, "search/where", :auth_token => false, :publisher => true, :client_ip => true
26
9
  end
27
10
  end
28
11
  end
29
12
  end
30
13
 
31
- [
32
- "detail", "search"
33
- ].each do |x|
34
- require "citygrid/api/content/places/#{x}"
35
- end
36
-
14
+ Dir[File.dirname(__FILE__) + '/places/*.rb'].each {|file| require file }
@@ -5,8 +5,4 @@ class CityGrid
5
5
  end
6
6
  end
7
7
 
8
- [
9
- "offers", "places", "reviews"
10
- ].each do |x|
11
- require "citygrid/api/content/#{x}"
12
- end
8
+ Dir[File.dirname(__FILE__) + '/content/*.rb'].each {|file| require file }
@@ -0,0 +1,15 @@
1
+ # /mutate endpoint
2
+
3
+ class CityGrid
4
+ class API
5
+ module Mutatable
6
+ def mutate options = {}
7
+ token = extract_auth_token options
8
+ request_and_handle :post,
9
+ "#{endpoint}/mutate",
10
+ :body => options.to_json,
11
+ :headers => merge_headers("authToken" => token)
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,15 @@
1
+ # /get endpoint
2
+
3
+ class CityGrid
4
+ class API
5
+ module Searchable
6
+ def search options = {}
7
+ token = extract_auth_token options
8
+ request_and_handle :get,
9
+ "#{endpoint}/get",
10
+ :query => options,
11
+ :headers => merge_headers("authToken" => token)
12
+ end
13
+ end
14
+ end
15
+ end
data/lib/citygrid/api.rb CHANGED
@@ -7,69 +7,72 @@ class CityGrid
7
7
  format :json
8
8
  #debug_output $stderr
9
9
 
10
+ DEFAULT_HEADERS = {
11
+ "Accept" => "application/json",
12
+ "Content-Type" => "Application/JSON"}
13
+
14
+ DEFAULT_DEFINE_OPTIONS = {
15
+ :auth_token => false,
16
+ :format => true,
17
+ :publisher => false,
18
+ :client_ip => false
19
+ }
20
+
10
21
  class << self
11
- # server setting - :default or :ssl
12
- def server val=nil
13
- return @server || (superclass.respond_to?(:server) ? superclass.server : nil) unless val
14
- @server = val
15
- end
22
+ def define_action name, method, action, define_options = {}
23
+ # options:
24
+ # auth_token: pass along auth_token
25
+ # publisher: pass CityGrid.publisher, currently needed for content APIs
26
+ # ip: pass IP, needed for content
27
+ # format: pass JSON flag
28
+
29
+ define_options = DEFAULT_DEFINE_OPTIONS.merge define_options
30
+
31
+ define_singleton_method name.intern do |*args|
32
+ options = args.first.clone || {}
33
+ params = {}
34
+
35
+ token = options.delete :token
36
+
37
+ headers = API::DEFAULT_HEADERS.clone
38
+
39
+ headers.merge! "authToken" => token if define_options[:auth_token]
16
40
 
17
- def hostname val=nil
18
- return @hostname || (superclass.respond_to?(:hostname) ? superclass.hostname : nil) unless val
19
- @hostname = val
41
+ options.merge! "format" => "json" if define_options[:format]
42
+ options.merge! "publisher" => CityGrid.publisher if define_options[:publisher]
43
+ options.merge! "client_ip" => "192.168.0.1" if define_options[:client_ip]
44
+
45
+ params[:headers] = headers
46
+
47
+ case method
48
+ when :post
49
+ params[:body] = options.to_json
50
+ when :get
51
+ params[:query] = options
52
+ end
53
+
54
+ request_and_handle method.intern, "#{endpoint}/#{action}", params
55
+ end
20
56
  end
21
-
22
- def endpoint val=nil
57
+
58
+ def endpoint val = nil
23
59
  return @endpoint unless val
24
60
  @endpoint = val
25
61
  end
26
62
 
27
- def publisher
28
- CityGrid.publisher
29
- end
30
-
31
63
  def request options = {}
32
64
  method = (options.delete(:method) || :get).to_sym
33
65
  query = options.merge :format => :json
34
66
  request_and_handle method, endpoint, :query => query
35
67
  end
36
68
 
37
- def request_with_publisher options = {}
38
- request options.merge(:publisher => publisher)
39
- end
40
-
41
- def mutate options = {}
42
- token = extract_auth_token options
43
- request_and_handle :post,
44
- "#{endpoint}/mutate",
45
- :body => options.to_json,
46
- :headers => merge_headers("authToken" => token)
47
- end
48
-
49
- def search options = {}
50
- token = extract_auth_token options
51
- request_and_handle :get,
52
- "#{endpoint}/get",
53
- :query => options,
54
- :headers => merge_headers("authToken" => token)
55
- end
56
-
57
69
  private
58
- def extract_auth_token options = {}
59
- options.delete(:token) #|| raise(MissingAuthToken)
60
- end
61
-
62
- def merge_headers options = {}
63
- {"Accept" => "application/json",
64
- "Content-Type" => "Application/JSON"}.merge options
70
+ def extract_auth_token options = {}
71
+ options.delete(:token) #|| raise(MissingAuthToken)
65
72
  end
66
-
67
- def convert_to_querystring hash
68
- hash.map do |k, v|
69
- key = URI.escape k.to_s, Regexp.new("[^#{URI::PATTERN::UNRESERVED}]")
70
- val = URI.escape v.to_s, Regexp.new("[^#{URI::PATTERN::UNRESERVED}]")
71
- !value.empty? && !key.empty? ? "#{key}=#{value}" : nil
72
- end.compact.join("&")
73
+
74
+ def merge_headers options = {}
75
+ DEFAULT_HEADERS.merge options
73
76
  end
74
77
 
75
78
  HTTP_METHODS = {
@@ -90,7 +93,6 @@ class CityGrid
90
93
  end
91
94
 
92
95
  req_options = default_options.dup
93
- req_options = req_options.merge({:base_uri => CityGrid.config[server]}) if !base_uri && server
94
96
  req_options = req_options.merge(options)
95
97
 
96
98
  req = HTTParty::Request.new http_method, path, req_options
@@ -100,10 +102,10 @@ class CityGrid
100
102
  response = req.perform
101
103
  rescue => ex
102
104
  puts "Something went wrong with Request.perform"
103
- error = StandardError.new "Internal Error"
105
+ error = ex
104
106
  rescue Psych::SyntaxError => ex
105
107
  puts "Something went wrong with Request.perform, Psych:SyntaxError"
106
- error = StandardError.new "Internal Error"
108
+ error = ex
107
109
  end
108
110
 
109
111
  if defined?(Rails.logger)
@@ -124,7 +126,7 @@ class CityGrid
124
126
 
125
127
  if error
126
128
  puts "an error happened"
127
- puts req.to_json
129
+ ap error
128
130
  #raise error
129
131
  end
130
132
  end
@@ -1,11 +1,8 @@
1
1
  class CityGrid
2
2
  module DetailsMethods
3
- def api
4
- CityGrid::API::Content::Places::Detail
5
- end
6
-
7
- def request opts = {}
8
- api.request_with_publisher opts.merge(:client_ip => "192.168.0.1")
3
+
4
+ def request options = {}
5
+ CityGrid::API::Content::Places.detail options
9
6
  end
10
7
  end
11
8
 
@@ -1,11 +1,7 @@
1
1
  class CityGrid
2
2
  class Offers < Abstraction::Collection
3
- def api
4
- CityGrid::API::Content::Offers
5
- end
6
-
7
3
  def request opts = {}
8
- api.request_with_publisher opts
4
+ CityGrid::API::Content::Offers.search opts
9
5
  end
10
6
 
11
7
  private
@@ -1,11 +1,8 @@
1
1
  class CityGrid
2
2
  class Search < Abstraction::Collection
3
- def api
4
- CityGrid::API::Content::Places::Search
5
- end
6
-
7
- def request opts = {}
8
- api.request_with_publisher opts
3
+
4
+ def request options = {}
5
+ CityGrid::API::Content::Places.search options
9
6
  end
10
7
 
11
8
  private
@@ -0,0 +1,65 @@
1
+ class CityGrid
2
+ class Session
3
+ class APIAccessor
4
+ attr_accessor :session
5
+
6
+ def initialize session
7
+ self.session = session
8
+ end
9
+
10
+ def content klass
11
+ end
12
+
13
+ def ad_center klass
14
+ end
15
+ end
16
+
17
+ attr_accessor :username, :auth_token, :logged_in, :api
18
+
19
+ def initialize
20
+ self.logged_in = false
21
+ self.api = {:ad_center => 1, :content => 2}
22
+ end
23
+
24
+ def logged_in?
25
+ self.logged_in == true
26
+ end
27
+
28
+ def login username, password
29
+ res = CityGrid::API::Accounts::Authentication.login :username => username, :password => password
30
+ self.username = username
31
+
32
+ case res.code
33
+ when 201
34
+ self.auth_token = res.authToken
35
+ self.logged_in = true
36
+ # puts "logged in ok"
37
+ when 400
38
+ # puts "error"
39
+ else
40
+ # puts "unexpected response code"
41
+ end
42
+ end
43
+
44
+ def call_api klass, action, options = {}
45
+ klass.send action, options.merge(:token => self.auth_token)
46
+ end
47
+
48
+ class << self
49
+ def login username, password
50
+ session = Session.new
51
+ session.login username, password
52
+
53
+ session
54
+ end
55
+
56
+ def from_auth_token auth_token
57
+ session = Session.new
58
+ session.auth_token = auth_token
59
+ session.logged_in = true
60
+
61
+ session
62
+ end
63
+ end
64
+ end
65
+ end
data/lib/citygrid.rb CHANGED
@@ -2,6 +2,10 @@
2
2
  # Ruby 1.8.7 doesn't have YAML::ENGINE, this should take care of both cases
3
3
  YAML::ENGINE.yamler= 'syck' if defined?(YAML::ENGINE)
4
4
 
5
+ # load monkey patches
6
+ require "string_ext"
7
+ require "request_ext"
8
+
5
9
  class CityGrid
6
10
  class << self
7
11
  def publisher= code
@@ -28,13 +32,18 @@ class CityGrid
28
32
  def reviews opts
29
33
  Reviews.new opts
30
34
  end
31
-
35
+
36
+ def session username, password
37
+ Session.login username, password
38
+ end
39
+
32
40
  def authenticate params
33
- API::AdCenter::Authentication.login params
41
+ API::Accounts::User.login params
34
42
  end
35
43
  alias_method :login, :authenticate
36
44
 
37
- # config info
45
+ # load in configs
46
+ # see citygrid_api.yml.sample for format
38
47
  def load_config file_path, env = nil
39
48
  config = YAML.load_file(file_path)
40
49
 
@@ -42,58 +51,36 @@ class CityGrid
42
51
 
43
52
  default_hostname = "http://#{defaults["hostname"]}"
44
53
  ssl_hostname = "https://#{defaults["ssl_hostname"]}"
45
-
46
- config["endpoints"].each do |k, v|
47
- # camelcase classname
48
- classname = k.gsub(/(_(.))/) { $2.upcase}.gsub(/^(.)/) { $1.upcase }
49
- klass = API::AdCenter.const_get(classname)
50
- if v.is_a? String
51
- endpoint = v.start_with?("/") ? v : "/#{v}"
52
- klass.endpoint endpoint
53
- klass.base_uri default_hostname
54
- elsif v.is_a? Hash
55
- hostname = v["hostname"] || (v["ssl"] ? ssl_hostname : default_hostname)
56
54
 
57
- endpoint = v["endpoint"].start_with?("/") ? v["endpoint"] : "/#{v["endpoint"]}"
58
- klass.endpoint endpoint
59
- klass.base_uri hostname
60
- else
61
- # should not get here
62
- end
63
- end
64
- end
65
-
66
- def config
67
- raise EndpointsNotConfigured unless @config && !@config.nil?
68
- @config
69
- end
70
-
71
- def set_endpoints config_file
72
- File.open config_file, "r" do |file|
73
- while line = file.gets
74
- api, endpoint = line.split("=").map{|x| x.chomp}
75
- endpoint = "/#{endpoint}" unless endpoint.start_with?("/")
76
- klass = CLASS_MAPPING[api]
77
- next unless klass
55
+ config["api"].each do |n, endpoints|
56
+ # namespace would be ad_center or content
57
+ namespace = API.const_get n.camelcase
58
+
59
+ endpoints.each do |k, v|
60
+ # camelcase classname
61
+ klass = namespace.const_get(k.camelcase)
78
62
 
79
- klass.endpoint endpoint
80
- end
81
- end
82
- end
83
-
84
- def set_env config_file
85
- File.open config_file, "r" do |file|
86
- while line = file.gets
87
- api, host = line.split("=").map{|x| x.chomp}
88
- host = "http://#{host}" unless host.start_with?("http")
89
- klass = CLASS_MAPPING[api]
90
- next unless klass
63
+ if v.is_a? String
64
+ # if value is a plain String, that's the endpoint
65
+ endpoint = v.start_with?("/") ? v : "/#{v}"
66
+ klass.endpoint endpoint
67
+ klass.base_uri default_hostname
68
+ elsif v.is_a? Hash
69
+ # if value is a Hash, fetch endpoint
70
+ # if hostname is set, use it
71
+ # otherwise if ssl is set then use ssl_hostname. fallback to default_hostname
72
+ hostname = v["hostname"] || (v["ssl"] ? ssl_hostname : default_hostname)
73
+ endpoint = v["endpoint"].start_with?("/") ? v["endpoint"] : "/#{v["endpoint"]}"
74
+ klass.endpoint endpoint
75
+ klass.base_uri hostname
76
+ else
77
+ # should not get here. value should be String or Hash
78
+ end
91
79
 
92
- klass.base_uri host
80
+ # puts "#{klass.name} => #{klass.base_uri} : #{klass.endpoint}"
93
81
  end
94
82
  end
95
83
  end
96
-
97
84
  end
98
85
 
99
86
  # Errors
@@ -106,45 +93,29 @@ class CityGrid
106
93
 
107
94
  class EndpointsNotConfigured < StandardError
108
95
  def initialize
109
- super "Endpoints haven't been configured. Run 'CityGrid.load_config'"
96
+ super "Endpoint is not properly configured. Run 'CityGrid.load_config'"
110
97
  end
111
98
  end
112
99
  end
113
100
 
114
101
  require "citygrid/abstraction"
115
102
  require "citygrid/api"
103
+ require "yaml"
104
+
105
+ require "citygrid/api/mutatable"
106
+ require "citygrid/api/searchable"
116
107
 
117
108
  require "citygrid/search"
118
109
  require "citygrid/reviews"
119
110
  require "citygrid/offers"
120
111
  require "citygrid/details"
121
112
  require "citygrid/listing"
113
+ require "citygrid/session"
122
114
 
123
115
  require "citygrid/api/response"
124
- require "citygrid/api/ad_center"
125
- require "citygrid/api/content"
126
116
 
127
- require "request_ext"
117
+ require "citygrid/api/accounts"
118
+ require "citygrid/api/advertising"
119
+ require "citygrid/api/content"
128
120
 
129
- class CityGrid
130
- CLASS_MAPPING = {
131
- "account" => CityGrid::API::AdCenter::Account,
132
- "accountmanager" => CityGrid::API::AdCenter::AccountManager,
133
- "adgroup" => CityGrid::API::AdCenter::AdGroup,
134
- "adgroupad" => CityGrid::API::AdCenter::AdGroupAd,
135
- "adgroupgeo" => CityGrid::API::AdCenter::AdGroupGeo,
136
- "billing" => CityGrid::API::AdCenter::Billing,
137
- "adgroupcriterion" => CityGrid::API::AdCenter::AdGroupCriterion,
138
- "authentication" => CityGrid::API::AdCenter::Authentication,
139
- "budget" => CityGrid::API::AdCenter::Budget,
140
- "campaign" => CityGrid::API::AdCenter::Campaign,
141
- "category" => CityGrid::API::AdCenter::Category,
142
- "geolocation" => CityGrid::API::AdCenter::GeoLocation,
143
- "mop" => CityGrid::API::AdCenter::MethodOfPayment,
144
- "image" => CityGrid::API::AdCenter::Image,
145
- "places" => CityGrid::API::AdCenter::Places,
146
- "performance" => CityGrid::API::AdCenter::Performance,
147
- # "reviews" => CityGrid::API::AdCenter::Reviews
148
- "user" => CityGrid::API::AdCenter::User
149
- }
150
- end
121
+ require "request_ext"
data/lib/request_ext.rb CHANGED
@@ -1,14 +1,13 @@
1
1
  module HTTParty
2
2
  class Request
3
3
 
4
+ # HTTParty::Request does some weird things with url params (namely, repeating them)
5
+ # use last_uri if it exists
4
6
  def get_uri
5
- if (self.respond_to?('last_uri') && last_uri)
6
- return last_uri
7
- else
8
- return uri
9
- end
7
+ (self.respond_to?('last_uri') && last_uri) ? last_uri : uri
10
8
  end
11
9
 
10
+ # monkey patch to_json and to_curl methods into Request
12
11
  def to_json
13
12
  {
14
13
  "requestUrl" => get_uri,
data/lib/string_ext.rb ADDED
@@ -0,0 +1,7 @@
1
+ # monkey patch camel-casing
2
+
3
+ class String
4
+ def camelcase
5
+ self.gsub(/(_(.))/) { $2.upcase}.gsub(/^(.)/) { $1.upcase }
6
+ end
7
+ end