emailvision 1.1.0 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/lib/emailvision.rb CHANGED
@@ -1,16 +1,18 @@
1
1
  require 'crack/xml'
2
2
  require 'httparty'
3
+ require 'active_support/inflector'
4
+ require 'logger'
3
5
 
4
6
  # Emailvision API wrapper
5
- #
6
- # More infos @ http://emvapi.emv3.com/apiccmd/services/rest?_wadl&_type=xml
7
- #
8
- # Call example :
9
- # resource path="url/createUnsubscribeUrl/" --> url.create_unsubscribe_url.call :params => value1
10
7
  #
11
8
  module Emailvision
12
9
  autoload :Api, 'emailvision/api'
13
10
  autoload :Exception, 'emailvision/exception'
14
11
  autoload :Logger, 'emailvision/logger'
15
12
  autoload :Relation, 'emailvision/relation'
13
+
14
+ if defined?(Rails)
15
+ require 'emailvision/railtie'
16
+ require 'generators/install'
17
+ end
16
18
  end
@@ -2,89 +2,130 @@ module Emailvision
2
2
  class Api
3
3
  include HTTParty
4
4
  default_timeout 30
5
- format :plain
6
-
7
- attr_accessor :token
5
+ format :xml
8
6
 
9
- HTTP_VERBS = [:get, :post]
10
-
11
- def initialize(login = nil, password = nil, key = nil, params = {})
12
- @login = login || EMV_LOGIN || ENV['EMV_LOGIN']
13
- @password = password || EMV_PASSWORD || ENV['EMV_PASSWORD']
14
- @key = key || EMV_KEY || ENV['EMV_KEY']
7
+ # HTTP verbs allowed to trigger a call-chain
8
+ HTTP_VERBS = [:get, :post].freeze
15
9
 
16
- @default_params = params
17
- end
18
-
19
- HTTP_VERBS.each do |http_verb|
20
- define_method(http_verb) do
21
- Emailvision::Relation.new(self, http_verb)
22
- end
10
+ # Attributes
11
+ class << self
12
+ attr_accessor :token, :server_name, :endpoint, :login, :password, :key, :debug
23
13
  end
14
+ attr_accessor :token, :server_name, :endpoint, :login, :password, :key, :debug
24
15
 
25
- # http_verb = (get, post, ...)
26
- # method = Method to call
27
- # params = Extra parameters (optionnal)
28
- def call(http_verb, method, params = {})
29
- params ||= {}
30
-
31
- # Build uri and parameters
32
- uri = endpoint + method
33
- params = @default_params.merge params
16
+ def initialize(params = {})
17
+ yield(self) if block_given?
34
18
 
35
- # Send request
36
- raw_result = self.class.send http_verb, uri, :query => params
37
-
38
- # Parse response
39
- http_code = raw_result.header.code
40
- parsed_result = Crack::XML.parse raw_result.body
41
-
42
- # Return response or raise an exception if request failed
43
- if (http_code == "200") and (parsed_result and parsed_result["response"])
44
- parsed_result["response"]["result"] || parsed_result["response"]
45
- else
46
- raise Emailvision::Exception.new "#{http_code} - #{raw_result.body}"
47
- end
48
- end
49
-
50
- # Connection token
51
- def token=(value)
52
- @token = value
53
- @default_params = @default_params.merge({:token => token})
54
- end
55
-
56
- # Endpoint base uri
57
- def endpoint
58
- 'https://emvapi.emv3.com/apiccmd/services/rest/'
59
- end
60
-
19
+ self.server_name ||= params[:server_name] || self.class.server_name
20
+ self.endpoint ||= params[:endpoint] || self.class.endpoint
21
+ self.login ||= params[:login] || self.class.login
22
+ self.password ||= params[:password] || self.class.password
23
+ self.key ||= params[:key] || self.class.key
24
+ self.token ||= params[:token] || self.class.token
25
+ self.debug ||= params[:debug] || self.class.debug
26
+ end
27
+
61
28
  # ----------------- BEGIN Pre-configured methods -----------------
62
29
 
63
30
  # Login to Emailvision API
64
31
  # Return :
65
32
  # - True if the connection has been established
66
33
  # - False if the connection cannot be established or has already been established
67
- def login
34
+ def open_connection
68
35
  return false if connected?
69
36
  self.token = get.connect.open.call :login => @login, :password => @password, :key => @key
70
37
  connected?
71
38
  end
72
39
 
73
- # Logout to Emailvision API
40
+ # Logout from Emailvision API
74
41
  # Return :
75
42
  # - True if the connection has been destroyed
76
43
  # - False if the connection cannot be destroyed or has already been destroyed
77
- def logout
44
+ def close_connection
78
45
  return false unless connected?
79
46
  get.connect.close.call if connected?
80
- self.token = nil
81
- connected?
47
+ invalidate_token!
48
+ not connected?
82
49
  end
83
50
 
84
51
  # Check whether the connection has been established or not
85
52
  def connected?
86
53
  !token.nil?
87
54
  end
88
- # ----------------- END Pre-configured methods -----------------
89
- end
55
+
56
+ def invalidate_token!
57
+ self.token = nil
58
+ end
59
+ # ----------------- END Pre-configured methods -----------------
60
+
61
+ # Perform the API call
62
+ # http_verb = (get, post, ...)
63
+ # method = Method to call
64
+ # content = Content to send (optionnal)
65
+ def call(http_verb, method, content = {})
66
+ params ||= {}
67
+
68
+ # Check presence of these essential attributes
69
+ unless server_name and endpoint
70
+ raise Emailvision::Exception.new "Cannot make an API call without a server name and an endpoint !"
71
+ end
72
+
73
+ # Build uri and parameters
74
+ uri = base_uri + method
75
+ query = token ? {:token => token} : {}
76
+ query.merge! content
77
+
78
+ # Send request
79
+ logger.send "#{uri} with content : #{content}"
80
+ response = self.class.send http_verb, uri, :query => query
81
+
82
+ # Parse response
83
+ http_code = response.header.code
84
+ content = Crack::XML.parse response.body
85
+ logger.receive content.inspect
86
+
87
+ # Return response or raise an exception if request failed
88
+ if (http_code == "200") and (content and content["response"])
89
+ content["response"]["result"] || content["response"]
90
+ else
91
+ raise Emailvision::Exception.new "#{http_code} - #{content}"
92
+ end
93
+ end
94
+
95
+ # Set token
96
+ # Override
97
+ def token=(value)
98
+ @token = value
99
+ end
100
+
101
+ # Set endpoint
102
+ # Override
103
+ def endpoint=(value)
104
+ invalidate_token!
105
+ @endpoint = value
106
+ end
107
+
108
+ # Base uri
109
+ def base_uri
110
+ "http://#{server_name}/#{endpoint}/services/rest/"
111
+ end
112
+
113
+ # Generate call-chain triggers
114
+ HTTP_VERBS.each do |http_verb|
115
+ define_method(http_verb) do
116
+ Emailvision::Relation.new(self, http_verb)
117
+ end
118
+ end
119
+
120
+ private
121
+
122
+ def logger
123
+ if @logger.nil?
124
+ @logger = Emailvision::Logger.new(STDOUT)
125
+ @logger.level = (debug ? Logger::DEBUG : Logger::WARN)
126
+ end
127
+ @logger
128
+ end
129
+
130
+ end
90
131
  end
@@ -1,4 +1,14 @@
1
1
  module Emailvision
2
- class Exception < ::Exception
2
+ class Exception < ::Exception
3
+
4
+ attr_accessor :http_status, :error
5
+
6
+ def initializer(http_status, error)
7
+ self.http_status = http_status
8
+ self.error = error
9
+
10
+ super("EMV API returns #{http_status} status code")
11
+ end
12
+
3
13
  end
4
14
  end
@@ -1,6 +1,13 @@
1
1
  module Emailvision
2
2
  class Logger < ::Logger
3
3
 
4
+ attr_accessor :debug
5
+
6
+ def initializer(*args)
7
+ debug = false
8
+ super args
9
+ end
10
+
4
11
  # Log a message sent to emailvision
5
12
  def send(message)
6
13
  info("[Emailvision] Send -> #{message}")
@@ -0,0 +1,26 @@
1
+ require 'rails'
2
+
3
+ module Emailvision
4
+ class Railtie < Rails::Railtie
5
+
6
+ generators do
7
+ require 'generators/install'
8
+ end
9
+
10
+ config.to_prepare do
11
+ file = "#{Rails.root}/config/emailvision.yml"
12
+
13
+ if File.exist?(file)
14
+ config = YAML.load_file(file)[Rails.env] || {}
15
+
16
+ Emailvision::Api.server_name = config['server_name']
17
+ Emailvision::Api.endpoint = config['endpoint']
18
+ Emailvision::Api.login = config['login']
19
+ Emailvision::Api.password = config['password']
20
+ Emailvision::Api.key = config['key']
21
+ Emailvision::Api.debug = config['debug']
22
+ end
23
+ end
24
+
25
+ end
26
+ end
@@ -5,17 +5,17 @@ module Emailvision
5
5
  @instance = instance
6
6
  @http_verb = http_verb
7
7
  @uri = []
8
- @params = {}
8
+ @options = {}
9
9
  end
10
10
 
11
11
  def call(*args)
12
- @params.merge! extract_args(args)
13
- @instance.call @http_verb, @uri.join('/'), @params
12
+ @options.merge! extract_args(args)
13
+ @instance.call @http_verb, @uri.join('/'), @options
14
14
  end
15
15
 
16
16
  def method_missing(method, *args)
17
17
  @uri << method.to_s.camelize(:lower)
18
- @params.merge! extract_args(args)
18
+ @options.merge! extract_args(args)
19
19
  self
20
20
  end
21
21
 
@@ -0,0 +1,15 @@
1
+ require 'rails/generators'
2
+
3
+ module Emailvision
4
+ module Generators
5
+ class Install < Rails::Generators::Base
6
+
7
+ source_root File.expand_path('../templates', __FILE__)
8
+
9
+ def generate_config
10
+ copy_file "emailvision.yml", "config/emailvision.yml" unless File.exist?("config/emailvision.yml")
11
+ end
12
+
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,15 @@
1
+ development:
2
+ server_name:
3
+ login:
4
+ password:
5
+ key:
6
+ production:
7
+ server_name:
8
+ login:
9
+ password:
10
+ key:
11
+ test:
12
+ server_name:
13
+ login:
14
+ password:
15
+ key:
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: emailvision
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 2.0.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -13,7 +13,7 @@ date: 2011-09-26 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: httparty
16
- requirement: &2164322100 !ruby/object:Gem::Requirement
16
+ requirement: &2164271140 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ~>
@@ -21,18 +21,18 @@ dependencies:
21
21
  version: 0.8.0
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *2164322100
24
+ version_requirements: *2164271140
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: crack
27
- requirement: &2164321620 !ruby/object:Gem::Requirement
27
+ requirement: &2164270660 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ~>
31
31
  - !ruby/object:Gem::Version
32
- version: 0.1.8
32
+ version: 0.3.0
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *2164321620
35
+ version_requirements: *2164270660
36
36
  description: REST API wrapper interacting with Emailvision
37
37
  email: basgys@gmail.com
38
38
  executables: []
@@ -42,8 +42,11 @@ files:
42
42
  - lib/emailvision/api.rb
43
43
  - lib/emailvision/exception.rb
44
44
  - lib/emailvision/logger.rb
45
+ - lib/emailvision/railtie.rb
45
46
  - lib/emailvision/relation.rb
46
47
  - lib/emailvision.rb
48
+ - lib/generators/install.rb
49
+ - lib/generators/templates/emailvision.yml
47
50
  homepage: http://github.com/basgys/emailvision
48
51
  licenses: []
49
52
  post_install_message: