instamojo-ruby 1.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/Rakefile ADDED
@@ -0,0 +1,44 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+ begin
6
+ Bundler.setup(:default, :development)
7
+ rescue Bundler::BundlerError => e
8
+ $stderr.puts e.message
9
+ $stderr.puts "Run `bundle install` to install missing gems"
10
+ exit e.status_code
11
+ end
12
+ require 'rake'
13
+
14
+ require 'jeweler'
15
+ Jeweler::Tasks.new do |gem|
16
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
17
+ gem.name = "instamojo-ruby"
18
+ gem.homepage = "http://github.com/Instamojo/Instamojo-rb"
19
+ gem.license = "MIT"
20
+ gem.summary = %Q{Instamojo Ruby library - Assists you to programmatically create, edit and delete offers on Instamojo}
21
+ gem.description = %Q{Instamojo Ruby library - Assists you to programmatically create, edit and delete offers on Instamojo. Also supports listing, updation and details of Payments, Payments Requests and Refunds.}
22
+ gem.email = "dev-accounts@instamojo.com"
23
+ gem.authors = ["Instamojo Technologies"]
24
+ # dependencies defined in Gemfile
25
+ end
26
+ Jeweler::RubygemsDotOrgTasks.new
27
+
28
+ require 'rspec/core'
29
+ require 'rspec/core/rake_task'
30
+ RSpec::Core::RakeTask.new(:spec) do |spec|
31
+ spec.pattern = FileList['spec/**/*_spec.rb']
32
+ end
33
+
34
+ task :default => :spec
35
+
36
+ require 'rdoc/task'
37
+ Rake::RDocTask.new do |rdoc|
38
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
39
+
40
+ rdoc.rdoc_dir = 'rdoc'
41
+ rdoc.title = "instamojo-ruby #{version}"
42
+ rdoc.rdoc_files.include('README*')
43
+ rdoc.rdoc_files.include('lib/**/*.rb')
44
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 1.1.2
@@ -0,0 +1,79 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+ # stub: Instamojo-rb 1.1.0 ruby lib
6
+
7
+ Gem::Specification.new do |s|
8
+ s.name = "instamojo-ruby"
9
+ s.version = "1.1.2"
10
+
11
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
12
+ s.require_paths = ["lib"]
13
+ s.authors = ["Instamojo Technologies"]
14
+ s.date = "2015-12-18"
15
+ s.description = "Instamojo Ruby library - Assists you to programmatically create, edit and delete offers on Instamojo. Also supports listing, updation and details of Payments, Payments Requests and Refunds."
16
+ s.email = "dev-accounts@instamojo.com"
17
+ s.extra_rdoc_files = [
18
+ "LICENSE.txt",
19
+ "README.md",
20
+ "README.rdoc"
21
+ ]
22
+ s.files = [
23
+ ".document",
24
+ ".rspec",
25
+ "Gemfile",
26
+ "Gemfile.lock",
27
+ "instamojo-ruby.gemspec",
28
+ "LICENSE.txt",
29
+ "README.md",
30
+ "README.rdoc",
31
+ "Rakefile",
32
+ "VERSION",
33
+ "lib/API/api.rb",
34
+ "lib/Instamojo-rb.rb",
35
+ "lib/base.rb",
36
+ "lib/client/client.rb",
37
+ "lib/client/link.rb",
38
+ "lib/client/payment.rb",
39
+ "lib/client/payment_request.rb",
40
+ "lib/client/refund.rb",
41
+ "lib/common_object.rb",
42
+ "lib/response.rb",
43
+ "lib/utility.rb",
44
+ "spec/Instamojo-rb_spec.rb",
45
+ "spec/spec_helper.rb"
46
+ ]
47
+ s.homepage = "http://github.com/Instamojo/Instamojo-rb"
48
+ s.licenses = ["MIT"]
49
+ s.rubygems_version = "2.2.2"
50
+ s.summary = "Instamojo Ruby library - Assists you to programmatically create, edit and delete offers on Instamojo"
51
+
52
+ if s.respond_to? :specification_version then
53
+ s.specification_version = 4
54
+
55
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
56
+ s.add_runtime_dependency(%q<faraday>, ["= 0.8.8"])
57
+ s.add_runtime_dependency(%q<rest-client>, ["~> 1.8"])
58
+ s.add_development_dependency(%q<rspec>, ["~> 2.8"])
59
+ s.add_development_dependency(%q<bundler>, ["~> 1.0"])
60
+ s.add_development_dependency(%q<jeweler>, ["~> 1.8"])
61
+ s.add_development_dependency(%q<debugger>, ["~> 1.6"])
62
+ else
63
+ s.add_dependency(%q<faraday>, ["= 0.8.8"])
64
+ s.add_dependency(%q<rest-client>, ["~> 1.8"])
65
+ s.add_dependency(%q<rspec>, ["~> 2.8"])
66
+ s.add_dependency(%q<bundler>, ["~> 1.0"])
67
+ s.add_dependency(%q<jeweler>, ["~> 1.8"])
68
+ s.add_dependency(%q<debugger>, ["~> 1.6"])
69
+ end
70
+ else
71
+ s.add_dependency(%q<faraday>, ["= 0.8.8"])
72
+ s.add_dependency(%q<rest-client>, ["~> 1.8"])
73
+ s.add_dependency(%q<rspec>, ["~> 2.8"])
74
+ s.add_dependency(%q<bundler>, ["~> 1.0"])
75
+ s.add_dependency(%q<jeweler>, ["~> 1.8"])
76
+ s.add_dependency(%q<debugger>, ["~> 1.6"])
77
+ end
78
+ end
79
+
data/lib/API/api.rb ADDED
@@ -0,0 +1,47 @@
1
+ module Instamojo
2
+
3
+ class << API
4
+ private
5
+ def whitelist_parameters
6
+ API::WHITELISTED_API_PARAMS.each { |x| attr_reader x }
7
+ end
8
+ end
9
+
10
+ class API
11
+ WHITELISTED_API_PARAMS = [:api_key, :auth_token, :endpoint]
12
+ whitelist_parameters
13
+ attr_reader :client
14
+
15
+ def initialize(api_key = nil, auth_token = nil, endpoint = nil, options = {}, &block)
16
+ options = find_params(api_key, auth_token, endpoint, options, block)
17
+
18
+ options.select { |k, _| WHITELISTED_API_PARAMS.include? k.to_sym }.each do |key, value|
19
+ instance_variable_set("@#{key}", value)
20
+ end
21
+ end
22
+
23
+ def client
24
+ @client ||= Instamojo::Client.new(self, @endpoint)
25
+ end
26
+
27
+ def to_s
28
+ @endpoint ? sprintf("Instamojo API(key: %s, endpoint: %s)", @api_key, @endpoint): sprintf("Instamojo API(key: %s)", @api_key)
29
+ end
30
+
31
+ private
32
+
33
+ def find_params(api_key, auth_token, endpoint, options, block)
34
+ params = if api_key.is_a? Hash
35
+ api_key
36
+ elsif api_key
37
+ { api_key: api_key, auth_token: auth_token }
38
+ elsif block
39
+ struct = OpenStruct.new
40
+ block.call(struct) && struct.marshal_dump
41
+ else options
42
+ end
43
+ params.merge!(endpoint: endpoint) if endpoint
44
+ params
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,15 @@
1
+ require 'faraday'
2
+ require 'json'
3
+ require 'uri'
4
+ require 'ostruct'
5
+ require 'rest_client'
6
+ require_relative 'utility'
7
+ require_relative 'common_object'
8
+ require_relative 'response'
9
+ require_relative 'base'
10
+ require_relative 'API/api'
11
+ require_relative 'client/link'
12
+ require_relative 'client/payment'
13
+ require_relative 'client/refund'
14
+ require_relative 'client/client'
15
+ require_relative 'client/payment_request'
data/lib/base.rb ADDED
@@ -0,0 +1,8 @@
1
+ module Instamojo
2
+ HOST = "https://www.instamojo.com"
3
+ API_VERSION = "1.1"
4
+ DEBUG = false
5
+ class API
6
+
7
+ end
8
+ end
@@ -0,0 +1,227 @@
1
+ module Instamojo
2
+ class Client
3
+ attr_reader :connection_options, :app_id
4
+ attr_reader :request, :response
5
+ attr_reader :authorized
6
+
7
+ URL = Instamojo::HOST + "/api/" + Instamojo::API_VERSION
8
+
9
+ def connection_options
10
+ @connection_options = lambda do |connection|
11
+ connection.request :url_encoded
12
+ connection.response :logger if Instamojo::DEBUG # TODO: set DEBUG flag for this
13
+ connection.adapter Faraday.default_adapter
14
+ end
15
+ end
16
+
17
+ def initialize(api, endpoint)
18
+ @endpoint = endpoint || URL
19
+ @conn = Faraday.new(@endpoint, &connection_options)
20
+
21
+ #TODO: To abstract in /errors.rb
22
+ raise "Supply API with api_key before generating client" unless api.api_key
23
+
24
+ @api_key = api.api_key
25
+ @auth_token = api.auth_token
26
+ add_header "X-Api-Key", @api_key
27
+ if @auth_token
28
+ add_header "X-Auth-Token", @auth_token
29
+ get 'debug' # dummy request to verify supplied auth_token
30
+ else
31
+ @authorized = "Not authorized"
32
+ end
33
+ end
34
+
35
+ def get_connection_object
36
+ @conn
37
+ end
38
+
39
+ def self.define_http_verb(http_verb)
40
+ define_method http_verb do |*args|
41
+ request = args[0] || "/"
42
+ params = args[1] || {}
43
+
44
+ @request = request
45
+ sanitize_request
46
+ method = @conn.method(http_verb)
47
+ @response = method.call(@request, params)
48
+ sanitize_response
49
+ end
50
+ end
51
+
52
+ define_http_verb :get
53
+ define_http_verb :post
54
+ define_http_verb :patch
55
+ define_http_verb :delete
56
+
57
+
58
+ # POST /auth/
59
+ # Authenticate, generate token and add header
60
+ def authenticate(username = nil, password = nil, options = {}, &block)
61
+ if username.is_a?(Hash) or password.is_a?(Hash)
62
+ options = username.is_a?(Hash) ? username : password
63
+ end
64
+ options["username"] = username if username and !username.is_a?Hash
65
+ options["password"] = password if password and !password.is_a?Hash
66
+
67
+ options = set_options(options, &block)
68
+
69
+ #TODO: Raise error if doesn't find username and password key in options
70
+ @response = post('auth', options)
71
+ if @response.has_key?("success") and @response['success']
72
+ add_header("X-Auth-Token", @response['token'])
73
+ end
74
+ @response
75
+ end
76
+
77
+ # GET /links
78
+ def links_list(opts = {})
79
+ get('links', opts)
80
+ @response.success? ? @response.body[:links].map { |link| Instamojo::Link.new link, self } : @response
81
+ end
82
+
83
+
84
+ # GET /links/:slug
85
+ def link_detail(slug)
86
+ slug = slug.slug if slug.instance_of? Instamojo::Link
87
+ get("links/#{slug}")
88
+ @response.success? ? Instamojo::Link.new(@response.body[:link], self) : @response
89
+ end
90
+
91
+ # POST /links
92
+ def create_link(options = {}, &block)
93
+ options = set_options(options, &block)
94
+ options[:file_upload_json] = options[:file_upload] && upload_file(options.delete(:file_upload))
95
+ options[:cover_image_json] = options[:cover_image] && upload_file(options.delete(:cover_image))
96
+ post('links', options)
97
+ @response.success? ? Instamojo::Link.new(@response.body[:link], self) : @response
98
+ end
99
+
100
+ # PATCH /links/:slug
101
+ def edit_link(link = nil, options = {}, &block)
102
+ if link && link.is_a?(Instamojo::Link)
103
+ yield(link) if block_given?
104
+ else
105
+ options = set_options(options, &block)
106
+ link = Instamojo::Link.new(options, self)
107
+ end
108
+ patch("links/#{link.slug}", link.to_h)
109
+ @response.success? ? Instamojo::Link.new(@response.body[:link], self) : @response
110
+ end
111
+
112
+ # DELETE /links/:slug
113
+ def archive_link(slug)
114
+ delete("links/#{slug}")
115
+ end
116
+
117
+ # POST 'https://filepicker.io/api/store/S3'
118
+ def upload_file(filepath)
119
+ if filepath && (file=File.open(File.expand_path(filepath), 'rb'))
120
+ if (url=get_file_upload_url).is_a? String
121
+ resource = RestClient::Resource.new(url)
122
+ resource.post fileUpload: file
123
+ end
124
+ end
125
+ end
126
+
127
+
128
+ # GET /payments
129
+ def payments_list(opts = {})
130
+ get('payments', opts)
131
+ @response.success? ? @response.body[:payments].map { |payment| Instamojo::Payment.new payment, self } : @response
132
+ end
133
+
134
+ # GET /payments/:payment_id
135
+ def payment_detail(payment_id)
136
+ payment_id = payment_id.payment_id if payment_id.instance_of? Instamojo::Payment
137
+ get("payments/#{payment_id}")
138
+ @response.success? ? Instamojo::Payment.new(@response.body[:payment], self) : @response
139
+ end
140
+
141
+ # POST /payment-requests
142
+ def payment_request(options, &block)
143
+ set_options(options, &block)
144
+ post('payment-requests', options)
145
+ @response.success? ? Instamojo::PaymentRequest.new(@response.body[:payment_request], self) : @response
146
+ end
147
+
148
+ # GET /payment-requests
149
+ def payment_requests_list(opts = {})
150
+ get('payment-requests', opts)
151
+ @response.success? ? @response.body[:payment_requests].map { |payment_request| Instamojo::PaymentRequest.new payment_request, self } : @response
152
+ end
153
+
154
+ def payment_request_status(payment_request_id)
155
+ payment_request_id = payment_request_id.id if payment_request_id.instance_of? Instamojo::PaymentRequest
156
+ get("payment-requests/#{payment_request_id}") if payment_request_id
157
+ @response.success? ? Instamojo::PaymentRequest.new(@response.body[:payment_request], self) : @response
158
+ end
159
+
160
+ # GET /refunds
161
+ def refunds_list(opts = {})
162
+ get('refunds', opts)
163
+ @response.success? ? @response.body[:refunds].map { |refund| Instamojo::Refund.new refund, self } : @response
164
+ end
165
+
166
+ # GET /refunds/:refund_id
167
+ def refund_detail(refund_id)
168
+ get("refunds/#{refund_id}")
169
+ @response.success? ? Instamojo::Refund.new(@response.body[:refund], self) : @response
170
+ end
171
+
172
+ # POST /refunds
173
+ def create_refund(options = {}, &block)
174
+ options = set_options(options, &block)
175
+ post('refunds', options)
176
+ @response.success? ? Instamojo::Refund.new(@response.body[:refund], self) : @response
177
+ end
178
+
179
+ # DELETE /auth/:token - Delete auth token
180
+ def logout
181
+ auth_token = get_connection_object.headers['X-Auth-Token']
182
+ raise "Can't find any authorization token to logout." unless auth_token
183
+ @response = delete("/auth/#{auth_token}")
184
+ if @response.has_key?("success") and @response['success']
185
+ get_connection_object.headers.delete("X-Auth-Token")
186
+ end
187
+ @response
188
+ end
189
+
190
+ def to_s
191
+ sprintf("Instamojo Client(URL: %s, Authorized: %s)", @endpoint, @authorized)
192
+ end
193
+
194
+ private
195
+ def sanitize_request
196
+ @request.concat('/') unless request.end_with? "/"
197
+ @request[0] = '' if request.start_with? "/"
198
+ end
199
+
200
+ def sanitize_response
201
+ @response = Instamojo::Response.new(@response)
202
+ @authorized = @response.success?
203
+ @response
204
+ end
205
+
206
+ def set_options(options, &block)
207
+ if block_given?
208
+ block_params = OpenStruct.new
209
+ block.call(block_params)
210
+ options = options.merge(block_params.marshal_dump)
211
+ end
212
+
213
+ options.symbolize_keys!
214
+ end
215
+
216
+ def add_header(key, value)
217
+ previous_headers = get_connection_object.headers
218
+ get_connection_object.headers = previous_headers.merge({key => value})
219
+ end
220
+
221
+ # GET /links/get_file_upload_url
222
+ def get_file_upload_url
223
+ get('links/get_file_upload_url')
224
+ @response.success? ? @response.body[:upload_url] : @response
225
+ end
226
+ end
227
+ end
@@ -0,0 +1,60 @@
1
+ module Instamojo
2
+
3
+ =begin Example
4
+ {
5
+ "title" => "Foo product",
6
+ "description" => "",
7
+ "slug" => "foo-product",
8
+ "shorturl" => "http://imojo.in/ankurfoobar",
9
+ "url" => "https://www.instamojo.com/ankurgel/foo-product/",
10
+ "cover_image" => "https://www.filepicker.io/api/file/BHeefKAARCKGC5l1J29e/convert?w=500&h=500&fit=clip&quality=70",
11
+ "currency" => "INR",
12
+ "base_price" => "0.00",
13
+ "quantity" => nil,
14
+ "quantity_sold" => 2,
15
+ "requires_shipping" => false,
16
+ "ships_within_days" => nil,
17
+ "start_date" => nil,
18
+ "end_date" => nil,
19
+ "venue" => nil,
20
+ "timezone" => nil,
21
+ "note" => nil,
22
+ "redirect_url" => nil,
23
+ "webhook_url" => nil,
24
+ "status" => "Live",
25
+ "enable_pwyw" => false,
26
+ "enable_sign" => false,
27
+ "socialpay_platforms" => ""
28
+ }
29
+ =end
30
+
31
+ class Link
32
+ attr_accessor :title, :description, :slug, :shorturl, :url, :cover_image, :currency, :base_price, :quantity
33
+ attr_accessor :quantity_sold, :requires_shipping, :ships_within_days, :start_date, :end_date, :venue, :timezone
34
+ attr_accessor :note, :redirect_url, :webhook_url, :status, :enable_pwyw, :enable_sign, :socialpay_platforms
35
+
36
+ attr_reader :original
37
+
38
+ include CommonObject
39
+ detail_method :link_detail, :slug
40
+
41
+ def initialize(link, client)
42
+ assign_values(link)
43
+ @client = client # Reference to client
44
+ end
45
+
46
+ # Carry out update request on a Link
47
+ def save(&block)
48
+ @client.edit_link(self, {}, &block)
49
+ end
50
+
51
+ # Carry out DELETE request on a link
52
+ def archive
53
+ @client.archive_link(self.slug)
54
+ end
55
+
56
+ def to_s
57
+ sprintf("Instamojo Link(slug: %s, title: %s, shorturl: %s, status: %s)", slug, title, shorturl, status)
58
+ end
59
+ end
60
+ end