transfer_to 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 2e00bcf3929974ec3401ec726a90aac36d75b06a
4
+ data.tar.gz: 926d7948ed5fdb4818bd23019f07c66a84a2ce5f
5
+ SHA512:
6
+ metadata.gz: 2e0a876c2acb854f3cdcee54db5d6bf33f00ab4f59a962ae347ae74db4b53288181f0196968558db651f739aa4b52f2372389c63b214200265f2bbac31a90726
7
+ data.tar.gz: a563d0072d5cb4332e47290228468fb16c169113452188b8306ba3f4f87921b3cd8b7740af4eafd89dbec0188da353744866d8b847e88deae3287d9d3b767d84
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in transfer_to.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Nikhil Gupta
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # TransferTo
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'transfer_to'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install transfer_to
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,12 @@
1
+ require "pry"
2
+ require "faraday"
3
+ require "net/http"
4
+
5
+ # require transfer_to files..
6
+ require "transfer_to/version"
7
+ require "transfer_to/errors"
8
+ require "transfer_to/request"
9
+ require "transfer_to/reply"
10
+ require "transfer_to/base"
11
+ require "transfer_to/api"
12
+ require "transfer_to/dsl"
@@ -0,0 +1,175 @@
1
+ module TransferTo
2
+ class API < Base
3
+
4
+ # This method can be used when you want to test the connection and your
5
+ # account credentials.
6
+ def ping
7
+ run_action :ping
8
+ end
9
+
10
+ # This function is used to retrieve the credit balance in your TransferTo
11
+ # account.
12
+ def check_wallet
13
+ run_action :check_wallet
14
+ end
15
+
16
+ # This method is used to recharge a destination number with a specified
17
+ # denomination (“product” field).
18
+ # This is the API’s most important action as it is required when sending
19
+ # a topup to a prepaid account phone numberin a live! environment.
20
+ #
21
+ # parameters
22
+ # ==========
23
+ # msisdn
24
+ # ------
25
+ # The international phone number of the user requesting to credit
26
+ # a TransferToAPI phone number. The format must contain the country code,
27
+ # and will be valid with or without the ‘+’ or ‘00’ placed before it. For
28
+ # example: “6012345678” or “+6012345678” or “006012345678” (Malaysia) are
29
+ # all valid.
30
+ #
31
+ # product
32
+ # -------
33
+ # This field is used to define the remote product(often, the same as the
34
+ # amount in destination currency) to use in the request.
35
+ #
36
+ # destination
37
+ # -----------
38
+ # This is the destination phone number that will be credited with the
39
+ # amount transferred. Format is similar to “msisdn”.
40
+ #
41
+ # operator_id
42
+ # -----------
43
+ # It defines the operator id of the destination MSISDN that must be used
44
+ # when treating the request. If set, the platform will be forced to use
45
+ # this operatorID and will not identify the operator of the destination
46
+ # MSISDN based on the numbering plan. It must be very useful in case of
47
+ # countries with number portability if you are able to know the destination
48
+ # operator.
49
+
50
+ def topup(msisdn, destination, product, reserved_id = nil,
51
+ recipient_sms = nil, sender_sms = nil, operator_id = nil)
52
+ @params = { msisdn: msisdn, destination_msisdn: destination, product: product }
53
+ self.oid = operator_id
54
+
55
+ @params.merge({
56
+ cid1: "", cid2: "", cid3: "",
57
+ reserved_id: reserved_id,
58
+ sender_sms: (sender_sms ? "yes" : "no"),
59
+ sms: recipient_sms,
60
+ sender_text: sender_sms,
61
+ delivered_amount_info: "1",
62
+ return_service_fee: "1",
63
+ return_timestamp: "1",
64
+ return_version: "1"
65
+ })
66
+
67
+ run_action :topup
68
+ end
69
+
70
+ # This method is used to retrieve various information of a specific MSISDN
71
+ # (operator, country…) as well as the list of all products configured for
72
+ # your specific account and the destination operator of the MSISDN.
73
+ def msisdn_info(msisdn, operator_id=nil)
74
+ @params = {
75
+ destination_msisdn: msisdn,
76
+ delivered_amount_info: "1",
77
+ return_service_fee: 1
78
+ }
79
+ self.oid = operator_id
80
+ run_action :msisdn_info
81
+ end
82
+
83
+ # This method can be used to retrieve available information on a specific
84
+ # transaction. Please note that values of “input_value” and
85
+ # “debit_amount_validated” are rounded to 2 digits after the comma but are
86
+ # the same as the values returned in the fields “input_value” and
87
+ # “validated_input_value” of the “topup” method response.
88
+ def trans_info(id)
89
+ @params = { transactionid: id }
90
+ run_action :trans_info
91
+ end
92
+
93
+ # This method is used to retrieve the list of transactions performed within
94
+ # the date range by the MSISDN if set. Note that both dates are included
95
+ # during the search.
96
+ #
97
+ # parameters
98
+ # ==========
99
+ # msisdn
100
+ # ------
101
+ # The format must be international with or without the ‘+’ or ‘00’:
102
+ # “6012345678” or “+6012345678” or “006012345678” (Malaysia)
103
+ #
104
+ # destination_msisdn
105
+ # ------------------
106
+ # The format must be international with or without the ‘+’ or ‘00’:
107
+ # “6012345678” or “+6012345678” or “006012345678” (Malaysia)
108
+ #
109
+ # code
110
+ # ----
111
+ # The error_code of the transactions to search for. E.g “0” to search for
112
+ # only all successful transactions. If left empty, all transactions will be
113
+ # returned(Failed and successful).
114
+ #
115
+ # start_date
116
+ # ----------
117
+ # Defines the start date of the search. Format must be YYYY-MM-DD.
118
+ #
119
+ # stop_date
120
+ # ---------
121
+ # Defines the end date of the search (included). Format must be YYYY-MM-DD.
122
+
123
+ def trans_list(start, stop, msisdn = nil, destination = nil, code = nil)
124
+ @params[:code] = code unless code
125
+ @params[:msisdn] = msisdn unless msisdn
126
+ @params[:stop_date] = to_yyyymmdd(stop)
127
+ @params[:start_date] = to_yyyymmdd(start)
128
+ @params[:destination_msisdn] = destination unless destination
129
+ run_action :trans_list
130
+ end
131
+
132
+ # This method is used to reserve an ID in the system. This ID can then be
133
+ # used in the “topup” or “simulation” requests.
134
+ # This way, your system knows the IDof the transaction before sending the
135
+ # request to TransferTo (else it will only be displayed in the response).
136
+ def reserve_id
137
+ run_action :reserve_id
138
+ end
139
+
140
+ # This method is used to retrieve the ID of a transaction previously
141
+ # performed based on the key used in the request at that time.
142
+ def get_id_from_key(key)
143
+ @params = { from_key: key }
144
+ run_action :get_id_from_key
145
+ end
146
+
147
+ # This method is used to retrieve coverage and pricelist offered to you.
148
+ # parameters
149
+ # ==========
150
+ # info_type
151
+ # ---------
152
+ # i) “countries”: Returns a list of all countries offered to you
153
+ # ii) “country” : Returns a list of operators in the country
154
+ # iii) “operator” : Returns a list of wholesale and retail price for the operator
155
+ #
156
+ # content
157
+ # -------
158
+ # i) Not used if info_type = “countries”
159
+ # ii) countryid of the requested country if info_type = “country”
160
+ # iii) operatorid of the requested operator if info_type = “operator”
161
+
162
+ def pricelist(info_type, content = nil)
163
+ @params[:info_type] = info_type
164
+ @params[:content] = content unless content
165
+ run_action :pricelist
166
+ end
167
+
168
+ private
169
+
170
+ def oid=(operator_id)
171
+ @params[:operatorid] = operator_id.to_i if operator_id.is_a?(Integer)
172
+ end
173
+
174
+ end
175
+ end
@@ -0,0 +1,31 @@
1
+ module TransferTo
2
+ class Base
3
+
4
+ attr_reader :reply, :request
5
+
6
+ def initialize(user, password)
7
+ aurl = "https://fm.transfer-to.com:5443"
8
+ @params = {}
9
+ @request = ::TransferTo::Request.new user, password, aurl
10
+ end
11
+
12
+ def run_action(name, method = :get)
13
+
14
+ @request.action = name
15
+ @request.params = @params
16
+
17
+ @request.run(method).on_complete do |reply|
18
+ @reply = ::TransferTo::Reply.new(reply)
19
+ raise ::TransferTo::Error.new @reply.error_code, @reply.error_message unless @reply.success?
20
+ return @reply
21
+ end
22
+ end
23
+
24
+ def test_numbers(num = nil)
25
+ numbers = [ "628123456710", "628123456770", "628123456780",
26
+ "628123456781", "628123456790", "628123456798",
27
+ "628123456799" ]
28
+ num > 0 && num < 8 ? numbers[num-1] : numbers
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,19 @@
1
+ module TransferTo
2
+ class DSL < API
3
+
4
+ # check the status of the TranferTo API
5
+ def check_status
6
+ self.ping
7
+ reply.success? && reply.message == "pong" && reply.auth_key == request.key
8
+ end
9
+
10
+ # get information about a phone number
11
+ def phone_search(number, operator_id = nil)
12
+ information = msisdn_info(number, operator_id).information
13
+ information[:product_list] = information[:product_list].split(",")
14
+ information
15
+ end
16
+
17
+ end
18
+ end
19
+
@@ -0,0 +1,9 @@
1
+ module TransferTo
2
+ class Error < StandardError
3
+ attr_reader :code
4
+ def initialize(code, message = nil)
5
+ @code = code.to_i
6
+ super(message)
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,75 @@
1
+ module TransferTo
2
+ class Reply
3
+
4
+ # "reply" as received from Faraday's request
5
+ def initialize(reply)
6
+ @response = reply.to_hash[:response]
7
+ end
8
+
9
+ def format_it
10
+ {
11
+ data: data,
12
+ status: status,
13
+ success: success?,
14
+ method: @response.env[:method],
15
+ url: url,
16
+ headers: headers,
17
+ raw_response: raw
18
+ }
19
+ end
20
+
21
+
22
+ ######## CONVENIENCE METHODS ##########
23
+
24
+ # get the actual data returned by the TransferTo API
25
+ def data
26
+ hash = {}
27
+ @response.body.lines.each do |line|
28
+ key, value = line.strip.split "="
29
+ hash[key.to_sym] = (key == "error_code") ? value.to_i : value
30
+ end; hash
31
+ end
32
+
33
+ def status
34
+ @response.status
35
+ end
36
+
37
+ def error_code
38
+ data[:error_code]
39
+ end
40
+
41
+ def error_message
42
+ data[:error_txt]
43
+ end
44
+
45
+ def success?
46
+ status == 200 && error_code == 0
47
+ end
48
+
49
+ def url
50
+ @response.env[:url].to_s
51
+ end
52
+
53
+ def information
54
+ data.reject do |key, value|
55
+ [:authentication_key, :error_code, :error_txt].include?(key)
56
+ end
57
+ end
58
+
59
+ def message
60
+ information[:info_txt]
61
+ end
62
+
63
+ def auth_key
64
+ data[:authentication_key]
65
+ end
66
+
67
+ def headers
68
+ @response.headers
69
+ end
70
+
71
+ def raw
72
+ @response.body
73
+ end
74
+ end
75
+ end
@@ -0,0 +1,82 @@
1
+ module TransferTo
2
+ class Request
3
+
4
+ attr_reader :user, :name, :params
5
+
6
+ def initialize(user, password, aurl = nil)
7
+ @user = user
8
+ @pass = password
9
+ @conn = Faraday.new(url: aurl) do |faraday|
10
+ faraday.request :url_encoded
11
+ faraday.adapter :net_http
12
+ end
13
+ end
14
+
15
+ def reset
16
+ @params = {}
17
+ authenticate
18
+ end
19
+
20
+ def authenticate
21
+ time = Time.now.to_i.to_s
22
+ add_param :key, time
23
+ add_param :md5, md5_hash(@user + @pass + time.to_s)
24
+ add_param :login, @user
25
+ end
26
+
27
+ def action=(name)
28
+ reset
29
+ @name = name
30
+ add_param :action, name
31
+ end
32
+
33
+ def params=(parameters)
34
+ @params.merge!(parameters)
35
+ end
36
+
37
+ def add_param(key, value)
38
+ @params[key.to_sym] = value
39
+ end
40
+
41
+ def key
42
+ @params[:key]
43
+ end
44
+
45
+ def get?
46
+ @params[:method] == :get
47
+ end
48
+
49
+ def post?
50
+ @params[:method] == :post
51
+ end
52
+
53
+ def run(method = :get)
54
+ add_param :method, method
55
+ @conn.send(method, "/cgi-bin/shop/topup", @params) do |req|
56
+ req.options = { timeout: 600, open_timeout: 600 }
57
+ end
58
+ end
59
+
60
+ private
61
+
62
+ def md5_hash(str)
63
+ (Digest::MD5.new << str).to_s
64
+ end
65
+
66
+ def to_time(time)
67
+ case time.class.name
68
+ when "String" then return Time.parse(time)
69
+ when "Integer" then return Time.at(time)
70
+ when "Time" then return time
71
+ else raise ArgumentError
72
+ end
73
+ rescue
74
+ Time.now
75
+ end
76
+
77
+ def to_yyyymmdd(time)
78
+ to_time(time).strftime("%Y-%m-%d")
79
+ end
80
+
81
+ end
82
+ end
@@ -0,0 +1,3 @@
1
+ module TransferTo
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'transfer_to/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "transfer_to"
8
+ spec.version = TransferTo::VERSION
9
+ spec.authors = ["Nikhil Gupta"]
10
+ spec.email = ["me@nikhgupta.com"]
11
+ spec.description = %q{Consumes TransferTo.com API and provides with ruby methods for the same}
12
+ spec.summary = %q{Gem to consume TransferTo.com API}
13
+ spec.homepage = "http://rubygems.org/gems/transfer_to"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+
24
+ spec.add_dependency "faraday"
25
+
26
+ spec.add_development_dependency "pry"
27
+ end
metadata ADDED
@@ -0,0 +1,114 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: transfer_to
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Nikhil Gupta
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-08-18 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: faraday
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: pry
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: Consumes TransferTo.com API and provides with ruby methods for the same
70
+ email:
71
+ - me@nikhgupta.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - .gitignore
77
+ - Gemfile
78
+ - LICENSE.txt
79
+ - README.md
80
+ - Rakefile
81
+ - lib/transfer_to.rb
82
+ - lib/transfer_to/api.rb
83
+ - lib/transfer_to/base.rb
84
+ - lib/transfer_to/dsl.rb
85
+ - lib/transfer_to/errors.rb
86
+ - lib/transfer_to/reply.rb
87
+ - lib/transfer_to/request.rb
88
+ - lib/transfer_to/version.rb
89
+ - transfer_to.gemspec
90
+ homepage: http://rubygems.org/gems/transfer_to
91
+ licenses:
92
+ - MIT
93
+ metadata: {}
94
+ post_install_message:
95
+ rdoc_options: []
96
+ require_paths:
97
+ - lib
98
+ required_ruby_version: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - '>='
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ required_rubygems_version: !ruby/object:Gem::Requirement
104
+ requirements:
105
+ - - '>='
106
+ - !ruby/object:Gem::Version
107
+ version: '0'
108
+ requirements: []
109
+ rubyforge_project:
110
+ rubygems_version: 2.0.0
111
+ signing_key:
112
+ specification_version: 4
113
+ summary: Gem to consume TransferTo.com API
114
+ test_files: []