apple_warranty_check 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 71194e5d218383d9d37d5f4f0ba7ef5b43c7482e
4
+ data.tar.gz: fb7f3c1ffdbda38240d09cf129f6e170da1b4b9b
5
+ SHA512:
6
+ metadata.gz: ee198ee1535bbaeab46e6170a2d1fdc4526e84b3b1bcd6aea838335f515fcdedb5ab1f0d983439a039e64a167a15b373dec3011af9fe76dde286b1bb8c18faf2
7
+ data.tar.gz: 174ff31b8622a8e56ff06f24a625a89771b3f5815e4ee2c7d92a3387b739b9fbbe0df333bfa245d3aae1d67e9cdf7785c6ba0a1252dfb10681cdd2e65a93c190
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.2
4
+ before_install: gem install bundler -v 1.10.3
data/Gemfile ADDED
@@ -0,0 +1,2 @@
1
+ source 'https://rubygems.org'
2
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Nick Chernyshev
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,45 @@
1
+ # AppleWarrantyCheck
2
+
3
+ [![Build Status](https://travis-ci.org/flowerett/apple_warranty_check.svg?branch=master)](https://travis-ci.org/flowerett/apple_warranty_check)
4
+ [![Code Climate](https://codeclimate.com/github/flowerett/apple_warranty_check/badges/gpa.svg)](https://codeclimate.com/github/flowerett/apple_warranty_check)
5
+
6
+ Simple tool to get warranty info for Apple devices by it's IMEI.
7
+
8
+ ## Installation
9
+
10
+ Add this line to your application's Gemfile:
11
+
12
+ ```ruby
13
+ gem 'apple_warranty_check'
14
+ ```
15
+
16
+ And then execute:
17
+
18
+ $ bundle
19
+
20
+ Or install it yourself as:
21
+
22
+ $ gem install apple_warranty_check
23
+
24
+ ## Usage
25
+
26
+ ```ruby
27
+ require 'apple_warranty_check'
28
+ AppleWarrantyCheck::Process.new('IMEI').run
29
+ ```
30
+
31
+ ## Development
32
+
33
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake rspec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
34
+
35
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
36
+
37
+ ## Contributing
38
+
39
+ Bug reports and pull requests are welcome on GitHub at https://github.com/flowerett/apple_warranty_check.
40
+
41
+
42
+ ## License
43
+
44
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
45
+
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,24 @@
1
+ lib = File.expand_path('../lib', __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require 'apple_warranty_check/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "apple_warranty_check"
7
+ spec.version = AppleWarrantyCheck::VERSION
8
+ spec.authors = ["Nick Chernyshev"]
9
+ spec.email = ["nick.chernyshev@gmail.com"]
10
+
11
+ spec.summary = 'Check Apple warranty by IMEI'
12
+ spec.description = 'Tool to parse warranty info for Apple devices from official site.'
13
+ spec.homepage = "https://github.com/flowerett/apple_warranty_check"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.10"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ spec.add_development_dependency "rspec"
24
+ end
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ lib = File.expand_path(File.dirname(__FILE__) + '/../lib')
4
+ $LOAD_PATH.unshift(lib) if File.directory?(lib) && !$LOAD_PATH.include?(lib)
5
+
6
+ require 'apple_warranty_check'
7
+ require 'json'
8
+
9
+ args = ARGV.dup
10
+ ARGV.clear
11
+
12
+ data = AppleWarrantyCheck::Process.new(args).run
13
+
14
+ puts JSON.pretty_generate data
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "apple_warranty_check"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,82 @@
1
+ require 'net/http'
2
+
3
+ module AppleWarrantyCheck
4
+ class Process
5
+ CHECK_URL = 'https://selfsolve.apple.com/wcResults.do'.freeze
6
+
7
+ STR_ARG = "'(.*)'".freeze
8
+ BOOL_ARG = "(true|false)".freeze
9
+
10
+ PRODUCT_INFO_REGEXP = /warrantyPage.warrantycheck.displayProductInfo\((.*)\)/.freeze
11
+ PH_SUPPORT_INFO_REGEXP = /warrantyPage.warrantycheck.displayPHSupportInfo\(\s*#{BOOL_ARG},\s*#{STR_ARG},\s*#{STR_ARG},\s*#{STR_ARG}/
12
+ HW_SUPPORT_INFO_REGEXP = /warrantyPage.warrantycheck.displayHWSupportInfo\(\s*#{BOOL_ARG},\s*#{STR_ARG},\s*#{STR_ARG},\s*#{STR_ARG},\s*#{STR_ARG}/.freeze
13
+
14
+ PH_SUPPORT_STATUS = /Telephone Technical Support: (Active|Expired)/.freeze
15
+ HW_SUPPORT_STATUS = /Repairs and Service Coverage: (Active|Expired)/.freeze
16
+ EXP_DATE = /Estimated Expiration Date: (.*\s\d{2},\s\d{4})/.freeze
17
+
18
+ ERR_RESP_REGEXP = /var errorMsg = '(.*)';/
19
+
20
+ PRODUCT_INFO_KEYS = %i(prodImgUrl prodDesc isIMEINum APIMEINum isProdId prodId sn).freeze
21
+ PH_SUPPORT_INFO_KEYS = %i(hasPhoneSuppCov phoneSuppSubHeader phoneSuppCovTxt phoneSuppLink).freeze
22
+ HW_SUPPORT_INFO_KEYS = %i(hasHWSuppCov HWRepairSubHead HWSuppCovTxt HWSuppLnk hasCLMessageCode).freeze
23
+
24
+ attr_accessor :imei
25
+
26
+ def initialize(imei=nil)
27
+ @imei = imei
28
+ end
29
+
30
+ def run
31
+ parse_body get_response.body
32
+ end
33
+
34
+ def parse_body(html)
35
+ if ERR_RESP_REGEXP.match(html).nil?
36
+ {
37
+ product_info: get_product_info(html),
38
+ phone_support: get_phone_support_info(html),
39
+ hw_support: get_hw_support_info(html)
40
+ }
41
+ else
42
+ { error: ERR_RESP_REGEXP.match(html)[1] }
43
+ end
44
+ end
45
+
46
+ def get_phone_support_info(html)
47
+ get_support_info PH_SUPPORT_INFO_REGEXP, PH_SUPPORT_INFO_KEYS, PH_SUPPORT_STATUS, html
48
+ end
49
+
50
+ def get_hw_support_info(html)
51
+ get_support_info HW_SUPPORT_INFO_REGEXP, HW_SUPPORT_INFO_KEYS, HW_SUPPORT_STATUS, html
52
+ end
53
+
54
+ def get_product_info(html)
55
+ [
56
+ PRODUCT_INFO_KEYS,
57
+ PRODUCT_INFO_REGEXP.match(html)[1].split(',').map{ |el| el.strip.gsub('\'', '') }
58
+ ].transpose.to_h
59
+ end
60
+
61
+ private
62
+
63
+ def get_response
64
+ uri = URI(CHECK_URL)
65
+ params = {sn: imei, Continue: 'Continue', cn: '', local: '', caller: '', num: 0 }
66
+
67
+ uri.query = URI.encode_www_form(params)
68
+
69
+ Net::HTTP.get_response(uri)
70
+ end
71
+
72
+ def get_support_info(regexp, keys, status_regexp, html)
73
+ match_data = regexp.match(html)
74
+
75
+ keys.map.with_index{ |el, i| [el, match_data[i+1]] }.to_h.
76
+ merge!(
77
+ support_status: status_regexp.match(match_data[2])[1],
78
+ expiration_date: (EXP_DATE.match(match_data[3])[1] rescue nil)
79
+ )
80
+ end
81
+ end
82
+ end
@@ -0,0 +1,3 @@
1
+ module AppleWarrantyCheck
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,3 @@
1
+ require 'apple_warranty_check/version'
2
+
3
+ require 'apple_warranty_check/process'
@@ -0,0 +1,98 @@
1
+ require 'spec_helper'
2
+
3
+ describe AppleWarrantyCheck do
4
+ it 'has a version number' do
5
+ expect(AppleWarrantyCheck::VERSION).to eq '0.0.1'
6
+ end
7
+
8
+ let(:valid_warranty_html) { File.open('spec/files/valid_warranty_resp.html').read }
9
+ let(:expired_warranty_html) { File.open('spec/files/expired_warranty_resp.html').read }
10
+ let(:invalid_imei_html) { File.open('spec/files/invalid_sn_resp.html').read }
11
+
12
+ let(:pr) { AppleWarrantyCheck::Process.new() }
13
+
14
+
15
+ describe '#parse_body' do
16
+ it 'returns error message from invalid html response' do
17
+ info = pr.parse_body invalid_imei_html
18
+
19
+ expect(info[:error]).to match(/serial number you have provided cannot be found/)
20
+ end
21
+
22
+ it 'returns info hash for valid html response' do
23
+ info = pr.parse_body valid_warranty_html
24
+
25
+ expect(info.keys).to eq %i(product_info phone_support hw_support)
26
+ expect(info[:product_info][:APIMEINum]).to eq "013977000323877"
27
+ expect(info[:hw_support][:support_status]).to eq 'Active'
28
+ expect(info[:hw_support][:expiration_date]).to eq "August 10, 2016"
29
+ end
30
+
31
+ it 'returns info hash for expired html response' do
32
+ info = pr.parse_body expired_warranty_html
33
+
34
+ expect(info.keys).to eq %i(product_info phone_support hw_support)
35
+ expect(info[:product_info][:APIMEINum]).to eq "013896000639712"
36
+ expect(info[:hw_support][:support_status]).to eq 'Expired'
37
+ expect(info[:hw_support][:expiration_date]).to be_nil
38
+ end
39
+ end
40
+
41
+ describe '#get_product_info' do
42
+ it 'returns device data from valid html response' do
43
+ info = pr.get_product_info valid_warranty_html
44
+
45
+ expect(info).to be_a Hash
46
+ expect(info[:APIMEINum]).to eq "013977000323877"
47
+ expect(info[:prodDesc]).to eq "iPhone 5c"
48
+ end
49
+
50
+ it 'returns device data from expired html response' do
51
+ info = pr.get_product_info expired_warranty_html
52
+
53
+ expect(info).to be_a Hash
54
+ expect(info[:APIMEINum]).to eq "013896000639712"
55
+ expect(info[:prodDesc]).to eq "iPhone 5c"
56
+ end
57
+ end
58
+
59
+ describe '#get_phone_support_info' do
60
+ it 'returns phone support data from valid html response' do
61
+ info = pr.get_phone_support_info valid_warranty_html
62
+
63
+ expect(info).to be_a Hash
64
+ expect(info[:hasPhoneSuppCov]).to eq 'true'
65
+ expect(info[:support_status]).to eq "Active"
66
+ expect(info[:expiration_date]).to eq "August 10, 2016"
67
+ end
68
+
69
+ it 'returns phone support data from expired html response' do
70
+ info = pr.get_phone_support_info expired_warranty_html
71
+
72
+ expect(info).to be_a Hash
73
+ expect(info[:hasPhoneSuppCov]).to eq 'false'
74
+ expect(info[:support_status]).to eq "Expired"
75
+ expect(info[:expiration_date]).to be_nil
76
+ end
77
+ end
78
+
79
+ describe '#get_hw_support_info' do
80
+ it 'returns hardware support data from valid html response' do
81
+ info = pr.get_hw_support_info valid_warranty_html
82
+
83
+ expect(info).to be_a Hash
84
+ expect(info[:hasHWSuppCov]).to eq 'true'
85
+ expect(info[:support_status]).to eq "Active"
86
+ expect(info[:expiration_date]).to eq "August 10, 2016"
87
+ end
88
+
89
+ it 'returns hardware support data from expired html response' do
90
+ info = pr.get_hw_support_info expired_warranty_html
91
+
92
+ expect(info).to be_a Hash
93
+ expect(info[:hasHWSuppCov]).to eq 'false'
94
+ expect(info[:support_status]).to eq "Expired"
95
+ expect(info[:expiration_date]).to be_nil
96
+ end
97
+ end
98
+ end