Module: TrackTry

Defined in:
lib/track_try.rb,
lib/track_try/errors.rb,
lib/track_try/tracker.rb,
lib/track_try/version.rb,
lib/track_try/tracking.rb,
lib/track_try/configuration.rb

Defined Under Namespace

Modules: Tracking Classes: Configuration, Error

Constant Summary collapse

VERSION =
"0.1.2"

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.configurationObject

Configure TrackTry, config/initializers/track_try.rb

Examples:

TrackTry.configure do |config|
  config.url = ''
  config.api_key = ''
end


15
16
17
# File 'lib/track_try/configuration.rb', line 15

def configuration
  @configuration
end

Class Method Details

.configure {|configuration| ... } ⇒ Object

Yields:



19
20
21
# File 'lib/track_try/configuration.rb', line 19

def self.configure
  yield(configuration)
end

.get_tracking!(carrier_code, tracking_number) ⇒ Object

Get single tracking



11
12
13
14
15
16
17
18
19
# File 'lib/track_try.rb', line 11

def self.get_tracking!(carrier_code, tracking_number)
  if carrier_code.to_s.empty? || tracking_number.to_s.empty?
    raise TrackTry::Error.new('Carrier or tracking code does not exist', 404)
  else
    base_url = TrackTry.configuration.url
    headers = TrackTry.configuration.headers
    tracker('GET', "#{base_url}/trackings/#{carrier_code}/#{tracking_number}", headers)
  end
end

.tracker(method, url, headers, data = nil) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/track_try/tracker.rb', line 7

def self.tracker(method, url, headers, data = nil)
  uri = URI.parse(url)
  if method == 'GET'
    @req = Net::HTTP::Get.new(uri.path, headers)
  elsif method == 'POST'
    @req = Net::HTTP::Post.new(uri.path, headers)
  end
  @req.body = data.to_json unless data.nil?

  https = Net::HTTP.new(uri.host, uri.port)
  https.use_ssl = true
  response = https.request(@req)
  if [200, 201].include?(response.code.to_i)
    response
  else
    raise TrackTry::Error.new(response.message, response.code)
  end
end