organizai 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.bundle/config ADDED
@@ -0,0 +1,2 @@
1
+ ---
2
+ BUNDLE_DISABLE_SHARED_GEMS: "1"
data/Gemfile ADDED
@@ -0,0 +1,11 @@
1
+ source 'http://rubygems.org'
2
+
3
+ gem 'oauth', '0.4.3'
4
+
5
+ group :development do
6
+ gem "shoulda", ">= 0"
7
+ gem "bundler", "~> 1.0.0"
8
+ gem "jeweler", "~> 1.6.4"
9
+ gem "rcov", ">= 0"
10
+ gem "rdoc", ">= 0"
11
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,24 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ git (1.2.5)
5
+ jeweler (1.6.4)
6
+ bundler (~> 1.0)
7
+ git (>= 1.2.5)
8
+ rake
9
+ oauth (0.4.3)
10
+ rake (0.9.2)
11
+ rcov (0.9.9)
12
+ rdoc (3.6.1)
13
+ shoulda (2.10.3)
14
+
15
+ PLATFORMS
16
+ ruby
17
+
18
+ DEPENDENCIES
19
+ bundler (~> 1.0.0)
20
+ jeweler (~> 1.6.4)
21
+ oauth (= 0.4.3)
22
+ rcov
23
+ rdoc
24
+ shoulda
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 Lucca Mordente
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 ADDED
@@ -0,0 +1,19 @@
1
+ = teste
2
+
3
+ Description goes here.
4
+
5
+ == Contributing to teste
6
+
7
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
8
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
9
+ * Fork the project
10
+ * Start a feature/bugfix branch
11
+ * Commit and push until you are happy with your contribution
12
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
13
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
14
+
15
+ == Copyright
16
+
17
+ Copyright (c) 2011 Lucca Mordente. See LICENSE.txt for
18
+ further details.
19
+
data/Rakefile ADDED
@@ -0,0 +1,53 @@
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 = "organizai"
18
+ gem.homepage = "http://github.com/ditointernet/organizai"
19
+ gem.license = "MIT"
20
+ gem.summary = %Q{Organizaí integration}
21
+ gem.description = %Q{Integrate daily deal sites with Organizaí}
22
+ gem.email = "luccamordente@gmail.com"
23
+ gem.authors = ["Lucca Mordente"]
24
+ # dependencies defined in Gemfile
25
+ end
26
+ Jeweler::RubygemsDotOrgTasks.new
27
+
28
+ require 'rake/testtask'
29
+ Rake::TestTask.new(:test) do |test|
30
+ test.libs << 'lib' << 'test'
31
+ test.pattern = 'test/**/test_*.rb'
32
+ test.verbose = true
33
+ end
34
+
35
+ require 'rcov/rcovtask'
36
+ Rcov::RcovTask.new do |test|
37
+ test.libs << 'test'
38
+ test.pattern = 'test/**/test_*.rb'
39
+ test.verbose = true
40
+ test.rcov_opts << '--exclude "gems/*"'
41
+ end
42
+
43
+ task :default => :test
44
+
45
+ require 'rdoc/task'
46
+ Rake::RDocTask.new do |rdoc|
47
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
48
+
49
+ rdoc.rdoc_dir = 'rdoc'
50
+ rdoc.title = "organizai #{version}"
51
+ rdoc.rdoc_files.include('README*')
52
+ rdoc.rdoc_files.include('lib/**/*.rb')
53
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,21 @@
1
+ require 'active_support/secure_random'
2
+
3
+ module Organizai
4
+ module Generators
5
+ class InstallGenerator < Rails::Generators::Base
6
+ source_root File.expand_path("../../templates", __FILE__)
7
+
8
+ desc "Creates a Organizaí initializer."
9
+ class_option :orm
10
+
11
+ def copy_initializer
12
+ template "organizai.rb", "config/initializers/organizai.rb"
13
+ end
14
+
15
+ def show_readme
16
+ readme "README" if behavior == :invoke
17
+ end
18
+
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,24 @@
1
+ Organizai.config do |config|
2
+
3
+ # Ask for sandbox credentials
4
+ config.consumer(:development) do |consumer|
5
+ consumer.key = "consumer_key"
6
+ consumer.secret = "consumer_secret"
7
+ consumer.kind = "sandbox"
8
+ end
9
+
10
+ # Ask for production credentials
11
+ # config.consumer(:production) do |consumer|
12
+ # consumer.key = "consumer_key"
13
+ # consumer.secret = "consumer_secret"
14
+ # consumer.kind = "production"
15
+ # end
16
+
17
+ # Api call format
18
+ # config.format = :json
19
+
20
+ # Protocol
21
+ # Only the production API supports HTTPS
22
+ # config.protocol = :http
23
+
24
+ end
@@ -0,0 +1,24 @@
1
+ module Organizai
2
+ class Api
3
+
4
+ attr_reader :token
5
+
6
+ def initialize(token=nil,secret=nil)
7
+ @token = OAuth::Token.new(token,secret)
8
+ raise "You must install the initializer before using Organizaí. Run 'rails g organizai:install'." unless Organizai.configured?
9
+ end
10
+
11
+ def user(action,*options)
12
+ existing_actions = [:create]
13
+ raise "Undefined api call '#{action.to_s}' for user." unless existing_actions.include?(action)
14
+ Organizai::User.send(action, *options)
15
+ end
16
+
17
+ def coupon(action,*options)
18
+ existing_actions = [:create]
19
+ raise "Undefined api call '#{action.to_s}' for coupon." unless existing_actions.include?(action)
20
+ Organizai::Coupon.send(action, @token, *options)
21
+ end
22
+
23
+ end
24
+ end
@@ -0,0 +1,13 @@
1
+ module Organizai
2
+ class Consumer
3
+
4
+ attr_accessor :key, :secret, :kind
5
+
6
+ def kind=(value)
7
+ value = value.to_sym
8
+ raise "Organizaí environment :#{value} does not exists." unless Organizai::HOST.keys.include?(value)
9
+ @kind = value
10
+ end
11
+
12
+ end
13
+ end
@@ -0,0 +1,9 @@
1
+ module Organizai
2
+ class Coupon
3
+
4
+ def self.create(token,coupon_data)
5
+ Organizai::Provider.request(:post,:coupon,'/users/me/coupons',coupon_data,token)
6
+ end
7
+
8
+ end
9
+ end
@@ -0,0 +1,11 @@
1
+ require "organizai"
2
+ require "rails"
3
+
4
+ module Organizai
5
+ class Engine < Rails::Engine
6
+
7
+
8
+
9
+
10
+ end
11
+ end
@@ -0,0 +1,24 @@
1
+ module Organizai
2
+ class Provider
3
+
4
+ def self.request(method,resource,endpoint,data,token=nil)
5
+
6
+ formatted_uri = "#{Organizai.protocol}://#{HOST[Organizai.api_env]}#{endpoint}.#{Organizai.format}"
7
+
8
+ formatted_data = case Organizai.format
9
+ when :json
10
+ ActiveSupport::JSON.encode(data)
11
+ when :xml
12
+ data.to_xml :root => resource
13
+ end
14
+
15
+ current_consumer = Organizai.current_consumer
16
+ oauth_consumer = OAuth::Consumer.new(current_consumer.key, current_consumer.secret)
17
+ response = oauth_consumer.request(method,formatted_uri,token,{},{resource => formatted_data})
18
+
19
+ response_object = Organizai::Response.new(response,Organizai.format)
20
+
21
+ end
22
+
23
+ end
24
+ end
@@ -0,0 +1,34 @@
1
+ module Organizai
2
+ class Response
3
+
4
+ attr_reader :http, :format, :error, :object
5
+
6
+ def initialize(http_response,format)
7
+ @http_response = http_response
8
+ @format = format
9
+ @error = http_response.code != "200"
10
+ set_response_object
11
+ end
12
+
13
+ def error?
14
+ @error
15
+ end
16
+
17
+ def success?
18
+ !error?
19
+ end
20
+
21
+ private
22
+
23
+ def set_response_object
24
+ @object = case @format
25
+ when :json
26
+ ActiveSupport::JSON.decode(@http_response.body)
27
+ when :xml
28
+ Hash.from_xml(@http_response.body)['response']
29
+ end.symbolize_keys!
30
+ end
31
+
32
+
33
+ end
34
+ end
@@ -0,0 +1,20 @@
1
+ module Organizai
2
+ class User
3
+
4
+ def self.create(user_data)
5
+ response = Organizai::Provider.request(:post,:user,'/users',user_data)
6
+ # hack to hasheize the tokens until the api gets fixed
7
+ if response.success?
8
+ object = response.object
9
+ tokens = object.delete(:token).split('&')
10
+ object[:access_token] = tokens[0].split('=').last
11
+ object[:token_secret] = tokens[1].split('=').last
12
+ response.instance_exec do
13
+ @object = object
14
+ end
15
+ end
16
+ response
17
+ end
18
+
19
+ end
20
+ end
data/lib/organizai.rb ADDED
@@ -0,0 +1,73 @@
1
+ require 'active_support'
2
+ require 'active_support/dependencies'
3
+ require 'active_support/core_ext'
4
+
5
+ require 'oauth'
6
+
7
+ require 'organizai/consumer'
8
+ require 'organizai/provider'
9
+ require 'organizai/response'
10
+ require 'organizai/api'
11
+
12
+ require 'organizai/user'
13
+ require 'organizai/coupon'
14
+
15
+ module Organizai
16
+
17
+ # Consumers for each environment
18
+ mattr_accessor :consumers
19
+ @@consumers = ActiveSupport::OrderedHash.new
20
+
21
+ # Api calls format
22
+ mattr_accessor :format
23
+ @@format = :json
24
+
25
+ # Protocol
26
+ mattr_accessor :protocol
27
+ @@protocol = :http
28
+
29
+ # Whether Organizai.config was called or not
30
+ @@configured = false;
31
+
32
+ # API hosts
33
+ HOST = {
34
+ :production => "api.organizai.com.br",
35
+ :sandbox => "sandbox.organizai.com.br"
36
+ }
37
+
38
+ # Setup
39
+ def self.config
40
+ yield self
41
+ @@configured = true
42
+ raise "Missing consumer config for Organizaí" if @@consumers.empty?
43
+ end
44
+
45
+ def self.configured?
46
+ @@configured
47
+ end
48
+
49
+ # Used by #config to add consumers configuration
50
+ def self.consumer env, &block
51
+ consumer = Organizai::Consumer.new
52
+ consumer.instance_eval(&block)
53
+ @@consumers[env] = consumer
54
+ end
55
+
56
+ # App env
57
+ def self.env
58
+ defined?(::Rails) ? ::Rails.env.to_sym : :development
59
+ end
60
+
61
+ # Current api env
62
+ def self.api_env
63
+ current_consumer.kind
64
+ end
65
+
66
+ # Current consumer by app env
67
+ def self.current_consumer
68
+ consumer = @@consumers[env]
69
+ raise "Missing consumer config for '#{env}' environment." if consumer.nil?
70
+ consumer
71
+ end
72
+
73
+ end
Binary file
metadata ADDED
@@ -0,0 +1,157 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: organizai
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 1
8
+ - 0
9
+ version: 0.1.0
10
+ platform: ruby
11
+ authors:
12
+ - Lucca Mordente
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2011-07-22 00:00:00 -03:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ prerelease: false
22
+ type: :runtime
23
+ name: oauth
24
+ version_requirements: &id001 !ruby/object:Gem::Requirement
25
+ requirements:
26
+ - - "="
27
+ - !ruby/object:Gem::Version
28
+ segments:
29
+ - 0
30
+ - 4
31
+ - 3
32
+ version: 0.4.3
33
+ requirement: *id001
34
+ - !ruby/object:Gem::Dependency
35
+ prerelease: false
36
+ type: :development
37
+ name: shoulda
38
+ version_requirements: &id002 !ruby/object:Gem::Requirement
39
+ requirements:
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ segments:
43
+ - 0
44
+ version: "0"
45
+ requirement: *id002
46
+ - !ruby/object:Gem::Dependency
47
+ prerelease: false
48
+ type: :development
49
+ name: bundler
50
+ version_requirements: &id003 !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ segments:
55
+ - 1
56
+ - 0
57
+ - 0
58
+ version: 1.0.0
59
+ requirement: *id003
60
+ - !ruby/object:Gem::Dependency
61
+ prerelease: false
62
+ type: :development
63
+ name: jeweler
64
+ version_requirements: &id004 !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ~>
67
+ - !ruby/object:Gem::Version
68
+ segments:
69
+ - 1
70
+ - 6
71
+ - 4
72
+ version: 1.6.4
73
+ requirement: *id004
74
+ - !ruby/object:Gem::Dependency
75
+ prerelease: false
76
+ type: :development
77
+ name: rcov
78
+ version_requirements: &id005 !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ segments:
83
+ - 0
84
+ version: "0"
85
+ requirement: *id005
86
+ - !ruby/object:Gem::Dependency
87
+ prerelease: false
88
+ type: :development
89
+ name: rdoc
90
+ version_requirements: &id006 !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ segments:
95
+ - 0
96
+ version: "0"
97
+ requirement: *id006
98
+ description: "Integrate daily deal sites with Organiza\xC3\xAD"
99
+ email: luccamordente@gmail.com
100
+ executables: []
101
+
102
+ extensions: []
103
+
104
+ extra_rdoc_files:
105
+ - LICENSE.txt
106
+ - README
107
+ files:
108
+ - .bundle/config
109
+ - Gemfile
110
+ - Gemfile.lock
111
+ - LICENSE.txt
112
+ - README
113
+ - Rakefile
114
+ - VERSION
115
+ - lib/generators/organizai/install_generator.rb
116
+ - lib/generators/templates/organizai.rb
117
+ - lib/organizai.rb
118
+ - lib/organizai/api.rb
119
+ - lib/organizai/consumer.rb
120
+ - lib/organizai/coupon.rb
121
+ - lib/organizai/init.rb
122
+ - lib/organizai/provider.rb
123
+ - lib/organizai/response.rb
124
+ - lib/organizai/user.rb
125
+ - pkg/organizai-0.1.0.gem
126
+ has_rdoc: true
127
+ homepage: http://github.com/ditointernet/organizai
128
+ licenses:
129
+ - MIT
130
+ post_install_message:
131
+ rdoc_options: []
132
+
133
+ require_paths:
134
+ - lib
135
+ required_ruby_version: !ruby/object:Gem::Requirement
136
+ requirements:
137
+ - - ">="
138
+ - !ruby/object:Gem::Version
139
+ segments:
140
+ - 0
141
+ version: "0"
142
+ required_rubygems_version: !ruby/object:Gem::Requirement
143
+ requirements:
144
+ - - ">="
145
+ - !ruby/object:Gem::Version
146
+ segments:
147
+ - 0
148
+ version: "0"
149
+ requirements: []
150
+
151
+ rubyforge_project:
152
+ rubygems_version: 1.3.6
153
+ signing_key:
154
+ specification_version: 3
155
+ summary: "Organiza\xC3\xAD integration"
156
+ test_files: []
157
+