Class: Callsign

Inherits:
Object
  • Object
show all
Defined in:
lib/callsign.rb

Instance Method Summary collapse

Constructor Details

#initialize(callsign) ⇒ Callsign

Returns a new instance of Callsign.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/callsign.rb', line 15

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.



35
36
37
# File 'lib/callsign.rb', line 35

def method_missing(name)
  @json[name.to_s]
end