Showing
11 changed files
with
204 additions
and
0 deletions
.gitignore
0 → 100644
.travis.yml
0 → 100644
Gemfile
0 → 100644
README.md
0 → 100644
1 | +# Var | ||
2 | + | ||
3 | +Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/var`. To experiment with that code, run `bin/console` for an interactive prompt. | ||
4 | + | ||
5 | +TODO: Delete this and the text above, and describe your gem | ||
6 | + | ||
7 | +## Installation | ||
8 | + | ||
9 | +Add this line to your application's Gemfile: | ||
10 | + | ||
11 | +```ruby | ||
12 | +gem 'var' | ||
13 | +``` | ||
14 | + | ||
15 | +And then execute: | ||
16 | + | ||
17 | + $ bundle | ||
18 | + | ||
19 | +Or install it yourself as: | ||
20 | + | ||
21 | + $ gem install var | ||
22 | + | ||
23 | +## Usage | ||
24 | + | ||
25 | +TODO: Write usage instructions here | ||
26 | + | ||
27 | +## Development | ||
28 | + | ||
29 | +After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment. | ||
30 | + | ||
31 | +To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release` to create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org). | ||
32 | + | ||
33 | +## Contributing | ||
34 | + | ||
35 | +1. Fork it ( https://github.com/[my-github-username]/var/fork ) | ||
36 | +2. Create your feature branch (`git checkout -b my-new-feature`) | ||
37 | +3. Commit your changes (`git commit -am 'Add some feature'`) | ||
38 | +4. Push to the branch (`git push origin my-new-feature`) | ||
39 | +5. Create a new Pull Request |
bin/console
0 → 100644
1 | +#!/usr/bin/env ruby | ||
2 | + | ||
3 | +require "bundler/setup" | ||
4 | +require "var" | ||
5 | + | ||
6 | +# You can add fixtures and/or initialization code here to make experimenting | ||
7 | +# with your gem easier. You can also use a different console, if you like. | ||
8 | + | ||
9 | +# (If you use this, don't forget to add pry to your Gemfile!) | ||
10 | +# require "pry" | ||
11 | +# Pry.start | ||
12 | + | ||
13 | +require "irb" | ||
14 | +IRB.start |
bin/setup
0 → 100644
lib/var.rb
0 → 100644
1 | +require "var/version" | ||
2 | +require 'conekta' | ||
3 | + | ||
4 | +module Var | ||
5 | + @@valid_services = [:conekta] | ||
6 | + | ||
7 | + def self.valid_services | ||
8 | + @@valid_services | ||
9 | + end | ||
10 | + | ||
11 | + def self.create_charge(service, object, options = {}) | ||
12 | + if(!@@valid_services.include?(service)) | ||
13 | + return { error_message: 'Service is not supported' } | ||
14 | + end | ||
15 | + if(!object.respond_to?(:charge_with)) | ||
16 | + return { error_message: "#{object.class.to_s} doesn't support charges"} | ||
17 | + end | ||
18 | + object.charge_with(:conekta, options) | ||
19 | + end | ||
20 | + | ||
21 | + private | ||
22 | +end | ||
23 | + | ||
24 | +module ActsAsChargeable | ||
25 | + extend ActiveSupport::Concern | ||
26 | + | ||
27 | + module ClassMethods | ||
28 | + def acts_as_chargeable(keys = {}) | ||
29 | + include ChargeableInstanceMethods | ||
30 | + cattr_accessor :sync_attributes | ||
31 | + self.sync_attributes = keys | ||
32 | + end | ||
33 | + end | ||
34 | + | ||
35 | + module ChargeableInstanceMethods | ||
36 | + def charge_with(service, options) | ||
37 | + begin | ||
38 | + self.send("charge_with_#{service.to_s}", options) | ||
39 | + rescue => exception | ||
40 | + return { error_message: exception.message } | ||
41 | + end | ||
42 | + end | ||
43 | + | ||
44 | + def charge_with_conekta(options) | ||
45 | + if(!options.include? :card_token) | ||
46 | + error_message = "Conekta needs a card token sent as a third paramater" | ||
47 | + return { error_message: error_message} | ||
48 | + end | ||
49 | + @charge ||= Conekta::Charge.create({ | ||
50 | + description: self.sync_attribute('description'), | ||
51 | + amount: self.sync_attribute('amount'), | ||
52 | + currency: "MXN", | ||
53 | + reference_id: self.sync_attribute('reference_id'), | ||
54 | + card: options[:card_token], | ||
55 | + details: { | ||
56 | + name: self.sync_attribute('details_name'), | ||
57 | + email: self.sync_attribute('details_email'), | ||
58 | + line_items: [{ | ||
59 | + name: self.sync_attribute('details_name'), | ||
60 | + description: self.sync_attribute('description'), | ||
61 | + unit_price: self.sync_attribute('amount'), | ||
62 | + quantity: 1, | ||
63 | + }] | ||
64 | + } | ||
65 | + }) | ||
66 | + end | ||
67 | + | ||
68 | + def find_charge(service) | ||
69 | + begin | ||
70 | + if(!Var.valid_services.include?(service)) | ||
71 | + return { error_message: 'Service is not supported' } | ||
72 | + end | ||
73 | + self.send("find_#{service.to_s}_charge") | ||
74 | + rescue => exception | ||
75 | + return { error_message: exception.message } | ||
76 | + end | ||
77 | + end | ||
78 | + | ||
79 | + def find_conekta_charge | ||
80 | + ref_id = self.sync_attribute('reference_id') | ||
81 | + Conekta::Charge.where({'status.ne'=>'paid', 'reference_id'=> ref_id}) | ||
82 | + end | ||
83 | + | ||
84 | + def sync_attribute(key) | ||
85 | + return self.send(key) unless self.sync_attributes.include? key.to_sym | ||
86 | + self.send(sync_attributes[key.to_sym]) | ||
87 | + end | ||
88 | + end | ||
89 | +end | ||
90 | + | ||
91 | +ActiveRecord::Base.send(:include, ActsAsChargeable) if defined? ActiveRecord::Base | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
lib/var/version.rb
0 → 100644
var-0.1.0.gem
0 → 100644
No preview for this file type
var.gemspec
0 → 100644
1 | +# coding: utf-8 | ||
2 | +lib = File.expand_path('../lib', __FILE__) | ||
3 | +$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) | ||
4 | +require 'var/version' | ||
5 | + | ||
6 | +Gem::Specification.new do |spec| | ||
7 | + spec.name = "var" | ||
8 | + spec.version = Var::VERSION | ||
9 | + spec.authors = ["abrahamrq"] | ||
10 | + spec.email = ["abraham.rq03@gmail.com"] | ||
11 | + | ||
12 | + spec.summary = %q{summary of var} | ||
13 | + spec.description = %q{description of var} | ||
14 | + spec.homepage = "https://github.com/abrahamrq" | ||
15 | + | ||
16 | + # Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or | ||
17 | + # delete this section to allow pushing this gem to any host. | ||
18 | + if spec.respond_to?(:metadata) | ||
19 | + spec.metadata['allowed_push_host'] = "TODO: Set to 'http://mygemserver.com'" | ||
20 | + else | ||
21 | + raise "RubyGems 2.0 or newer is required to protect against public gem pushes." | ||
22 | + end | ||
23 | + | ||
24 | + spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) } | ||
25 | + spec.bindir = "exe" | ||
26 | + spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) } | ||
27 | + spec.require_paths = ["lib"] | ||
28 | + | ||
29 | + spec.add_dependency 'conekta', "~> 0.5" | ||
30 | + spec.add_development_dependency "bundler", "~> 1.10.a" | ||
31 | + spec.add_development_dependency "rake", "~> 10.0" | ||
32 | +end |
-
Please register or login to post a comment