notams 0.0.1 → 0.1

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -1,8 +1,12 @@
1
- # Notams [![Build Status](https://secure.travis-ci.org/tarakanbg/notams.png)](http://travis-ci.org/tarakanbg/notams)
1
+ # Notams
2
2
 
3
3
  A ruby gem for retrieving the currently active NOTAMs for an airport or a region.
4
- Supports multiple airports/regions in one request. Pulls data from [FAA](http://www.faa.gov/) website.
5
- Depends on `nokogiri` for the heavy lifting.
4
+ Supports multiple airports/regions in one request. Pulls data from
5
+ [FAA](http://www.faa.gov/) website. Depends on `nokogiri` for the heavy lifting.
6
+
7
+ [![Build Status](https://secure.travis-ci.org/tarakanbg/notams.png)](http://travis-ci.org/tarakanbg/notams)
8
+ [![Code Climate](https://codeclimate.com/badge.png)](https://codeclimate.com/github/tarakanbg/notams)
9
+ [![Gemnasium](https://gemnasium.com/tarakanbg/notams.png?travis)](https://gemnasium.com/tarakanbg/notams)
6
10
 
7
11
  ## Installation
8
12
 
@@ -20,10 +24,14 @@ Or install it yourself as:
20
24
 
21
25
  ## Usage
22
26
 
23
- The `.notams` method can be applied to any string (or variable containing a string), representing a valid
24
- ICAO code of an airport or FIR, or a comma-separated list of airports/regions. It will return an array,
25
- containing all the **currently active** NOTAMs for your selection. You can loop through the array to display or
26
- parse individual notams.
27
+ ### Easy mode
28
+
29
+ The `.notams` method can be applied to any string literal or variable,
30
+ representing a valid ICAO code of an airport or FIR, or a comma-separated list
31
+ of airports/regions. It will return an array, containing all the **currently
32
+ active** NOTAMs for your selection. You can loop through the array to display or
33
+ parse individual notams. I figured this would be the most common use case. For
34
+ advanced usage and possible customizations [see below](#advanced-usage).
27
35
 
28
36
  ```ruby
29
37
  icao = "lowi"
@@ -32,13 +40,84 @@ icao.notams # => returns an array containing all NOTAMs for Innsbruck airport
32
40
  "lowi".notams # => same as above
33
41
 
34
42
  icao = "lqsa,lqsb"
35
- icao.notams # => returns an array containing all NOTAMs for Sarajevo Airport and Bosnia and Herzegovina FIR
43
+ icao.notams # => returns an array containing all NOTAMs for Sarajevo Airport
44
+ # and Bosnia and Herzegovina FIR
36
45
  ```
37
46
 
47
+ ### Advanced usage
48
+
49
+ The `.notams` method can be customized by passing an optional hash of arguments.
50
+
51
+ The `:objects => true` option will cause the `.notams` method to return an array
52
+ of notam **objects** instead of strings. Thus each notam is parsed and
53
+ encapuslated in an instance of the `Notam` class and exposes a number of
54
+ **attributes**:
55
+
56
+ ```ruby
57
+ icao = "lqsa"
58
+ icao.notams(:objects => true) # => returns an array of notam objects
59
+
60
+ notam = icao.notams.first # => returns the first notam as an object
61
+
62
+ # Notam object attributes
63
+
64
+ notam.raw # => returns the raw (unprocessed) notam as a string
65
+ notam.icao # => returns the icao code of the airport or area, covered by the notam;
66
+ # useful when iterating over multiple notams, covering a collection of airports or areas
67
+ notam.message # => returns the actual information message of the notam as a string
68
+ ```
69
+
70
+ ### Example Ruby on Rails implementation
71
+
72
+ Here's a possible scenario of using this gem in a Ruby on Rails application.
73
+ Verbosity is kept on purpose for clarity.
74
+
75
+ **In your controller:**
76
+ ```ruby
77
+ def action
78
+
79
+ # We define the airpots and/or areas that we're interested in.
80
+ # In this case this is Sarajevo Airport and the entire Bosnia & Herzegovina FIR
81
+
82
+ icao = "lqsa, lqsb"
83
+
84
+ # Now we want to pull all the latest notams for both these areas.
85
+ # We also want to pull them as objects, so that we can process their attributes
86
+ # separately in our views. We're assigning the result to the @notams instance
87
+ # variable which we will use in our views
88
+
89
+ @notams = icao.notams(:objects => true)
90
+
91
+ end
92
+ ```
93
+
94
+ **In your view (HAML is used for clarity):**
95
+
96
+ ```haml
97
+ // We could play with the attributes individually if we need to:
98
+ - for notam in @notams
99
+ %li
100
+ = notam.icao
101
+ = notam.message
102
+
103
+ // Or we could just loop through the raw notams:
104
+ - for notam in @notams
105
+ %li= notam.raw
106
+ ```
107
+
108
+ ## Changelog
109
+
110
+ ### v. 0.1 28 July 2012
111
+
112
+ * added optional arguments customization to the `.notams` method (see [Advanced Usage](#advanced-usage))
113
+ * code refactored into classes for flexibility
114
+
115
+
38
116
  ## Contributing
39
117
 
40
118
  1. Fork it
41
119
  2. Create your feature branch (`git checkout -b my-new-feature`)
42
120
  3. Commit your changes (`git commit -am 'Added some feature'`)
43
- 4. Push to the branch (`git push origin my-new-feature`)
44
- 5. Create new Pull Request
121
+ 4. Make sure all tests are passing
122
+ 5. Push to the branch (`git push origin my-new-feature`)
123
+ 6. Create new Pull Request
@@ -1,25 +1,82 @@
1
1
  require "notams/version"
2
- require 'nokogiri'
3
- require 'open-uri'
4
-
5
2
 
6
3
  class String
7
- def notams
8
- Notams.notams(self)
4
+ def notams(args={})
5
+ Notams.notams(self, args)
9
6
  end
10
7
  end
11
8
 
12
9
  module Notams
13
- def self.notams(icao)
14
- notams = []
15
- get_raw_page(icao).css("pre").each {|n| notams << n.text }
16
- return notams
10
+ def self.notams(icao, args)
11
+ NotamFetcher.new(icao, args).fetch
12
+ end
13
+
14
+ class NotamFetcher
15
+ require 'nokogiri'
16
+ require 'open-uri'
17
+
18
+ attr_accessor :icao, :obj_list, :notams, :string_notams
19
+
20
+ def initialize(icao, args = nil)
21
+ @icao = icao
22
+ process_arguments(args) if args.class == Hash
23
+ @notams = []; @string_notams = []
24
+ notams_object_list
25
+ notams_string_list
26
+ end
27
+
28
+ def raw_list
29
+ Nokogiri::HTML(open("https://pilotweb.nas.faa.gov/PilotWeb/notamRetrievalByICAOAction.do?method=displayByICAOs&reportType=RAW&formatType=DOMESTIC&retrieveLocId=#{@icao}&actionType=notamRetrievalByICAOs"))
30
+ end
31
+
32
+ def fetch
33
+ @obj_list == true ? @notams : @string_notams
34
+ end
35
+
36
+ private
37
+
38
+ def notams_string_list
39
+ @notams.each {|n| @string_notams << n.to_s}
40
+ end
41
+
42
+ def notams_object_list
43
+ raw_list.css("pre").each {|n| @notams << Notam.new(n.text)}
44
+ end
45
+
46
+ def process_arguments(args)
47
+ args[:objects] == true ? @obj_list = true : @obj_list = false
48
+ end
49
+
17
50
  end
18
51
 
19
- private
52
+ class Notam
53
+
54
+ attr_accessor :raw, :message, :icao
55
+
56
+ def initialize(text)
57
+ @raw = text
58
+ @message = get_message
59
+ @icao = get_icao
60
+ end
61
+
62
+ def to_s
63
+ @raw
64
+ end
65
+
66
+ private
67
+
68
+ def get_message
69
+ starts = @raw.index('E)') + 3
70
+ @raw.index('F)') ? ends = @raw.index('F)') : ends = @raw.length
71
+ @raw[starts..ends]
72
+ end
73
+
74
+ def get_icao
75
+ starts = @raw.index('A)') + 3
76
+ ends = @raw.index('B)') - 2
77
+ @raw[starts..ends]
78
+ end
20
79
 
21
- def self.get_raw_page(icao)
22
- Nokogiri::HTML(open("https://pilotweb.nas.faa.gov/PilotWeb/notamRetrievalByICAOAction.do?method=displayByICAOs&reportType=RAW&formatType=DOMESTIC&retrieveLocId=#{icao}&actionType=notamRetrievalByICAOs"))
23
80
  end
24
81
 
25
82
  end
@@ -1,3 +1,3 @@
1
1
  module Notams
2
- VERSION = "0.0.1"
2
+ VERSION = "0.1"
3
3
  end
@@ -20,6 +20,6 @@ Gem::Specification.new do |gem|
20
20
  gem.add_development_dependency "guard"
21
21
  gem.add_development_dependency "libnotify"
22
22
  gem.add_development_dependency "guard-rspec"
23
- gem.add_dependency "nokogiri"
23
+ gem.add_dependency "nokogiri", "~> 1.5.5"
24
24
 
25
25
  end
@@ -36,15 +36,46 @@ describe String do
36
36
  "lqsa".notams.length.should_not eq("lqsa,lqsb".notams.length)
37
37
  end
38
38
 
39
+ it "should process params" do
40
+ "lqsa".notams(:objects => false).should eq("lqsa".notams)
41
+ "lqsa".notams(:objects => false).should_not eq("lqsa".notams(:objects => true))
42
+ end
43
+
44
+ it "should return objects when argument is set" do
45
+ icao = "lqsa"
46
+ icao.notams(:objects => true).class.should eq(Array)
47
+ Notams::NotamFetcher.new(icao, :objects => true).obj_list.should eq(true)
48
+ Notams::NotamFetcher.new(icao, :objects => true).notams.should_not eq(Notams::NotamFetcher.new(icao, :objects => true).string_notams)
49
+ icao.notams(:objects => true).first.class.should_not eq(String)
50
+ icao.notams(:objects => true).first.class.should eq(Notams::Notam)
51
+ end
52
+
53
+ it "should properly return the notam object message" do
54
+ icao = "lqsa"
55
+ Notams::NotamFetcher.new(icao, :objects => true).fetch.first.message.should eq(icao.notams(:objects => true).first.message)
56
+ icao.notams(:objects => true).first.message.class.should eq(String)
57
+ # "lqsb".notams(:objects => true).first.message.should eq("moo")
58
+ end
59
+
60
+ it "should properly return the notam object icao" do
61
+ icao = "lqsa"
62
+ Notams::NotamFetcher.new(icao, :objects => true).fetch.first.icao.should eq(icao.notams(:objects => true).first.icao)
63
+ icao.notams(:objects => true).first.icao.class.should eq(String)
64
+ icao.notams(:objects => true).first.icao.should eq("LQSA")
65
+ icao.notams(:objects => true).first.icao.should eq(icao.upcase)
66
+ "lqsb".notams(:objects => true).first.icao.should eq("LQSB")
67
+ end
68
+
69
+
39
70
  end
40
71
 
41
- describe Notams do
72
+ describe Notams::NotamFetcher do
42
73
 
43
- describe "get_raw_page" do
74
+ describe "raw_list" do
44
75
  it "should execute open uri request via Nokogiri" do
45
76
  icao = "lqsa"
46
- Notams.get_raw_page(icao).class.should eq(Nokogiri::HTML::Document)
47
- Notams.get_raw_page(icao).css("title").text.should eq("PilotWeb: Results Page ")
77
+ Notams::NotamFetcher.new(icao).raw_list.class.should eq(Nokogiri::HTML::Document)
78
+ Notams::NotamFetcher.new(icao).raw_list.css("title").text.should eq("PilotWeb: Results Page ")
48
79
  end
49
80
  end
50
81
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: notams
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: '0.1'
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: 2012-07-09 00:00:00.000000000 Z
12
+ date: 2012-07-28 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
@@ -96,17 +96,17 @@ dependencies:
96
96
  requirement: !ruby/object:Gem::Requirement
97
97
  none: false
98
98
  requirements:
99
- - - ! '>='
99
+ - - ~>
100
100
  - !ruby/object:Gem::Version
101
- version: '0'
101
+ version: 1.5.5
102
102
  type: :runtime
103
103
  prerelease: false
104
104
  version_requirements: !ruby/object:Gem::Requirement
105
105
  none: false
106
106
  requirements:
107
- - - ! '>='
107
+ - - ~>
108
108
  - !ruby/object:Gem::Version
109
- version: '0'
109
+ version: 1.5.5
110
110
  description: Retrieves the currently active NOTAMs for an airport or a region. Supports
111
111
  multiple airports/regions in one request. Pulls data from FAA website.
112
112
  email: