data247 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/.gitignore ADDED
@@ -0,0 +1,22 @@
1
+ ## MAC OS
2
+ .DS_Store
3
+
4
+ ## TEXTMATE
5
+ *.tmproj
6
+ tmtags
7
+
8
+ ## EMACS
9
+ *~
10
+ \#*
11
+ .\#*
12
+
13
+ ## VIM
14
+ *.swp
15
+
16
+ ## PROJECT::GENERAL
17
+ coverage
18
+ rdoc
19
+ pkg
20
+
21
+ ## PROJECT::SPECIFIC
22
+ *.gemspec
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 Gerard de Brieder
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,43 @@
1
+ = Data247
2
+
3
+ A simple wrapper for the NumberPortabilityLookup service of Data24-7. Give them a msisdn and they (usually) give you an operator code. Compile your own list of relevant codes.
4
+
5
+ I'm scratching my own itch here, so I'm only adding what I use. Feedback and patches are welcome.
6
+
7
+ For more info about the service, take a look at: http://data24-7.com/
8
+
9
+ == Example
10
+
11
+ The usual way you'd do a lookup
12
+
13
+ Data247.username = "meeeeee"
14
+ Data247.password = "verysecret"
15
+ data247 = Data247.detect(31612345678)
16
+ data247.operator_code # => "20415"
17
+ data247.operator_name # => "T-Mobile Netherlands"
18
+ data247.msisdn # => "31612345678"
19
+
20
+ When the lookup does not work due to a timeout
21
+
22
+ Data247.detect(31612345678) # => nil
23
+
24
+ This means that the amount of retries specified as Data247.retries have all timed out after Data247.timeout seconds. You can retry at a later time or do something else. This is great if, as is my use case, the lookup is used as a type of pre-selection for numbers.
25
+
26
+ == Note on Patches/Pull Requests
27
+
28
+ * Fork the project.
29
+ * Make your feature addition or bug fix.
30
+ * Add tests for it. This is important so I don't break it in a
31
+ future version unintentionally.
32
+ * Commit, do not mess with rakefile, version, or history.
33
+ (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)
34
+ * Send me a pull request. Bonus points for topic branches.
35
+
36
+ == Contributors
37
+
38
+ * Gerard 'smeevil' de Brieder
39
+ * Wes 'Narnach' Oldenbeuving
40
+
41
+ == Copyright
42
+
43
+ Copyright (c) 2011 Gerard de Brieder. 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 = "data247"
8
+ gem.summary = "Wrapper class for Data24-7 mobile operator detection based on msisdn."
9
+ gem.description = gem.summary
10
+ gem.email = "smeevil@me.com"
11
+ gem.homepage = "http://github.com/smeevil/data247"
12
+ gem.authors = ["Gerard de Brieder"]
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 = "data24-7 #{version}"
44
+ rdoc.rdoc_files.include('README*')
45
+ rdoc.rdoc_files.include('lib/**/*.rb')
46
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.1
data/lib/data24_7.rb ADDED
@@ -0,0 +1,75 @@
1
+ require 'open-uri'
2
+ require 'timeout'
3
+ require 'active_support'
4
+
5
+ class Data247
6
+ attr_accessor :operator_name, :operator_code, :msisdn, :status
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 Data24-7")
27
+ end
28
+
29
+ def username
30
+ @username || raise("No username set for Data24-7")
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
+ data=Hash.from_xml(open("https://api.data24-7.com/carrier.php?username=#{self.username}&password=#{self.password}&p1=#{msisdn}").read)["response"]["results"]["result"]
41
+ status=data["status"]
42
+ operator_name=data["carrier_name"]
43
+ operator_code=data["carrier_id"]
44
+ unless status != "OK"
45
+ return new(:operator_name=> operator_name, :operator_code => operator_code, :msisdn => msisdn, :status=>status)
46
+ else
47
+ return new(:operator_code => nil, :msisdn => msisdn, :status=>status)
48
+ end
49
+ end
50
+ rescue Timeout::Error, SystemCallError => e
51
+ # ignore
52
+ end
53
+ end
54
+ new(:status=>"Timeout from Data24-7")
55
+ end
56
+
57
+ def setup_fakeweb_response(options={})
58
+ raise "FakeWeb is not defined. Please require 'fakeweb' and make sure the fakeweb rubygem is installed." unless defined?(FakeWeb)
59
+ raise ArgumentError.new("Option missing: :msisdn") unless options[:msisdn]
60
+ raise ArgumentError.new("Option missing: :status") unless options[:status]
61
+ options[:username]||= self.username
62
+ options[:password]||= self.password
63
+ FakeWeb.register_uri :get, "https://api.data24-7.com/carrier.php?username=#{options[:username]}&password=#{options[:password]}&p1=#{options[:msisdn]}", :body=> <<-MSG
64
+ <?xml version="1.0"?><response><results><result item="1"><status>#{options[:status]}</status><number>#{options[:msisdn]}</number><wless>y</wless><carrier_name>T-Mobile</carrier_name><carrier_id>#{options[:result]}</carrier_id><sms_address>#{options[:msisdn]}@tmomail.net</sms_address><mms_address>#{options[:msisdn]}@tmomail.net</mms_address></result></results><balance>21.5000</balance></response>
65
+ MSG
66
+ end
67
+ end
68
+
69
+ def ==(other)
70
+ [:operator_code, :msisdn, :status].each do |attribute|
71
+ return false unless self.send(attribute) == other.send(attribute)
72
+ end
73
+ true
74
+ end
75
+ end
@@ -0,0 +1,43 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe "Data247" do
4
+ before(:all) do
5
+ Data247.username = "username"
6
+ Data247.password = "password"
7
+ Data247.setup_fakeweb_response(:msisdn=>"31612345678", :status=>"OK", :result => 12345)
8
+ Data247.setup_fakeweb_response(:msisdn=>"31612345621", :status => "ERROR")
9
+ end
10
+
11
+ describe "detect" do
12
+ it "should return a new Data247 instance" do
13
+ @data247 = Data247.detect(31612345678)
14
+ @data247.should be_instance_of(Data247)
15
+ end
16
+
17
+ it "should retry after a timeout and return nil when it still fails" do
18
+ 3.times {Timeout.should_receive(:timeout).with(2).and_raise(Timeout::Error)}
19
+ @data247 = Data247.detect(31612345678)
20
+ @data247.should == Data247.new(:status=>"Timeout from Data24-7")
21
+ end
22
+
23
+ describe "the returned Data247 instance" do
24
+ before(:each) do
25
+ @data247 = Data247.detect("31612345678")
26
+ end
27
+
28
+ it "should contain the returned operator code" do
29
+ @data247.operator_code.should == "12345"
30
+ end
31
+
32
+ it "should contain the returned msisdn" do
33
+ @data247.msisdn.should == "31612345678"
34
+ end
35
+ end
36
+
37
+ it "should return an instance with error code when no operator code is returned" do
38
+ @data247 = Data247.detect(31612345621)
39
+ @data247.operator_code.should be_nil
40
+ @data247.status.should == "ERROR"
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,12 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
+ require 'data24_7'
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: data247
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Gerard de Brieder
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-04-04 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 Data24-7 mobile operator detection based on msisdn.
52
+ email: smeevil@me.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/data24_7.rb
68
+ - spec/data24_7_spec.rb
69
+ - spec/spec_helper.rb
70
+ has_rdoc: true
71
+ homepage: http://github.com/smeevil/data247
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 Data24-7 mobile operator detection based on msisdn.
104
+ test_files:
105
+ - spec/data24_7_spec.rb
106
+ - spec/spec_helper.rb