Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Corrected some library paths and added Travis config #1

Merged
merged 12 commits into from
Oct 11, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -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
6 changes: 6 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
language: ruby
rvm:
- 2.2
- jruby
- rbx-2
script: bundle exec rspec
55 changes: 55 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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')
=> #<Android::Devices::Model:0x007fadd9582d28 @manufacturer="Samsung", @brand="Galaxy S4", @device="jftdd", @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')
=> #<Android::Devices::Model:0x007f93889a4700 @manufacturer="Samsung", @brand=nil, @device="GT-S5820", @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')
=> [#<Android::Devices::Model:0x007f9388864278 @manufacturer="Samsung", @brand="Galaxy Core Prime", @device="coreprimeltetfnvzw", @model="SM-S820L">,
#<Android::Devices::Model:0x007f9388864250 @manufacturer="Samsung", @brand="Galaxy Core Prime", @device="coreprimeltevzw", @model="SM-G360V">,
#<Android::Devices::Model:0x007f9388864228 @manufacturer="Samsung", @brand="Galaxy Core Prime", @device="coreprimeve3g", @model="SM-G361H">,
etc.
```
1 change: 1 addition & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
require "bundler/gem_tasks"
9 changes: 9 additions & 0 deletions android-devices.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -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
52 changes: 52 additions & 0 deletions lib/android/devices.rb
Original file line number Diff line number Diff line change
@@ -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
19 changes: 19 additions & 0 deletions lib/android/devices/model.rb
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion lib/android/devices/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module Android
module Devices
VERSION = '0.0.1'
VERSION = '1.0.0'
end
end
36 changes: 36 additions & 0 deletions spec/android_devices_spec.rb
Original file line number Diff line number Diff line change
@@ -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
4 changes: 4 additions & 0 deletions spec/test_devices.csv
Original file line number Diff line number Diff line change
@@ -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