marinetraffic 0.0.5 → 0.0.6.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 0f01e7fa9707d497da72d9ef6e12eb34d535fba5
4
- data.tar.gz: 6b0c19cc2e002b3df7d665490807ab82b63eb158
3
+ metadata.gz: dca7779891746440b8e9417d557e58ca35df826c
4
+ data.tar.gz: 385169c61fa198c1ea5b5665f6b1573100a18de8
5
5
  SHA512:
6
- metadata.gz: 14fd7b860b44c1e8d3c50e19623e256be04c567577f8e6ec592ece9d2053c61a97f2f3256aed42a73f438fc9c2344f533ec11afe15ca5aa32c19331599589089
7
- data.tar.gz: 745922760aec1ac8e7bbd79045125dbd031294e657a66aa9a85172182060b2ad074f6a5fc8ef7c14c5a20bbc57de78564be33d25ab86a748911b90a66817f4a9
6
+ metadata.gz: 58f77469499e90d3eed2ac155e78604b4821ee0492c02d8b6988896b2c82f85512c21e0ca6cd6ba0ba68b33bc77f0effe908a67d8f200ccd24a857cade8d94af
7
+ data.tar.gz: b0737a76b56f8dd098580efb557833b6721c47320ea333136c506449f86e18032a1545ecda3bd70406e9e367c844d0767bc73ab2c270901e66623de6390b5b5e
data/lib/marinetraffic.rb CHANGED
@@ -1,9 +1,16 @@
1
1
  require "marinetraffic/version"
2
- require "marinetraffic/vessel"
3
2
  require "marinetraffic/api"
3
+ require "marinetraffic/vessel"
4
+ require "marinetraffic/subscription"
5
+
6
+ require "marinetraffic/errors/marinetraffic_error"
7
+ require "marinetraffic/errors/authentication_error"
8
+ require "marinetraffic/errors/api_error"
4
9
 
5
10
  module Marinetraffic
11
+ @api_base = 'http://services.marinetraffic.com/api'
12
+
6
13
  class << self
7
- attr_accessor :api_key
14
+ attr_accessor :api_key, :api_base
8
15
  end
9
16
  end
@@ -1,26 +1,30 @@
1
1
  require 'faraday'
2
2
  require 'nokogiri'
3
3
 
4
- API_URL = "http://services.marinetraffic.com/api"
5
-
6
4
  module Marinetraffic
7
5
  class API
8
6
  def self.call(name, params = {})
7
+ api_key = params.delete(:api_key)
8
+
9
+ unless api_key ||= Marinetraffic.api_key
10
+ raise AuthenticationError.new('No API key provided. ' \
11
+ 'Set your API key using "Marinetraffic.api_key = <API-KEY>" ' \
12
+ 'or pass it as parameter to your request.')
13
+ end
14
+
9
15
  params[:protocol] = :xml
10
16
  param_string = params.map{|attr| "#{attr.first}:#{attr.last}"}.join('/')
11
- url = "#{API_URL}/#{name}/#{Marinetraffic.api_key}/#{param_string}"
17
+ url = "#{Marinetraffic.api_base}/#{name}/#{api_key}/#{param_string}"
12
18
  response = Faraday.get(url)
13
19
  doc = Nokogiri::XML(response.body)
14
- raise_exception(doc) if doc.xpath('//ERROR').any?
20
+ handle_api_error(response, doc) if doc.xpath('//ERROR').any?
15
21
  doc
16
22
  end
17
23
 
18
- def self.raise_exception(doc)
19
- message = doc.xpath("//ERROR")[0]['DESCRIPTION']
20
- raise MarinetrafficException.new(message)
24
+ def self.handle_api_error(response, doc)
25
+ error = doc.xpath('//ERROR')[0]['DESCRIPTION']
26
+ code = doc.xpath('//ERROR')[0]['CODE']
27
+ raise APIError.new(error, response.status.to_s, response.body, response.headers, code)
21
28
  end
22
29
  end
23
-
24
- class MarinetrafficException < Exception
25
- end
26
30
  end
@@ -0,0 +1,4 @@
1
+ module Marinetraffic
2
+ class APIError < MarinetrafficError
3
+ end
4
+ end
@@ -0,0 +1,4 @@
1
+ module Marinetraffic
2
+ class AuthenticationError < MarinetrafficError
3
+ end
4
+ end
@@ -0,0 +1,22 @@
1
+ module Marinetraffic
2
+ class MarinetrafficError < StandardError
3
+ attr_reader :message
4
+ attr_reader :error_code
5
+ attr_reader :http_status
6
+ attr_reader :http_body
7
+ attr_reader :http_headers
8
+
9
+ def initialize(message=nil, http_status=nil, http_body=nil, http_headers=nil, error_code=nil)
10
+ @message = message
11
+ @http_status = http_status
12
+ @http_body = http_body
13
+ @http_headers = http_headers || {}
14
+ @error_code = error_code
15
+ end
16
+
17
+ def to_s
18
+ code_string = @error_code.nil? ? "" : "(Error #{@error_code}) "
19
+ "#{code_string}#{@message}"
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,31 @@
1
+ module Marinetraffic
2
+ class Subscription
3
+ attr_accessor :api_key
4
+
5
+ def initialize(api_key)
6
+ @api_key = api_key
7
+ end
8
+
9
+ def find(mmsi, extended = false, options = {})
10
+ params = { api_key: api_key }.merge(options)
11
+ params[:msgtype] = :extended if extended
12
+ response = API.call(:exportvessels, params)
13
+ result = response.xpath("//row[@MMSI=#{mmsi}]")[0]
14
+
15
+ if result != nil
16
+ attributes = Marinetraffic::Vessel.map_attributes(result, extended)
17
+ Marinetraffic::Vessel.new(attributes)
18
+ end
19
+ end
20
+
21
+ def all(extended = false, options = {})
22
+ params = { timespan: 20, api_key: api_key }.merge(options)
23
+ params[:msgtype] = :extended if extended
24
+ response = API.call(:exportvessels, params)
25
+ result = response.xpath("//row").map do |row|
26
+ attributes = Marinetraffic::Vessel.map_attributes(row, extended)
27
+ Marinetraffic::Vessel.new(attributes)
28
+ end
29
+ end
30
+ end
31
+ end
@@ -1,3 +1,3 @@
1
1
  module Marinetraffic
2
- VERSION = "0.0.5"
2
+ VERSION = "0.0.6.1"
3
3
  end
@@ -115,7 +115,7 @@ module Marinetraffic
115
115
  # http://help.marinetraffic.com/hc/en-us/articles/205579997-What-is-the-significance-of-the-AIS-SHIPTYPE-number-
116
116
  def ship_type_human
117
117
  return if ship_type == nil
118
- case ship_type / 10
118
+ case ship_type.to_s[0].to_i
119
119
  when 1
120
120
  'Reserved'
121
121
  when 2
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: marinetraffic
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.6.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Fabio Kuhn
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2015-08-24 00:00:00.000000000 Z
11
+ date: 2015-08-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday
@@ -156,6 +156,10 @@ files:
156
156
  - bin/setup
157
157
  - lib/marinetraffic.rb
158
158
  - lib/marinetraffic/api.rb
159
+ - lib/marinetraffic/errors/api_error.rb
160
+ - lib/marinetraffic/errors/authentication_error.rb
161
+ - lib/marinetraffic/errors/marinetraffic_error.rb
162
+ - lib/marinetraffic/subscription.rb
159
163
  - lib/marinetraffic/version.rb
160
164
  - lib/marinetraffic/vessel.rb
161
165
  - marinetraffic.gemspec