Class: Callsign
- Inherits:
-
Object
- Object
- Callsign
- Defined in:
- lib/callsign.rb
Instance Attribute Summary collapse
-
#json ⇒ Object
Returns the value of attribute json.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(callsign) ⇒ Callsign
constructor
A new instance of Callsign.
-
#method_missing(name) ⇒ Object
This literally passes to the JSON response that we get.
Constructor Details
#initialize(callsign) ⇒ Callsign
Returns a new instance of Callsign.
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/callsign.rb', line 18 def initialize(callsign) json_uri = URI.parse "http://callook.info/#{callsign}/json" json_response = Net::HTTP.new(json_uri.host, json_uri.port).get(json_uri.path) raise InvalidHTTPResponseException if json_response.code.to_i > 200 @json = JSON.parse json_response.body # Handle invalid/update before the user can do anything that # would error anyway. case @json['status'] when 'INVALID' raise InvalidCallsignException, 'Invalid callsign' when 'UPDATING' raise CallookUpdateException, 'Callook.info offline for daily update' end end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name) ⇒ Object
This literally passes to the JSON response that we get. This means that if the server returns a string for a key, you will get that. If it returns a hash with more info, so will we. Hey, this is 2.0.0 baby. We’re allowed to play hardball.
42 43 44 |
# File 'lib/callsign.rb', line 42 def method_missing(name) @json[name.to_s] end |
Instance Attribute Details
#json ⇒ Object
Returns the value of attribute json.
16 17 18 |
# File 'lib/callsign.rb', line 16 def json @json end |