Class: Track

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

Instance Method Summary collapse

Constructor Details

#initialize(number) ⇒ Track

Returns a new instance of Track.



25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/has_tracking.rb', line 25

def initialize(number)
  begin
    buffer = Curl::Easy.new("http://sporing.bring.no/sporing.json?q=#{number}")
    buffer.headers["User-Agent"] = "Ruby/#{RUBY_VERSION}"
    buffer.perform
    @result = JSON.parse(buffer.body_str)["consignmentSet"][0]["packageSet"][0]
    @exists = true
  rescue
    @exists = false
    return false
  end
end

Instance Method Details

#detailsObject



72
73
74
75
76
77
78
79
80
# File 'lib/has_tracking.rb', line 72

def details
  details = {}
  details["weight"] = @result["weightInKgs"]
  details["length"] = @result["lengthInCm"]
  details["height"] = @result["heightInCm"]
  details["width"] = @result["widthInCm"]
  details["volume"] = @result["volumeInDm3"]
  return details
end

#eventsObject



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/has_tracking.rb', line 54

def events
  events = []
  for event in @result["eventSet"]
    e = {}
    e["city"] = event["city"]
    e["postalcode"] = event["postalCode"]
    e["date"] = DateTime.parse(event["dateIso"])
    e["description"] = event["description"]
    e["status"] = event["status"]
    events << e
  end
  if events.length == 0
    return false
  else
    return events.reverse!
  end
end

#exists?Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/has_tracking.rb', line 38

def exists?
  return @exists
end

#received?Boolean

Returns:

  • (Boolean)


42
43
44
45
46
47
48
49
50
51
52
# File 'lib/has_tracking.rb', line 42

def received?
  begin
    if self.events.last["status"] == "DELIVERED"
      return true
    else
      return false
    end
  rescue
    return false
  end
end