Class: Callsign

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#jsonObject

Returns the value of attribute json.



16
17
18
# File 'lib/callsign.rb', line 16

def json
  @json
end

Class Method Details

.search(callsign) ⇒ Object



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

def self.search(callsign)
  Hashie::Mash.new(Callsign.new(callsign).json)
end