api_object 0.3.3 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -35,6 +35,8 @@ This will be defined in the upper object over the function "initialize_from_api"
35
35
 
36
36
  :key - api key
37
37
 
38
+ :data_tags - specify tag parameters under which object data might be stored, for example <location value='San Francisco'/> - "value" would be a data tag. :data_tags accepts a single value or an array.
39
+
38
40
  :url_options - parameters
39
41
  ```
40
42
 
@@ -89,12 +91,59 @@ If the data is an array of hashes, then it might be used to create an array of o
89
91
  ```
90
92
  stations = data.map {|d| Station.new(d)}
91
93
  ```
92
- 6) Testing
94
+
95
+ 6) Getting location based data by ip.
96
+
97
+ This gem uses [geo_ip gem](https://github.com/jeroenj/geo_ip) and [ipinfodb.com](http://ipinfodb.com/) webservice to retrieve location based on ip.
98
+
99
+ The service requires an API key, in order to get it [register](http://ipinfodb.com/register.php) at the web site.
100
+
101
+ Consider making a donation to [ipinfodb.com](http://ipinfodb.com/) at [http://ipinfodb.com/donate.php](http://ipinfodb.com/donate.php).
102
+
103
+ To get the data, call "get_results_by_ip" instead of "get_results":
104
+
105
+ ```
106
+ data = Weather.get_results_by_ip('99.156.82.20', KEY, :weather => :zip_code)
107
+ ```
108
+
109
+ The [geo_ip gem](https://github.com/jeroenj/geo_ip) retrieves location as:
110
+
111
+ ```
112
+ {
113
+ :status_code => "OK",
114
+ :status_message => "",
115
+ :ip => "209.85.227.104"
116
+ :country_code => "US",
117
+ :country_name => "UNITED STATES",
118
+ :region_name => "CALIFORNIA",
119
+ :city => "MONTEREY PARK",
120
+ :zip_code => "91754",
121
+ :latitude => "34.0505",
122
+ :longitude => "-118.13"
123
+ }
124
+ ```
125
+
126
+ The third parameter in the function is to define what fields from the location object are passed as what parameter. In this case, the original function is:
127
+
128
+ ```
129
+ data = Weather.get_results(:weather => '99.156.82.20')
130
+ ```
131
+
132
+ 7) Testing
93
133
 
94
134
  The gem has been tested on BART, Google Weather and NextBus APIs.
95
135
 
136
+ To run test by ip location, please, [register](http://ipinfodb.com/register.php) for an API key.
137
+
138
+ The key should be either placed into the test/data/keys/ipinfodb_key.txt file or passed as an environment variable:
139
+
140
+ ```
141
+ API_KEY='\<your key\>' rake test
142
+ ```
143
+
144
+ There is no existing api key provided with this gem as per the Terms and Conditions of the ipinfodb service.
96
145
 
97
- 7) Limitations
146
+ 8) Limitations
98
147
 
99
148
  * Api data must be presented either in XML or in JSON format. The distinction between XML and JSON is determinted automatically.
100
149
  * When using this gem with external APIs, check Terms and Conditions of the API usage.
data/api_object.gemspec CHANGED
@@ -29,5 +29,6 @@ Gem::Specification.new do |s|
29
29
  s.add_dependency "activesupport"
30
30
  s.add_dependency('nori', '>=1.1')
31
31
  s.add_dependency('rest-client', '>= 1.6')
32
+ s.add_dependency 'geo_ip'
32
33
 
33
34
  end
data/lib/api_object.rb CHANGED
@@ -1,6 +1,7 @@
1
1
  require "api_object/version"
2
2
  require "api_object/query"
3
3
  require "active_support/all"
4
+ require "geo_ip"
4
5
 
5
6
  module ActiveApi
6
7
 
@@ -15,6 +16,15 @@ module ActiveApi
15
16
  self.url, self.action, self.key, self.mode, self.url_options, self.data_tags, self.object_name = [options[:url], options[:action], options[:key], options[:mode], (options[:url_options] || {}), ([*options[:data_tags]] || []), (options[:object_name] || self.to_s.downcase.gsub(/^(.+::)(.+)$/, '\2'))]
16
17
  instance_eval do
17
18
 
19
+ def get_results_by_ip ip, key, arguments = {}
20
+ self.api_key = key
21
+ location = GeoIp.geolocation(ip)
22
+ raise unless location[:status_code] == "OK"
23
+ get_results [*arguments.keys].inject({}) { |opts, a| opts.merge(a.to_sym => location[arguments[a.to_sym]]) }
24
+ rescue
25
+ puts "WARNING: Cannot get results or location by ip. Verify that you have a valid key for the ipinfodb.com service"
26
+ end
27
+
18
28
  def get_results options = {}
19
29
  self.url_options.merge!(:key => self.key) unless self.key.nil?
20
30
  [:url, :action, :mode].each {|opt| eval("self.#{opt.to_s} = options.delete(opt)") if options[opt]}
@@ -25,6 +35,10 @@ module ActiveApi
25
35
  return {}
26
36
  end
27
37
 
38
+ def api_key=(key)
39
+ GeoIp.api_key = key
40
+ end
41
+
28
42
  def warning_invalid_url url
29
43
  "The request url is #{url}, please, check if it's invalid of there is no connectivity." unless url.nil?
30
44
  end
@@ -1,3 +1,3 @@
1
1
  module ApiObject
2
- VERSION = "0.3.3"
2
+ VERSION = "0.4.0"
3
3
  end
@@ -0,0 +1,49 @@
1
+ <xml_api_reply version="1">
2
+ <weather module_id="0" tab_id="0" mobile_row="0" mobile_zipped="1" row="0" section="0">
3
+ <forecast_information>
4
+ <city data="Austin, TX"/>
5
+ <postal_code data="78744"/>
6
+ <latitude_e6 data=""/>
7
+ <longitude_e6 data=""/>
8
+ <forecast_date data="2012-06-12"/>
9
+ <current_date_time data="2012-06-13 03:53:00 +0000"/>
10
+ <unit_system data="US"/>
11
+ </forecast_information>
12
+ <current_conditions>
13
+ <condition data="Partly Cloudy"/>
14
+ <temp_f data="70"/>
15
+ <temp_c data="21"/>
16
+ <humidity data="Humidity: 84%"/>
17
+ <icon data="/ig/images/weather/partly_cloudy.gif"/>
18
+ <wind_condition data="Wind: W at 5 mph"/>
19
+ </current_conditions>
20
+ <forecast_conditions>
21
+ <day_of_week data="Tue"/>
22
+ <low data="73"/>
23
+ <high data="99"/>
24
+ <icon data="/ig/images/weather/chance_of_storm.gif"/>
25
+ <condition data="Chance of Storm"/>
26
+ </forecast_conditions>
27
+ <forecast_conditions>
28
+ <day_of_week data="Wed"/>
29
+ <low data="73"/>
30
+ <high data="95"/>
31
+ <icon data="/ig/images/weather/mostly_sunny.gif"/>
32
+ <condition data="Mostly Sunny"/>
33
+ </forecast_conditions>
34
+ <forecast_conditions>
35
+ <day_of_week data="Thu"/>
36
+ <low data="75"/>
37
+ <high data="95"/>
38
+ <icon data="/ig/images/weather/mostly_sunny.gif"/>
39
+ <condition data="Mostly Sunny"/>
40
+ </forecast_conditions>
41
+ <forecast_conditions>
42
+ <day_of_week data="Fri"/>
43
+ <low data="70"/>
44
+ <high data="95"/>
45
+ <icon data="/ig/images/weather/mostly_sunny.gif"/>
46
+ <condition data="Mostly Sunny"/>
47
+ </forecast_conditions>
48
+ </weather>
49
+ </xml_api_reply>
@@ -12,14 +12,16 @@ class ApiObjectTest < MiniTest::Unit::TestCase
12
12
  @@estimate_directory = @@data_directory + "/estimate"
13
13
  @@weather_directory = @@data_directory + "/weather"
14
14
  @@bus_directory = @@data_directory + "/bus"
15
+ @@key_directory = @@data_directory +"/keys"
15
16
 
16
17
  @@glen_estimate = Station.load_from_xml(File.read(@@estimate_directory + '/glen.xml'))
17
18
  @@sixteenth_estimate = Station.load_from_xml(File.read(@@estimate_directory + '/sixteenth.xml'))
18
19
  @@twenty_fourth_estimate = Station.load_from_xml(File.read(@@estimate_directory + '/twenty_fourth.xml'))
19
20
 
20
-
21
-
22
- @@weather = Weather.load_from_xml(File.read(@@weather_directory + '/mountain_view.xml'))
21
+ @@weather_mv = Weather.load_from_xml(File.read(@@weather_directory + '/mountain_view.xml'))
22
+ @@weather_au = Weather.load_from_xml(File.read(@@weather_directory + '/austin.xml'))
23
+ @@ip_key = File.read(@@key_directory + "/ipinfodb_key.txt") rescue ENV['API_KEY']
24
+ IP = '99.156.82.20'
23
25
 
24
26
  @@muni_routes = Route.load_from_xml(File.read(@@bus_directory + "/muni_routes.xml"))
25
27
  @@muni_F = Route.load_from_xml(File.read(@@bus_directory + "/muni_F.xml"))
@@ -50,8 +52,17 @@ class ApiObjectTest < MiniTest::Unit::TestCase
50
52
 
51
53
 
52
54
  def test_should_get_correct_weather
53
- weather = Weather.new(Weather.get_results(:weather => 'Mountain+View'))
54
- assert_equal(weather, @@weather)
55
+ weather_mv = Weather.new(Weather.get_results(:weather => 'Mountain+View'))
56
+ assert_equal(weather_mv, @@weather_mv)
57
+ weather_au = Weather.new(Weather.get_results(:weather => '78744'))
58
+ assert_equal(weather_au, @@weather_au)
59
+ end
60
+
61
+ def test_should_get_correct_weather_by_ip
62
+ unless @@ip_key.nil?
63
+ weather_au = Weather.new(Weather.get_results_by_ip(IP, @@ip_key, :weather => :zip_code))
64
+ assert_equal(weather_au, @@weather_au)
65
+ end
55
66
  end
56
67
 
57
68
  def test_should_get_correct_bus_routes
@@ -98,9 +109,6 @@ private
98
109
 
99
110
  def business_time? now
100
111
  unless now.to_date.sunday?
101
- #t = now.to_time.localtime(now.zone)
102
- #t1 = DateTime.strptime("#{now.month}/#{now.day}/#{now.year} 10:00:00 AM #{now.zone}", "%m/%d/%Y %I:%M:%S %p %:z").to_time.localtime(now.zone)
103
- #t2 = DateTime.strptime("#{now.month}/#{now.day}/#{now.year} 08:00:00 PM #{now.zone}", "%m/%d/%Y %I:%M:%S %p %:z").to_time.localtime(now.zone)
104
112
  t1 = DateTime.strptime("#{now.month}/#{now.day}/#{now.year} 10:00:00 AM #{now.zone}", "%m/%d/%Y %I:%M:%S %p %:z")
105
113
  t2 = DateTime.strptime("#{now.month}/#{now.day}/#{now.year} 08:00:00 PM #{now.zone}", "%m/%d/%Y %I:%M:%S %p %:z")
106
114
  return now > t1 && now < t2
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: api_object
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.3
4
+ version: 0.4.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -13,7 +13,7 @@ date: 2012-06-13 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activemodel
16
- requirement: &70472730 !ruby/object:Gem::Requirement
16
+ requirement: &70091670 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *70472730
24
+ version_requirements: *70091670
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: rspec
27
- requirement: &70472480 !ruby/object:Gem::Requirement
27
+ requirement: &70091430 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '0'
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *70472480
35
+ version_requirements: *70091430
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: minitest
38
- requirement: &70472210 !ruby/object:Gem::Requirement
38
+ requirement: &70091000 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: '0'
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *70472210
46
+ version_requirements: *70091000
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: activesupport
49
- requirement: &70471940 !ruby/object:Gem::Requirement
49
+ requirement: &70090700 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ! '>='
@@ -54,10 +54,10 @@ dependencies:
54
54
  version: '0'
55
55
  type: :runtime
56
56
  prerelease: false
57
- version_requirements: *70471940
57
+ version_requirements: *70090700
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: nori
60
- requirement: &70471570 !ruby/object:Gem::Requirement
60
+ requirement: &70090310 !ruby/object:Gem::Requirement
61
61
  none: false
62
62
  requirements:
63
63
  - - ! '>='
@@ -65,10 +65,10 @@ dependencies:
65
65
  version: '1.1'
66
66
  type: :runtime
67
67
  prerelease: false
68
- version_requirements: *70471570
68
+ version_requirements: *70090310
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: rest-client
71
- requirement: &70471130 !ruby/object:Gem::Requirement
71
+ requirement: &70089920 !ruby/object:Gem::Requirement
72
72
  none: false
73
73
  requirements:
74
74
  - - ! '>='
@@ -76,7 +76,18 @@ dependencies:
76
76
  version: '1.6'
77
77
  type: :runtime
78
78
  prerelease: false
79
- version_requirements: *70471130
79
+ version_requirements: *70089920
80
+ - !ruby/object:Gem::Dependency
81
+ name: geo_ip
82
+ requirement: &70089700 !ruby/object:Gem::Requirement
83
+ none: false
84
+ requirements:
85
+ - - ! '>='
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ type: :runtime
89
+ prerelease: false
90
+ version_requirements: *70089700
80
91
  description: An interface to load objects from external APIs provided in XML and JSON
81
92
  formats
82
93
  email:
@@ -100,6 +111,7 @@ files:
100
111
  - test/data/estimate/glen.xml
101
112
  - test/data/estimate/sixteenth.xml
102
113
  - test/data/estimate/twenty_fourth.xml
114
+ - test/data/weather/austin.xml
103
115
  - test/data/weather/mountain_view.xml
104
116
  - test/unit/api_object_test.rb
105
117
  - test/unit/load_from_xml.rb