wurfl-lite 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,5 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
5
+ vendor/*
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in wurfl-lite.gemspec
4
+ gemspec
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
@@ -0,0 +1,3 @@
1
+ class WURFL
2
+ VERSION = "0.0.1"
3
+ end
data/lib/wurfl-lite.rb ADDED
@@ -0,0 +1,65 @@
1
+ require 'open-uri'
2
+ require 'zlib'
3
+ require 'hpricot'
4
+ require 'amatch'
5
+
6
+ class WURFL
7
+
8
+ class Hash < ::Hash
9
+ def method_missing( method )
10
+ has_key?( method ) ? self[ method ] : nil
11
+ end
12
+ end
13
+
14
+ def initialize( filename = 'http://downloads.sourceforge.net/project/wurfl/WURFL/latest/wurfl-latest.xml.gz' )
15
+
16
+ file = open( filename )
17
+ stream = begin
18
+ Zlib::GzipReader.new( file )
19
+ rescue Zlib::GzipFile::Error
20
+ file
21
+ end
22
+ data = stream.read
23
+ doc = Hpricot( data )
24
+
25
+ @devices = Hash.new
26
+ devices_by_id = {}
27
+
28
+ (doc/'devices'/'device').each do |device_element|
29
+ device = Hash.new
30
+ %w|id user_agent fall_back|.each do |attribute|
31
+ device[ attribute.to_sym ] = device_element.attributes[ attribute ]
32
+ end
33
+ (device_element/'capability').each do |capability|
34
+ name = capability.attributes[ 'name' ].to_sym
35
+ value = capability.attributes[ 'value' ]
36
+ next if value.empty?
37
+ if value.to_i.to_s == value.strip
38
+ value = value.to_i
39
+ elsif value.strip.downcase =~ /^(true|false)$/
40
+ value = ( value.strip.downcase == 'true' )
41
+ end
42
+ device[ name ] = value
43
+ end
44
+
45
+ @devices[ device[ :user_agent ] ] = device
46
+ devices_by_id[ device[ :id ] ] = device
47
+ end
48
+
49
+ @devices.keys.each do |key|
50
+ @devices[ key ][ :fall_back ] = devices_by_id[ @devices[ key ][ :fall_back ] ]
51
+ end
52
+
53
+ end
54
+
55
+ def []( user_agent )
56
+ device = @devices[ user_agent ]
57
+ return device if device
58
+ match = Amatch::Sellers.new( user_agent )
59
+ keys = @devices.keys
60
+ distances = match.match( keys )
61
+ use_key = keys.zip( distances ).sort{|a,b|a.last<=>b.last}.first.first
62
+ @devices[ user_agent ] = @devices[ use_key ]
63
+ end
64
+
65
+ end
@@ -0,0 +1,25 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "wurfl-lite/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "wurfl-lite"
7
+ s.version = WURFL::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Clive Crous"]
10
+ s.email = ["clive@crous.co.za"]
11
+ s.homepage = "http://www.darkarts.co.za/wurfl-lite"
12
+ s.summary = %q{Simple Ruby usage of the WURFL device capabilities and features database}
13
+ s.description = %q{Simple Ruby usage of the WURFL device capabilities and features database}
14
+
15
+ s.add_dependency "hpricot", ">= 0.8.2", "!= 0.8.3"
16
+ s.add_dependency "amatch", ">= 0.2.5"
17
+
18
+ s.add_development_dependency "bundler", ">= 1.0.0"
19
+ s.add_development_dependency "rake", ">= 0.8.7"
20
+
21
+ s.files = `git ls-files`.split("\n")
22
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
23
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
24
+ s.require_paths = ["lib"]
25
+ end
metadata ADDED
@@ -0,0 +1,138 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: wurfl-lite
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 0
8
+ - 1
9
+ version: 0.0.1
10
+ platform: ruby
11
+ authors:
12
+ - Clive Crous
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2011-02-08 00:00:00 +02:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: hpricot
22
+ requirement: &id001 !ruby/object:Gem::Requirement
23
+ none: false
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ segments:
28
+ - 0
29
+ - 8
30
+ - 2
31
+ version: 0.8.2
32
+ - - "!="
33
+ - !ruby/object:Gem::Version
34
+ segments:
35
+ - 0
36
+ - 8
37
+ - 3
38
+ version: 0.8.3
39
+ type: :runtime
40
+ prerelease: false
41
+ version_requirements: *id001
42
+ - !ruby/object:Gem::Dependency
43
+ name: amatch
44
+ requirement: &id002 !ruby/object:Gem::Requirement
45
+ none: false
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ segments:
50
+ - 0
51
+ - 2
52
+ - 5
53
+ version: 0.2.5
54
+ type: :runtime
55
+ prerelease: false
56
+ version_requirements: *id002
57
+ - !ruby/object:Gem::Dependency
58
+ name: bundler
59
+ requirement: &id003 !ruby/object:Gem::Requirement
60
+ none: false
61
+ requirements:
62
+ - - ">="
63
+ - !ruby/object:Gem::Version
64
+ segments:
65
+ - 1
66
+ - 0
67
+ - 0
68
+ version: 1.0.0
69
+ type: :development
70
+ prerelease: false
71
+ version_requirements: *id003
72
+ - !ruby/object:Gem::Dependency
73
+ name: rake
74
+ requirement: &id004 !ruby/object:Gem::Requirement
75
+ none: false
76
+ requirements:
77
+ - - ">="
78
+ - !ruby/object:Gem::Version
79
+ segments:
80
+ - 0
81
+ - 8
82
+ - 7
83
+ version: 0.8.7
84
+ type: :development
85
+ prerelease: false
86
+ version_requirements: *id004
87
+ description: Simple Ruby usage of the WURFL device capabilities and features database
88
+ email:
89
+ - clive@crous.co.za
90
+ executables: []
91
+
92
+ extensions: []
93
+
94
+ extra_rdoc_files: []
95
+
96
+ files:
97
+ - .gitignore
98
+ - Gemfile
99
+ - Rakefile
100
+ - lib/wurfl-lite.rb
101
+ - lib/wurfl-lite/version.rb
102
+ - wurfl-lite.gemspec
103
+ has_rdoc: true
104
+ homepage: http://www.darkarts.co.za/wurfl-lite
105
+ licenses: []
106
+
107
+ post_install_message:
108
+ rdoc_options: []
109
+
110
+ require_paths:
111
+ - lib
112
+ required_ruby_version: !ruby/object:Gem::Requirement
113
+ none: false
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ hash: -366239857
118
+ segments:
119
+ - 0
120
+ version: "0"
121
+ required_rubygems_version: !ruby/object:Gem::Requirement
122
+ none: false
123
+ requirements:
124
+ - - ">="
125
+ - !ruby/object:Gem::Version
126
+ hash: -366239857
127
+ segments:
128
+ - 0
129
+ version: "0"
130
+ requirements: []
131
+
132
+ rubyforge_project:
133
+ rubygems_version: 1.3.7
134
+ signing_key:
135
+ specification_version: 3
136
+ summary: Simple Ruby usage of the WURFL device capabilities and features database
137
+ test_files: []
138
+