diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..4b7b717 --- /dev/null +++ b/.gitignore @@ -0,0 +1,52 @@ + +*.gem +*.rbc +/.config +/coverage/ +/InstalledFiles +/pkg/ +/spec/reports/ +/spec/examples.txt +/test/tmp/ +/test/version_tmp/ +/tmp/ + +# Used by dotenv library to load environment variables. +# .env + +## Specific to RubyMotion: +.dat* +.repl_history +build/ +*.bridgesupport +build-iPhoneOS/ +build-iPhoneSimulator/ + +## Specific to RubyMotion (use of CocoaPods): +# +# We recommend against adding the Pods directory to your .gitignore. However +# you should judge for yourself, the pros and cons are mentioned at: +# https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control +# +# vendor/Pods/ + +## Documentation cache and generated files: +/.yardoc/ +/_yardoc/ +/doc/ +/rdoc/ + +## Environment normalization: +/.bundle/ +/vendor/bundle +/lib/bundler/man/ + +# for a library or gem, you might want to ignore these files since the code is +# intended to run in multiple environments; otherwise, check them in: +Gemfile.lock +.ruby-version +.ruby-gemset + +# unless supporting rvm < 1.11.0 or doing something fancy, ignore this: +.rvmrc +.idea diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..551cdb6 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,6 @@ +language: ruby +rvm: + - 2.2 + - jruby + - rbx-2 +script: bundle exec rspec diff --git a/README.md b/README.md index e69de29..cd81aef 100644 --- a/README.md +++ b/README.md @@ -0,0 +1,55 @@ +# Android::Devices + +Gem used to convert android model numbers (e.g. GT-I9507) into something more human readable (e.g. Samsung Galaxy S4) + +## Installation + +Add the following into your application's Gemfile: + +`gem android-devices` + +Then execute: + +`bundle install` + + +## Usage + +This gem uses the devices list provided by the Google Play store as it's information store. When you first run the gem it +will attempt to automatically download the list, if you wish to re-download the list you can use the following command: + +` Android::Devices.update_devices` + +To then search for a device, you can do something similar to the following: + +``` +device = Android::Devices.search_by_model('GT-I9507') +=> # + +device.name +=> "Samsung Galaxy S4" + +device.brand +=> "Galaxy S4" + +``` + +If a brand name isn't available it will make use the manufacturer and model as the name: + +``` +device = Android::Devices.search_by_model('GT-S5820') +=> # + +device.name +=> "Samsung GT-S5820" +``` + +Finally, if you want a list of devices by a certain manufacturer you can: + +``` +devices = Android::Devices.search_by_manufacturer('Samsung') +=> [#, + #, + #, + etc. +``` \ No newline at end of file diff --git a/Rakefile b/Rakefile index e69de29..2995527 100644 --- a/Rakefile +++ b/Rakefile @@ -0,0 +1 @@ +require "bundler/gem_tasks" diff --git a/android-devices.gemspec b/android-devices.gemspec index c6c10f2..0d1b661 100644 --- a/android-devices.gemspec +++ b/android-devices.gemspec @@ -9,4 +9,13 @@ Gem::Specification.new do |spec| spec.version = Android::Devices::VERSION spec.authors = ['Jon Wilson'] spec.email = ['jon.wilson01@bbc.co.uk'] + spec.summary = %q{Human readable Android device names} + + spec.files = `git ls-files -z`.split("\x0") + spec.require_paths = ['lib'] + + spec.add_development_dependency 'bundler' + spec.add_development_dependency 'rake' + spec.add_development_dependency 'pry' + spec.add_development_dependency 'rspec' end diff --git a/lib/android/devices.rb b/lib/android/devices.rb index e69de29..ab7cb21 100644 --- a/lib/android/devices.rb +++ b/lib/android/devices.rb @@ -0,0 +1,52 @@ +require 'android/devices/version' +require 'android/devices/model' +require 'csv' + +module Android + module Devices + + def self.update_devices(url = '') + csv_url = url.nil? || url.empty? ? 'http://storage.googleapis.com/play_public/supported_devices.csv' : url + begin + devices = CSV.parse(open(csv_url).read) + File.open('devices.csv','w') {|f| f.write(devices.inject([]) { |csv,row| csv << CSV.generate_line(row) }.join('').encode('UTF-8'))} + return true + rescue Exception + raise 'Unable to update devices' + end + end + + def self.list_exists + return File.exists?('devices.csv') + end + + def self.old_list? + return (File.mtime('devices.csv') < Time.parse((DateTime.now - 30).to_s)) + end + + def self.search_by_model(model) + models.select { |device| device.model == model}.first + end + + def self.search_by_manufacturer(manufacturer) + models.select { |device| device.manufacturer == manufacturer} + end + + def self.models + return @models unless @models.nil? + @models = [] + devices.shift + devices.each do |device| + @models << Model.new(device[0], device[1], device[2], device[3]) + end + @models + end + + def self.devices + return @devices unless @devices.nil? + update_devices unless list_exists + @devices = CSV.read('devices.csv') + @devices + end + end +end diff --git a/lib/android/devices/model.rb b/lib/android/devices/model.rb new file mode 100644 index 0000000..b971127 --- /dev/null +++ b/lib/android/devices/model.rb @@ -0,0 +1,19 @@ +module Android + module Devices + class Model + attr_accessor :manufacturer, :brand, :device, :model + + def initialize(manufacturer, brand, device, model) + @manufacturer = manufacturer + @brand = brand + @device = device + @model = model + end + + def name + return "#{@manufacturer} #{@brand}" if @brand + "#{@manufacturer} #{@model}" + end + end + end +end \ No newline at end of file diff --git a/lib/android/devices/version.rb b/lib/android/devices/version.rb index 49a4ebf..53a3fca 100644 --- a/lib/android/devices/version.rb +++ b/lib/android/devices/version.rb @@ -1,5 +1,5 @@ module Android module Devices - VERSION = '0.0.1' + VERSION = '1.0.0' end end diff --git a/spec/android_devices_spec.rb b/spec/android_devices_spec.rb new file mode 100644 index 0000000..9650b03 --- /dev/null +++ b/spec/android_devices_spec.rb @@ -0,0 +1,36 @@ +describe Android::Devices do + describe 'check' do + + it 'should check if the devices list exists' do + File.delete('devices.csv') if File.exists? 'devices.csv' + expect(Android::Devices.list_exists).to be(false) + end + + it 'should warn if the devices list is over 30 days old' do + FileUtils.touch 'devices.csv', :mtime => Time.parse((DateTime.now - 31).to_s) + expect(Android::Devices.old_list?).to be(true) + end + end + + describe 'update' do + it 'should be able to update itself' do + expect(Android::Devices.update_devices('spec/test_devices.csv')).to be(true) + expect(Android::Devices.list_exists).to be(true) + expect(CSV.read('devices.csv').count).to be(4) + end + end + + describe 'search' do + before(:all) do + Android::Devices.update_devices('spec/test_devices.csv') + end + + it 'should search by model' do + expect(Android::Devices.search_by_model('GT-I9507').manufacturer).to eq('Samsung') + end + + it 'should search by manufacturer' do + expect(Android::Devices.search_by_manufacturer('Samsung').count).to be(3) + end + end +end \ No newline at end of file diff --git a/spec/test_devices.csv b/spec/test_devices.csv new file mode 100644 index 0000000..040e844 --- /dev/null +++ b/spec/test_devices.csv @@ -0,0 +1,4 @@ +Retail Branding,Marketing Name,Device,Model +Samsung,Galaxy S4,jftdd,GT-I9507 +Samsung,Galaxy S5,klte,SM-G900M +Samsung,Galaxy S7,herolteskt,SM-G930S \ No newline at end of file