comcetera 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +5 -0
- data/.gitignore +22 -0
- data/LICENSE +20 -0
- data/README.rdoc +44 -0
- data/Rakefile +46 -0
- data/VERSION +1 -0
- data/lib/comcetera.rb +55 -0
- data/spec/comcetera_spec.rb +57 -0
- data/spec/spec_helper.rb +12 -0
- metadata +106 -0
data/.document
ADDED
data/.gitignore
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 Wes Oldenbeuving
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
= comcetera
|
2
|
+
|
3
|
+
First stab at a simple class that wraps the Comcetera mobile operator detection service. Give them a msisdn, they give you an operator code. Compile your own list of relevant codes based on wikipedia.
|
4
|
+
|
5
|
+
I'm scratching my own itch here, so I'm only adding what I use. Feedback and patches are welcome
|
6
|
+
|
7
|
+
== Example
|
8
|
+
|
9
|
+
The usual way you'd do a lookup
|
10
|
+
|
11
|
+
Comcetera.username = "meeeeee"
|
12
|
+
Comcetera.password = "verysecret"
|
13
|
+
comcetera = Comcetera.detect(31612345678)
|
14
|
+
comcetera.operator_code # => "T-Mobile"
|
15
|
+
comcetera.msisdn # => "31612345678"
|
16
|
+
|
17
|
+
When the lookup does not work due to a timeout
|
18
|
+
Comcetera.detect(31612345678) # => nil
|
19
|
+
|
20
|
+
When the lookup gives a result, but not what you'd expect
|
21
|
+
|
22
|
+
comcetera = Comcetera.detect(31612345678)
|
23
|
+
comcetera.error_code # => "ERR21"
|
24
|
+
comcetera.debug # => "QUERYOK\n31612345678 ERR21\nENDBATCH"
|
25
|
+
|
26
|
+
== Operator codes
|
27
|
+
|
28
|
+
Check the wikipedia page for Mobile Network Codes (http://en.wikipedia.org/wiki/Mobile_Network_Code) for a list of codes that you can expect.
|
29
|
+
|
30
|
+
Example: Wikipedia mentions T-Mobile in the Netherlands as: MCC 204, MNC 16. Comcetera would return this as code 20416. This logic seems consistent across countries.
|
31
|
+
|
32
|
+
== Note on Patches/Pull Requests
|
33
|
+
|
34
|
+
* Fork the project.
|
35
|
+
* Make your feature addition or bug fix.
|
36
|
+
* Add tests for it. This is important so I don't break it in a
|
37
|
+
future version unintentionally.
|
38
|
+
* Commit, do not mess with rakefile, version, or history.
|
39
|
+
(if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
|
40
|
+
* Send me a pull request. Bonus points for topic branches.
|
41
|
+
|
42
|
+
== Copyright
|
43
|
+
|
44
|
+
Copyright (c) 2010 Wes Oldenbeuving. See LICENSE for details.
|
data/Rakefile
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'jeweler'
|
6
|
+
Jeweler::Tasks.new do |gem|
|
7
|
+
gem.name = "comcetera"
|
8
|
+
gem.summary = "Wrapper class for Comcetera mobile operator detection based on msisdn."
|
9
|
+
gem.description = gem.summary
|
10
|
+
gem.email = "narnach@gmail.com"
|
11
|
+
gem.homepage = "http://github.com/Narnach/comcetera"
|
12
|
+
gem.authors = ["Wes Oldenbeuving"]
|
13
|
+
gem.add_development_dependency "rspec", ">= 1.2.9"
|
14
|
+
gem.add_development_dependency "fakeweb"
|
15
|
+
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
16
|
+
end
|
17
|
+
Jeweler::GemcutterTasks.new
|
18
|
+
rescue LoadError
|
19
|
+
puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
|
20
|
+
end
|
21
|
+
|
22
|
+
require 'spec/rake/spectask'
|
23
|
+
Spec::Rake::SpecTask.new(:spec) do |spec|
|
24
|
+
spec.libs << 'lib' << 'spec'
|
25
|
+
spec.spec_files = FileList['spec/**/*_spec.rb']
|
26
|
+
end
|
27
|
+
|
28
|
+
Spec::Rake::SpecTask.new(:rcov) do |spec|
|
29
|
+
spec.libs << 'lib' << 'spec'
|
30
|
+
spec.pattern = 'spec/**/*_spec.rb'
|
31
|
+
spec.rcov = true
|
32
|
+
end
|
33
|
+
|
34
|
+
task :spec => :check_dependencies
|
35
|
+
|
36
|
+
task :default => :spec
|
37
|
+
|
38
|
+
require 'rake/rdoctask'
|
39
|
+
Rake::RDocTask.new do |rdoc|
|
40
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
41
|
+
|
42
|
+
rdoc.rdoc_dir = 'rdoc'
|
43
|
+
rdoc.title = "comcetera #{version}"
|
44
|
+
rdoc.rdoc_files.include('README*')
|
45
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
46
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.2.0
|
data/lib/comcetera.rb
ADDED
@@ -0,0 +1,55 @@
|
|
1
|
+
require 'open-uri'
|
2
|
+
require 'timeout'
|
3
|
+
|
4
|
+
class Comcetera
|
5
|
+
attr_accessor :operator_code, :msisdn
|
6
|
+
attr_accessor :error_code, :debug
|
7
|
+
|
8
|
+
def initialize(attributes={})
|
9
|
+
attributes.each do |key, value|
|
10
|
+
self.send("#{key}=", value)
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
class << self
|
15
|
+
attr_accessor :username, :password, :timeout, :retries
|
16
|
+
|
17
|
+
def timeout
|
18
|
+
@timeout ||= 2
|
19
|
+
end
|
20
|
+
|
21
|
+
def retries
|
22
|
+
@retries ||= 2
|
23
|
+
end
|
24
|
+
|
25
|
+
def password
|
26
|
+
@password || raise("No password set for Comcetera")
|
27
|
+
end
|
28
|
+
|
29
|
+
def username
|
30
|
+
@username || raise("No username set for Comcetera")
|
31
|
+
end
|
32
|
+
|
33
|
+
# Attempt an operator lookup based on msisdn.
|
34
|
+
def detect(msisdn)
|
35
|
+
attempts = 0
|
36
|
+
while attempts <= self.retries
|
37
|
+
attempts += 1
|
38
|
+
begin
|
39
|
+
Timeout::timeout(self.timeout) do
|
40
|
+
body=open("http://api.comcetera.com/npl?user=#{self.username}&pass=#{self.password}&msisdn=#{msisdn}").read
|
41
|
+
msisdn, operator_code = body.split("\n")[1].split(" ") # 2nd line, last word is the operator hexcode
|
42
|
+
unless operator_code.to_s =~ /ERR\d+/
|
43
|
+
return new(:operator_code => operator_code, :msisdn => msisdn)
|
44
|
+
else
|
45
|
+
return new(:operator_code => nil, :msisdn => msisdn, :error_code=>operator_code, :debug=>body)
|
46
|
+
end
|
47
|
+
end
|
48
|
+
rescue Timeout::Error, SystemCallError => e
|
49
|
+
# ignore
|
50
|
+
end
|
51
|
+
end
|
52
|
+
nil
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
@@ -0,0 +1,57 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
|
+
|
3
|
+
FakeWeb.register_uri :get, 'http://api.comcetera.com/npl?user=username&pass=password&msisdn=31612345678', :body=> <<-MSG
|
4
|
+
QUERYOK
|
5
|
+
31612345678 12345
|
6
|
+
ENDBATCH
|
7
|
+
MSG
|
8
|
+
FakeWeb.register_uri :get, 'http://api.comcetera.com/npl?user=username&pass=password&msisdn=31612345621', :body=> <<-MSG
|
9
|
+
QUERYOK
|
10
|
+
31612345621 ERR21
|
11
|
+
ENDBATCH
|
12
|
+
MSG
|
13
|
+
|
14
|
+
describe "Comcetera" do
|
15
|
+
before(:all) do
|
16
|
+
Comcetera.username = "username"
|
17
|
+
Comcetera.password = "password"
|
18
|
+
end
|
19
|
+
|
20
|
+
describe "detect" do
|
21
|
+
it "should return a new Comcetera instance" do
|
22
|
+
@comcetera = Comcetera.detect(31612345678)
|
23
|
+
@comcetera.should be_instance_of(Comcetera)
|
24
|
+
end
|
25
|
+
|
26
|
+
it "should retry after a timeout and return nil when it still fails" do
|
27
|
+
3.times {Timeout.should_receive(:timeout).with(2).and_raise(Timeout::Error)}
|
28
|
+
@comcetera = Comcetera.detect(31612345678)
|
29
|
+
@comcetera.should be_nil
|
30
|
+
end
|
31
|
+
|
32
|
+
describe "the returned Comcetera instance" do
|
33
|
+
before(:each) do
|
34
|
+
@comcetera = Comcetera.detect(31612345678)
|
35
|
+
end
|
36
|
+
|
37
|
+
it "should contain the returned operator code" do
|
38
|
+
@comcetera.operator_code.should == "12345"
|
39
|
+
end
|
40
|
+
|
41
|
+
it "should contain the returned msisdn" do
|
42
|
+
@comcetera.msisdn.should == "31612345678"
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
it "should return an instance with error code and debug info when no operator code is returned" do
|
47
|
+
@comcetera = Comcetera.detect(31612345621)
|
48
|
+
@comcetera.operator_code.should be_nil
|
49
|
+
@comcetera.error_code.should == "ERR21"
|
50
|
+
@comcetera.debug.should == <<-MSG
|
51
|
+
QUERYOK
|
52
|
+
31612345621 ERR21
|
53
|
+
ENDBATCH
|
54
|
+
MSG
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
2
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
3
|
+
require 'comcetera'
|
4
|
+
require 'spec'
|
5
|
+
require 'spec/autorun'
|
6
|
+
require 'fakeweb'
|
7
|
+
|
8
|
+
Spec::Runner.configure do |config|
|
9
|
+
config.before do
|
10
|
+
FakeWeb.allow_net_connect = false
|
11
|
+
end
|
12
|
+
end
|
metadata
ADDED
@@ -0,0 +1,106 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: comcetera
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 23
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 2
|
9
|
+
- 0
|
10
|
+
version: 0.2.0
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Wes Oldenbeuving
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2010-08-09 00:00:00 +02:00
|
19
|
+
default_executable:
|
20
|
+
dependencies:
|
21
|
+
- !ruby/object:Gem::Dependency
|
22
|
+
name: rspec
|
23
|
+
prerelease: false
|
24
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ">="
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
hash: 13
|
30
|
+
segments:
|
31
|
+
- 1
|
32
|
+
- 2
|
33
|
+
- 9
|
34
|
+
version: 1.2.9
|
35
|
+
type: :development
|
36
|
+
version_requirements: *id001
|
37
|
+
- !ruby/object:Gem::Dependency
|
38
|
+
name: fakeweb
|
39
|
+
prerelease: false
|
40
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ">="
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
hash: 3
|
46
|
+
segments:
|
47
|
+
- 0
|
48
|
+
version: "0"
|
49
|
+
type: :development
|
50
|
+
version_requirements: *id002
|
51
|
+
description: Wrapper class for Comcetera mobile operator detection based on msisdn.
|
52
|
+
email: narnach@gmail.com
|
53
|
+
executables: []
|
54
|
+
|
55
|
+
extensions: []
|
56
|
+
|
57
|
+
extra_rdoc_files:
|
58
|
+
- LICENSE
|
59
|
+
- README.rdoc
|
60
|
+
files:
|
61
|
+
- .document
|
62
|
+
- .gitignore
|
63
|
+
- LICENSE
|
64
|
+
- README.rdoc
|
65
|
+
- Rakefile
|
66
|
+
- VERSION
|
67
|
+
- lib/comcetera.rb
|
68
|
+
- spec/comcetera_spec.rb
|
69
|
+
- spec/spec_helper.rb
|
70
|
+
has_rdoc: true
|
71
|
+
homepage: http://github.com/Narnach/comcetera
|
72
|
+
licenses: []
|
73
|
+
|
74
|
+
post_install_message:
|
75
|
+
rdoc_options:
|
76
|
+
- --charset=UTF-8
|
77
|
+
require_paths:
|
78
|
+
- lib
|
79
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
80
|
+
none: false
|
81
|
+
requirements:
|
82
|
+
- - ">="
|
83
|
+
- !ruby/object:Gem::Version
|
84
|
+
hash: 3
|
85
|
+
segments:
|
86
|
+
- 0
|
87
|
+
version: "0"
|
88
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
89
|
+
none: false
|
90
|
+
requirements:
|
91
|
+
- - ">="
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
hash: 3
|
94
|
+
segments:
|
95
|
+
- 0
|
96
|
+
version: "0"
|
97
|
+
requirements: []
|
98
|
+
|
99
|
+
rubyforge_project:
|
100
|
+
rubygems_version: 1.3.7
|
101
|
+
signing_key:
|
102
|
+
specification_version: 3
|
103
|
+
summary: Wrapper class for Comcetera mobile operator detection based on msisdn.
|
104
|
+
test_files:
|
105
|
+
- spec/comcetera_spec.rb
|
106
|
+
- spec/spec_helper.rb
|