roempro 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/CHANGELOG.md +20 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +62 -0
- data/README.rdoc +61 -0
- data/Rakefile +1 -0
- data/lib/rails/generators/roempro/install_generator.rb +21 -0
- data/lib/rails/generators/roempro/templates/initializer.rb +9 -0
- data/lib/rails/generators/roempro/templates/roempro.yml +14 -0
- data/lib/roempro/base.rb +34 -0
- data/lib/roempro/class.rb +100 -0
- data/lib/roempro/config.rb +45 -0
- data/lib/roempro/request.rb +200 -0
- data/lib/roempro/response.rb +67 -0
- data/lib/roempro/version.rb +7 -0
- data/lib/roempro.rb +9 -0
- data/roempro.gemspec +23 -0
- metadata +66 -0
data/.gitignore
ADDED
data/CHANGELOG.md
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
# ChangeLog
|
2
|
+
|
3
|
+
## 0.1 (2012-10-14)
|
4
|
+
|
5
|
+
* Write documentation
|
6
|
+
* Improve url validation into Roempro::Config and Roempro::Request contructors
|
7
|
+
* Handle global session id as well as instance one
|
8
|
+
* Improve Roempro::Request intializer
|
9
|
+
* Make Roempro::Request and Roempro::Response inherit from Roempro::Class
|
10
|
+
* Split Roempro content into Roempro::Base and Roempro::Config
|
11
|
+
* Create Roempro::Class to override default class.
|
12
|
+
* Create Rails 3 initializer
|
13
|
+
* New method Roempro::Request#command to submit the command by submit a string
|
14
|
+
* Keep the last response into request
|
15
|
+
* Handle login stuff
|
16
|
+
* Improve exception handling
|
17
|
+
* Create Roempro::Response
|
18
|
+
* Create Roempro::Request class.
|
19
|
+
* Set up description and summary in gemspec file.
|
20
|
+
* Initialise roempro gem.
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Révillon Loïk
|
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,62 @@
|
|
1
|
+
# Roempro - Oempro Ruby Wrapper
|
2
|
+
|
3
|
+
It aims to easily deal with the API of a given Oempro application.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
### Using Gemfile
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
gem 'roempro'
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle install
|
16
|
+
|
17
|
+
### By hand
|
18
|
+
|
19
|
+
Install it yourself as:
|
20
|
+
|
21
|
+
$ gem install roempro
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
### Rails 3
|
26
|
+
|
27
|
+
After installing the gem, run the rails generator to populate the `config` and `config/initializers` directories
|
28
|
+
|
29
|
+
$ rails g roempro:install
|
30
|
+
|
31
|
+
Set up the configuration in `config/roempro.yml`
|
32
|
+
|
33
|
+
production:
|
34
|
+
url: path_to_oempro_api
|
35
|
+
username: oempro_user
|
36
|
+
password: oempro_password
|
37
|
+
|
38
|
+
This will set up a default configuration use by Roempro.
|
39
|
+
|
40
|
+
**Don't forget to restrict the permitions on the file.**
|
41
|
+
|
42
|
+
$ chmod 600 config/roempro.yml
|
43
|
+
|
44
|
+
### Ruby and others frameworks
|
45
|
+
|
46
|
+
The default configuration can also be set using Roempro::Config object, as
|
47
|
+
|
48
|
+
irb > Roempro::Config.load_from_hash :url => "path_to_oempro",
|
49
|
+
:username => "oempro_user",
|
50
|
+
:password => "oempro_password"
|
51
|
+
|
52
|
+
### All cases
|
53
|
+
|
54
|
+
The configuration can be set dynamicly, using the Roempro::Request constructor. *Useless if you set up Roempro::Config*
|
55
|
+
|
56
|
+
request = Roempro::Request.new :url => "path_to_oempro", :username => "oempro_user", :password => "oempro_password"
|
57
|
+
|
58
|
+
Then, actually perform the request. For instance, retrieve all campaigns
|
59
|
+
|
60
|
+
request.get_campaigns
|
61
|
+
|
62
|
+
**See the documentation for an advanced usage.**
|
data/README.rdoc
ADDED
@@ -0,0 +1,61 @@
|
|
1
|
+
= Roempro - Oempro Ruby Wrapper
|
2
|
+
|
3
|
+
It aims to easily deal with the API of a given Oempro application.
|
4
|
+
|
5
|
+
== Installation
|
6
|
+
|
7
|
+
=== Using Gemfile
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
gem 'roempro'
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle install
|
16
|
+
|
17
|
+
=== By hand
|
18
|
+
|
19
|
+
Install it yourself as:
|
20
|
+
|
21
|
+
$ gem install roempro
|
22
|
+
|
23
|
+
== Usage
|
24
|
+
|
25
|
+
=== Rails 3
|
26
|
+
|
27
|
+
After installing the gem, run the rails generator to populate the `config` and `config/initializers` directories
|
28
|
+
|
29
|
+
$ rails g roempro:install
|
30
|
+
|
31
|
+
Set up the configuration in `config/roempro.yml`
|
32
|
+
|
33
|
+
production:
|
34
|
+
url: path_to_oempro_api
|
35
|
+
username: oempro_user
|
36
|
+
password: oempro_password
|
37
|
+
|
38
|
+
This will set up a default configuration use by Roempro.
|
39
|
+
|
40
|
+
<b>Don't forget to restrict the permitions on the file.</b>
|
41
|
+
|
42
|
+
$ chmod 600 config/roempro.yml
|
43
|
+
|
44
|
+
=== Ruby and others frameworks
|
45
|
+
|
46
|
+
The default configuration can also be set using Roempro::Config object, as
|
47
|
+
|
48
|
+
irb > Roempro::Config.load_from_hash :url => "path_to_oempro", :username => "oempro_user", :password => "oempro_password"
|
49
|
+
|
50
|
+
=== All cases
|
51
|
+
|
52
|
+
The configuration can be set dynamicly, using the Roempro::Request constructor.
|
53
|
+
*Useless if you set up Roempro::Config*
|
54
|
+
|
55
|
+
request = Roempro::Request.new :url => "path_to_oempro", :username => "oempro_user", :password => "oempro_password"
|
56
|
+
|
57
|
+
Then, actually perform the request. For instance, retrieve all campaigns
|
58
|
+
|
59
|
+
request.get_campaigns
|
60
|
+
|
61
|
+
<b>See the documentation for an advanced usage.</b>
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# -*- encoding : UTF-8 -*-
|
2
|
+
|
3
|
+
module Roempro
|
4
|
+
module Generators
|
5
|
+
class InstallGenerator < Rails::Generators::Base
|
6
|
+
desc 'Create the Roempro gem configuration file at config/roempro.yml, and an initializer at config/initializers/roempro.rb'
|
7
|
+
|
8
|
+
def self.source_root
|
9
|
+
@roempro_source_root = File.expand_path("../templates", __FILE__)
|
10
|
+
end
|
11
|
+
|
12
|
+
def create_config_file
|
13
|
+
template 'roempro.yml', File.join('config', 'roempro.yml')
|
14
|
+
end
|
15
|
+
|
16
|
+
def create_initializer_file
|
17
|
+
template 'initializer.rb', File.join('config', 'initializers', 'roempro.rb')
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,9 @@
|
|
1
|
+
# -*- encoding : UTF-8 -*-
|
2
|
+
|
3
|
+
require 'yaml'
|
4
|
+
|
5
|
+
# Retrieve the values from config/roempro.yml
|
6
|
+
config_values = YAML.load_file File.join(Rails.root, 'config', 'roempro.yml')
|
7
|
+
|
8
|
+
# Load the configuration into Roempro
|
9
|
+
Roempro::Config.load_from_hash config_values[Rails.env.to_s]
|
data/lib/roempro/base.rb
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
# -*- encoding : UTF-8 -*-
|
2
|
+
|
3
|
+
##
|
4
|
+
# Roempro, a shortcut for Ruby Oempro, is a Ruby wrapper for the Oempro API,
|
5
|
+
# which provide a way to deal with the API easily.
|
6
|
+
#
|
7
|
+
# It's splited into mutiples sub-classes :
|
8
|
+
# * Roempro::Base
|
9
|
+
# * Roempro::Config
|
10
|
+
# * Roempro::Request
|
11
|
+
# * Roempro::Response
|
12
|
+
#
|
13
|
+
# Actually, Roempro::Class is also available but only used internaly, to give
|
14
|
+
# more power to the Ruby class.
|
15
|
+
#
|
16
|
+
# Roempro::Base is responsible to give the commun stuff and hold some global
|
17
|
+
# informations. Roempro::Config is responsible to hold the default
|
18
|
+
# configuration. It give a way to efficiently init Roempro in Rails 3, with
|
19
|
+
# intializers. Roempro::Request is responsible to handle what is needed to
|
20
|
+
# actually deal with the API. It also provide severals other abilities.
|
21
|
+
# Roempro::Response is responsible to store the API response. More, it yields
|
22
|
+
# means to easily acces to the response component.
|
23
|
+
module Roempro
|
24
|
+
|
25
|
+
##
|
26
|
+
# Hold few informations used by the Roempro's components.
|
27
|
+
#
|
28
|
+
# For instance, it keep in mind the session_id when the user is logged in
|
29
|
+
# the API. Thus, all new Roempro::Request object will used this session_id,
|
30
|
+
# unless explicitly told them to not use it.
|
31
|
+
class Base < Roempro::Class
|
32
|
+
cattr_accessor :session_id
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,100 @@
|
|
1
|
+
# -*- encoding : UTF-8 -*-
|
2
|
+
|
3
|
+
module Roempro
|
4
|
+
|
5
|
+
##
|
6
|
+
# Few improvements appended to the default Ruby *class*. This aim to be used
|
7
|
+
# by the others Roempro's components, to provide them tools to help with
|
8
|
+
# development.
|
9
|
+
#
|
10
|
+
# To achieve this, the Roempro component must inhérite from this specific
|
11
|
+
# Roempro::Class.
|
12
|
+
class Class
|
13
|
+
|
14
|
+
##
|
15
|
+
# Create the reader accessors for the given attributes.
|
16
|
+
#
|
17
|
+
# <b>Planed to be used by the inherited class.</b>
|
18
|
+
#
|
19
|
+
# === Parameters
|
20
|
+
# [Array]
|
21
|
+
# Named variables which required the accessors to be made.
|
22
|
+
#
|
23
|
+
# === Examples
|
24
|
+
#
|
25
|
+
# Roempro.module_eval do
|
26
|
+
# class SomeClass < Roempro::Class
|
27
|
+
# cattr_reader :var
|
28
|
+
# end
|
29
|
+
# end
|
30
|
+
#
|
31
|
+
# > Roempro::SomeClass.var
|
32
|
+
# => nil
|
33
|
+
def self.cattr_reader(*attributes)
|
34
|
+
attributes.map!(&:to_sym).each do |attr|
|
35
|
+
class_eval(<<-EOS, __FILE__, __LINE__ + 1)
|
36
|
+
@@#{attr} = nil unless defined? @@#{attr}
|
37
|
+
|
38
|
+
def self.#{attr}
|
39
|
+
@@#{attr}
|
40
|
+
end
|
41
|
+
EOS
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
##
|
46
|
+
# Create the writer accessors for the given attributes.
|
47
|
+
#
|
48
|
+
# <b>Planed to be used by the inherited class.</b>
|
49
|
+
#
|
50
|
+
# === Parameters
|
51
|
+
# [Array]
|
52
|
+
# Named variables which required the accessors to be made.
|
53
|
+
#
|
54
|
+
# === Examples
|
55
|
+
#
|
56
|
+
# Roempro.module_eval do
|
57
|
+
# class SomeClass < Roempro::Class
|
58
|
+
# cattr_writer :var
|
59
|
+
# end
|
60
|
+
# end
|
61
|
+
#
|
62
|
+
# > Roempro::SomeClass.var = "Hello world!"
|
63
|
+
# => "Hello world!"
|
64
|
+
def self.cattr_writer(*attributes)
|
65
|
+
attributes.map!(&:to_sym).each do |attr|
|
66
|
+
class_eval(<<-EOS, __FILE__, __LINE__ + 1)
|
67
|
+
@@#{attr} = nil unless defined? @@#{attr}
|
68
|
+
|
69
|
+
def self.#{attr}=(value)
|
70
|
+
@@#{attr} = value
|
71
|
+
end
|
72
|
+
EOS
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
##
|
77
|
+
# Create both reader/writer accessors for the given attributes.
|
78
|
+
#
|
79
|
+
# <i>See cattr_reader and cattr_writer, to learn more.</i>
|
80
|
+
#
|
81
|
+
# <b>Planed to be used by the inherited class.</b>
|
82
|
+
#
|
83
|
+
# === Examples
|
84
|
+
#
|
85
|
+
# Roempro.module_eval do
|
86
|
+
# class SomeClass < Roempro::Class
|
87
|
+
# cattr_reader :var
|
88
|
+
# end
|
89
|
+
# end
|
90
|
+
#
|
91
|
+
# > Roempro::SomeClass.var = "Hello world!"
|
92
|
+
# => "Hello world!"
|
93
|
+
# > Roempro::SomeClass.var
|
94
|
+
# => "Hello world!"
|
95
|
+
def self.cattr_accessor(*attributes)
|
96
|
+
cattr_reader *attributes
|
97
|
+
cattr_writer *attributes
|
98
|
+
end
|
99
|
+
end
|
100
|
+
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
# -*- encoding : UTF-8 -*-
|
2
|
+
|
3
|
+
module Roempro
|
4
|
+
class Config < Roempro::Class
|
5
|
+
cattr_reader :url, :username, :password
|
6
|
+
|
7
|
+
##
|
8
|
+
# Load the default configuration into Roempro::Config.
|
9
|
+
#
|
10
|
+
# Once the default configuration loaded into this object, all new
|
11
|
+
# Roempro's components will use those informations to configure
|
12
|
+
# themself. Unless a specific configuration is provide at creation.
|
13
|
+
#
|
14
|
+
# === Parameters
|
15
|
+
# [Hash]
|
16
|
+
# [:url]
|
17
|
+
# Define the path to the desired Oempro API
|
18
|
+
# [:username]
|
19
|
+
# The username to use for login
|
20
|
+
# [:password]
|
21
|
+
# The user's password
|
22
|
+
def self.load_from_hash(params={})
|
23
|
+
params = Hash[params.map {|k,v| [k.to_sym,v] }]
|
24
|
+
|
25
|
+
@@username = params[:username].to_s if params[:username]
|
26
|
+
@@password = params[:password].to_s if params[:password]
|
27
|
+
@@url = if params[:url]
|
28
|
+
if uri = URI.parse(params[:url]) and
|
29
|
+
uri.instance_of? URI::HTTPS
|
30
|
+
raise ArgumentError, "Roempro::Request doesn't support SSL yet"
|
31
|
+
end
|
32
|
+
if uri.instance_of? URI::Generic
|
33
|
+
"http://" + params[:url].to_s
|
34
|
+
elsif uri.kind_of? URI::HTTP
|
35
|
+
params[:url].to_s
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
return self if @@url or @@username or @@password
|
40
|
+
|
41
|
+
rescue URI::Error, ArgumentError => message
|
42
|
+
puts message
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
@@ -0,0 +1,200 @@
|
|
1
|
+
# -*- encoding : UTF-8 -*-
|
2
|
+
|
3
|
+
require "net/http"
|
4
|
+
require "uri"
|
5
|
+
|
6
|
+
module Roempro
|
7
|
+
##
|
8
|
+
# Entity which actually perform the request to the given Oempro API.
|
9
|
+
#
|
10
|
+
# It give a way to merely deal with the API handling all the undestood
|
11
|
+
# commands. Actually, it build the request chain from the methods names and
|
12
|
+
# the hash submited as argument.
|
13
|
+
#
|
14
|
+
# For instance, to fetch all emails :
|
15
|
+
#
|
16
|
+
# req = Roempro::Request.new
|
17
|
+
# # => #<Roempro::Request>
|
18
|
+
# req.get_emails # Will actually retrieve all emails.
|
19
|
+
class Request < Roempro::Class
|
20
|
+
|
21
|
+
##
|
22
|
+
# Return a new Roempro::Request object
|
23
|
+
#
|
24
|
+
# Paramters are optionals. Those which are missing will
|
25
|
+
# be got from Roempro::Config.
|
26
|
+
#
|
27
|
+
# === Parameters
|
28
|
+
# [Hash]
|
29
|
+
# [:url]
|
30
|
+
# Define the path to the desired Oempro API
|
31
|
+
# [:username]
|
32
|
+
# The username to use for login
|
33
|
+
# [:password]
|
34
|
+
# The user's password
|
35
|
+
def initialize(params={})
|
36
|
+
@username = params[:username].to_s if params[:username]
|
37
|
+
@password = params[:password].to_s if params[:password]
|
38
|
+
@@url = if params[:url]
|
39
|
+
if uri = URI.parse(params[:url]) and
|
40
|
+
uri.instance_of? URI::HTTPS
|
41
|
+
raise ArgumentError, "Roempro::Request doesn't support SSL yet"
|
42
|
+
end
|
43
|
+
if uri.instance_of? URI::Generic
|
44
|
+
"http://" + params[:url].to_s
|
45
|
+
elsif uri.kind_of? URI::HTTP
|
46
|
+
params[:url].to_s
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
if @url or @username or @password
|
51
|
+
@session_id = nil
|
52
|
+
end
|
53
|
+
|
54
|
+
rescue URI::Error, ArgumentError => message
|
55
|
+
puts message
|
56
|
+
end
|
57
|
+
|
58
|
+
##
|
59
|
+
# Fallback to request a command to the Oempro API.
|
60
|
+
#
|
61
|
+
# For the most commands, just call them using the <i>method *to*
|
62
|
+
# command</i> pattern (see method_missing) should work fine. Nevertheless, few command in the
|
63
|
+
# Oempro API just cannot be send by this way, because of how
|
64
|
+
# Roempro::Request compute a called method to a proper command name.
|
65
|
+
#
|
66
|
+
# === Parameters
|
67
|
+
# [String]
|
68
|
+
# The command name, as defined in the Oempro API documentation.
|
69
|
+
# [Hash]
|
70
|
+
# Parameters to submit with the command.
|
71
|
+
def command(command_name, *args)
|
72
|
+
unless args.flatten.compact.empty? or args.flatten.first.kind_of? Hash
|
73
|
+
raise ArgumentError, "#{self.class}##{command_name.to_s} only accept hash argument"
|
74
|
+
end
|
75
|
+
|
76
|
+
login unless logged_in?
|
77
|
+
|
78
|
+
if logged_in?
|
79
|
+
perform (args.flatten.first || {}).merge :command => command_name
|
80
|
+
end
|
81
|
+
|
82
|
+
rescue ArgumentError => message
|
83
|
+
puts message
|
84
|
+
end
|
85
|
+
|
86
|
+
##
|
87
|
+
# Provide to the Object's user a flexible way to command to the Oempro API.
|
88
|
+
#
|
89
|
+
# It work with the following pattern :
|
90
|
+
#
|
91
|
+
# Romepro::Request#given_command
|
92
|
+
# # Use "Command.Given" as command to send to the Oempro API
|
93
|
+
#
|
94
|
+
# === Examples
|
95
|
+
#
|
96
|
+
# > req = Roempro::Request.new
|
97
|
+
# => #<Roempro::Request>
|
98
|
+
# > req.get_emails # GET [...]api.php?command=Emails.Get[...]
|
99
|
+
def method_missing(method_id, *args)
|
100
|
+
unless args.empty? or args.first.kind_of? Hash
|
101
|
+
raise ArgumentError, "#{self.class}##{method_id.to_s} only accept hash argument"
|
102
|
+
end
|
103
|
+
|
104
|
+
command(method_id.to_s.split('_').map(&:capitalize).reverse.join('.'), args)
|
105
|
+
|
106
|
+
rescue ArgumentError => message
|
107
|
+
puts message
|
108
|
+
end
|
109
|
+
|
110
|
+
##
|
111
|
+
# Return the last response answered by the Oempro API
|
112
|
+
#
|
113
|
+
# === Return
|
114
|
+
# Roempro::Response
|
115
|
+
def last_response
|
116
|
+
@last_response
|
117
|
+
end
|
118
|
+
|
119
|
+
##
|
120
|
+
# Tell if Roempro is currently logged in the Oempro API.
|
121
|
+
#
|
122
|
+
# If the current object has been created with a specific url, username or
|
123
|
+
# password else than the default configuration, then, it check the
|
124
|
+
# <i>session id</i> stored within this Roempro::Request object.
|
125
|
+
#
|
126
|
+
# Else, it look for <i>session id</i> kept into Roempro::Base.
|
127
|
+
def logged_in?
|
128
|
+
if defined? @session_id
|
129
|
+
@session_id ? true : false;
|
130
|
+
else
|
131
|
+
Base.session_id ? true : false;
|
132
|
+
end
|
133
|
+
end
|
134
|
+
|
135
|
+
##
|
136
|
+
# Log into the Oempro API
|
137
|
+
#
|
138
|
+
# If the given url, username or password are different from the default
|
139
|
+
# ones, then it use them and the returned <i>session id</i> is stored
|
140
|
+
# within this Roempro::Request object.
|
141
|
+
#
|
142
|
+
# Else, the <i>session id</i> is kept into Roempro::Base
|
143
|
+
def login
|
144
|
+
unless logged_in?
|
145
|
+
unless (@username and @password) or (Config.username and Config.password)
|
146
|
+
raise ArgumentError, "You have to submit your username and password to log into Oempro"
|
147
|
+
end
|
148
|
+
|
149
|
+
perform :command => "User.Login",
|
150
|
+
:username => @username || Config.username,
|
151
|
+
:password => @password || Config.password,
|
152
|
+
:disablecaptcha => true
|
153
|
+
|
154
|
+
unless @last_response.success
|
155
|
+
raise RuntimeError, @last_response.error_text.join("\n")
|
156
|
+
end
|
157
|
+
|
158
|
+
if defined? @session_id
|
159
|
+
@session_id = @last_response.session_id
|
160
|
+
else
|
161
|
+
Base.session_id = @last_response.session_id
|
162
|
+
end
|
163
|
+
end
|
164
|
+
|
165
|
+
rescue ArgumentError => message
|
166
|
+
puts message
|
167
|
+
end
|
168
|
+
|
169
|
+
private
|
170
|
+
|
171
|
+
##
|
172
|
+
# Perform a request to the Oempro API.
|
173
|
+
#
|
174
|
+
# It look for specific url, username or password and use it. Else, it
|
175
|
+
# look for the default configuration.
|
176
|
+
#
|
177
|
+
# === Parameters
|
178
|
+
# [Hash]
|
179
|
+
# Parameters to submit with the command
|
180
|
+
#
|
181
|
+
# === Return
|
182
|
+
# Roempro::Response
|
183
|
+
def perform(query={})
|
184
|
+
unless @url or Config.url
|
185
|
+
raise ArgumentError, "Unable to perform the request : Uknown URL to Oempro"
|
186
|
+
end
|
187
|
+
unless query.nil? or query.kind_of? Hash
|
188
|
+
raise ArgumentError, "Unable to perform the request : params have to be a hash"
|
189
|
+
end
|
190
|
+
|
191
|
+
query ||= {}
|
192
|
+
query.merge! :sessionid => @session_id || Base.session_id, :responseformat => 'JSON'
|
193
|
+
|
194
|
+
uri = URI(@url || Config.url)
|
195
|
+
uri.query = URI::encode_www_form query
|
196
|
+
|
197
|
+
@last_response = Roempro::Response.new(Net::HTTP.get_response(uri))
|
198
|
+
end
|
199
|
+
end
|
200
|
+
end
|
@@ -0,0 +1,67 @@
|
|
1
|
+
# -*- encoding : UTF-8 -*-
|
2
|
+
|
3
|
+
require 'net/http'
|
4
|
+
require 'json'
|
5
|
+
|
6
|
+
module Roempro
|
7
|
+
|
8
|
+
##
|
9
|
+
# Handle the response from Oempro API.
|
10
|
+
#
|
11
|
+
# It allow to easly access to the response parts.
|
12
|
+
#
|
13
|
+
# <b>Only work with JSON formated response!</b>
|
14
|
+
class Response < Roempro::Class
|
15
|
+
|
16
|
+
##
|
17
|
+
# Create a new Roempro::Response object.
|
18
|
+
#
|
19
|
+
# === Parameters
|
20
|
+
# <b>URI::HTTPResponse</b>
|
21
|
+
def initialize(http_response)
|
22
|
+
unless http_response.kind_of? Net::HTTPResponse
|
23
|
+
raise ArgumentError, "#{self.class}#new only support Net::HTTPResponse as input"
|
24
|
+
end
|
25
|
+
|
26
|
+
@response = JSON.parse(http_response.body)
|
27
|
+
@response['HttpSuccess'] = http_response.is_a? Net::HTTPSuccess
|
28
|
+
@response = Hash[@response.map { |k,v| [k.downcase, v] }]
|
29
|
+
|
30
|
+
rescue ArgumentError => message
|
31
|
+
puts message
|
32
|
+
rescue JSON::ParserError
|
33
|
+
# Report the exception message ?
|
34
|
+
puts "JSON parser fail to compute the API's response"
|
35
|
+
end
|
36
|
+
|
37
|
+
##
|
38
|
+
# As well as Roempro::Request, Roempro::Response return a response part
|
39
|
+
# according the called method.
|
40
|
+
#
|
41
|
+
# The followed pattern is simple. To access to a response part just call
|
42
|
+
# it.
|
43
|
+
#
|
44
|
+
# ==== Examples
|
45
|
+
#
|
46
|
+
# > req = Roempro::Request.new
|
47
|
+
# => #<Roempro::Request>
|
48
|
+
# > res = req.get_emails
|
49
|
+
# => #<Roempro::Response @response={"success"=>true,
|
50
|
+
# # "errorcode"=>0, "errortext"=>"", "totalemailcount"=>1,
|
51
|
+
# # "emails"=>[{"EmailID"=>"1"[...]}], "httpsuccess"=>true}>
|
52
|
+
# > res.success
|
53
|
+
# => true
|
54
|
+
# > res.totalemailcount
|
55
|
+
# => 1
|
56
|
+
def method_missing(method_id, *args)
|
57
|
+
if args.any?
|
58
|
+
raise ArgumentError, "#{self.class}##{method_id.to_s} doesn't accept any agument"
|
59
|
+
end
|
60
|
+
|
61
|
+
@response[method_id.to_s.split('_').join('')]
|
62
|
+
|
63
|
+
rescue ArgumentError => message
|
64
|
+
puts message
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
data/lib/roempro.rb
ADDED
data/roempro.gemspec
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
lib = File.expand_path('../lib', __FILE__)
|
4
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
5
|
+
|
6
|
+
require 'roempro/version'
|
7
|
+
|
8
|
+
Gem::Specification.new do |gem|
|
9
|
+
gem.name = "roempro"
|
10
|
+
gem.version = Roempro::VERSION
|
11
|
+
gem.authors = ["Nilsine", "Révillon Loïk", "Jean-philippe Lannoy"]
|
12
|
+
gem.email = ["info@nilsine.fr"]
|
13
|
+
gem.description = "Deal with a given Oempro application API. Handle about subscribers, campaigns and emails"
|
14
|
+
gem.summary = "Ruby wrapper for Oempro API"
|
15
|
+
gem.homepage = ""
|
16
|
+
|
17
|
+
gem.files = `git ls-files`.split($/)
|
18
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
19
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
20
|
+
gem.require_paths = ["lib"]
|
21
|
+
|
22
|
+
gem.has_rdoc = true
|
23
|
+
end
|
metadata
ADDED
@@ -0,0 +1,66 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: roempro
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Nilsine
|
9
|
+
- Révillon Loïk
|
10
|
+
- Jean-philippe Lannoy
|
11
|
+
autorequire:
|
12
|
+
bindir: bin
|
13
|
+
cert_chain: []
|
14
|
+
date: 2012-10-24 00:00:00.000000000 Z
|
15
|
+
dependencies: []
|
16
|
+
description: Deal with a given Oempro application API. Handle about subscribers, campaigns
|
17
|
+
and emails
|
18
|
+
email:
|
19
|
+
- info@nilsine.fr
|
20
|
+
executables: []
|
21
|
+
extensions: []
|
22
|
+
extra_rdoc_files: []
|
23
|
+
files:
|
24
|
+
- .gitignore
|
25
|
+
- CHANGELOG.md
|
26
|
+
- Gemfile
|
27
|
+
- LICENSE.txt
|
28
|
+
- README.md
|
29
|
+
- README.rdoc
|
30
|
+
- Rakefile
|
31
|
+
- lib/rails/generators/roempro/install_generator.rb
|
32
|
+
- lib/rails/generators/roempro/templates/initializer.rb
|
33
|
+
- lib/rails/generators/roempro/templates/roempro.yml
|
34
|
+
- lib/roempro.rb
|
35
|
+
- lib/roempro/base.rb
|
36
|
+
- lib/roempro/class.rb
|
37
|
+
- lib/roempro/config.rb
|
38
|
+
- lib/roempro/request.rb
|
39
|
+
- lib/roempro/response.rb
|
40
|
+
- lib/roempro/version.rb
|
41
|
+
- roempro.gemspec
|
42
|
+
homepage: ''
|
43
|
+
licenses: []
|
44
|
+
post_install_message:
|
45
|
+
rdoc_options: []
|
46
|
+
require_paths:
|
47
|
+
- lib
|
48
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
55
|
+
none: false
|
56
|
+
requirements:
|
57
|
+
- - ! '>='
|
58
|
+
- !ruby/object:Gem::Version
|
59
|
+
version: '0'
|
60
|
+
requirements: []
|
61
|
+
rubyforge_project:
|
62
|
+
rubygems_version: 1.8.24
|
63
|
+
signing_key:
|
64
|
+
specification_version: 3
|
65
|
+
summary: Ruby wrapper for Oempro API
|
66
|
+
test_files: []
|