mandao 0.0.1 → 0.0.2pre

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 58644bbb6aba2476b05b6d702a384f2fc9c1b06e
4
- data.tar.gz: 76c812d8c68f7a8d8dcced5436c6b89ebb9ff03b
3
+ metadata.gz: 4be4b5ae37269795d42e7b34ba1f9e1ddab0f0a1
4
+ data.tar.gz: 5cd84033120c48876de7f6f0ce53aa6be32ef31c
5
5
  SHA512:
6
- metadata.gz: 917a65ae5842d0d3f262a70cf7a15d1eb9fdc879199c3989abab22b9f31575ce8244b767cebe1a471323a68d0e98a04b334fae9d5042822664dd445d475a3c19
7
- data.tar.gz: b399a72a298c4855dfe79370e61e6df3781aa01ef5233297ac3ac1fdcebb9f66806fc5e019a438573433f2aa93c1eb66f626bc554bbb0972292785ba6eb89146
6
+ metadata.gz: fe717b0cb9ea9249bef8d4dabd5dc4993cc00a1d14094e570af80ea49868fd6600f4f6cbca389fd6e7b579a3c31b371fabdbf86ab5ed067e0c23f6c98aeb2471
7
+ data.tar.gz: 2f154fe98a9e903fba86e928e10793b0f032fc22d5f53e078e3dd67bd5bff5d630ef8f28baff23141fbc9c0191f3a9e27737f0921426ee201e39ca69f0dc3d62
data/.gitignore CHANGED
@@ -22,6 +22,7 @@ mkmf.log
22
22
  /test/tmp/
23
23
  /test/version_tmp/
24
24
  /tmp/
25
+ /push/
25
26
 
26
27
  ## Specific to RubyMotion:
27
28
  .dat*
@@ -38,6 +39,7 @@ build/
38
39
  /.bundle/
39
40
  /lib/bundler/man/
40
41
 
42
+
41
43
  # for a library or gem, you might want to ignore these files since the code is
42
44
  # intended to run in multiple environments; otherwise, check them in:
43
45
  # Gemfile.lock
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --require spec_helper
@@ -0,0 +1,49 @@
1
+
2
+ module Mandao
3
+
4
+ class << self
5
+ attr_accessor :configuration
6
+ end
7
+
8
+ def self.configure
9
+ self.configuration ||= Configuration.new
10
+ yield(configuration)
11
+ Mandao::Base.activate
12
+ end
13
+
14
+ def self.config
15
+ self.configuration
16
+ end
17
+
18
+ class Configuration
19
+ attr_accessor :endpoint
20
+ attr_accessor :username
21
+ attr_accessor :password
22
+ attr_accessor :protocol
23
+ attr_accessor :format
24
+
25
+ VALID_OPTIONS_KEYS = [:endpoint, :username, :password]
26
+ REQUIRED_OPTIONS = [:username, :password]
27
+ DEFAULT_PROTOCOL = "https".freeze
28
+ DEFAULT_ENDPOINT = 'stage.rest.click2mail.com/v1'.freeze # TODO [ add production endpoint by default ]
29
+ DEFAULT_FORMAT = ActiveResource::Formats::XmlFormat
30
+
31
+ def initialize
32
+ @endpoint = DEFAULT_ENDPOINT
33
+ @protocol = DEFAULT_PROTOCOL
34
+ @format = DEFAULT_FORMAT
35
+ end
36
+
37
+ def api_endpoint
38
+ "#{@protocol}://#{@username}:#{@password}@#{@endpoint}/"
39
+ end
40
+
41
+ def valid?
42
+ REQUIRED_OPTIONS.each do |opt|
43
+ raise StandardError, 'You must provide username and password.' if send(opt)
44
+ end
45
+ end
46
+
47
+ end
48
+
49
+ end
@@ -0,0 +1,35 @@
1
+ module Mandao
2
+ class Address
3
+ attr_accessor :first_name, :last_name, :organization, :address1,
4
+ :address2, :address3, :city, :state, :zip, :country_non_us
5
+
6
+ def initialize(options={})
7
+ @first_name = options[:first_name]
8
+ @last_name = options[:last_name]
9
+ @organization = options[:organization]
10
+ @address1 = options[:address1]
11
+ @address2 = options[:address2]
12
+ @address3 = options[:address3]
13
+ @city = options[:city]
14
+ @state = options[:state]
15
+ @zip = options[:zip]
16
+ @country_non_us = options[:country_non_us]
17
+ end
18
+
19
+ def to_xml
20
+ "<address>
21
+ <First_name>#{ @first_name }</First_name>
22
+ <Last_Name>#{ @last_name }</Last_Name>
23
+ <Organization>#{ @organization }</Organization>
24
+ <Address1>#{ @address1 }</Address1>
25
+ <Address2>#{ @address2 }</Address2>
26
+ <Address3>#{ @address3 }</Address3>
27
+ <City>#{@city}</City>
28
+ <State>#{@state}</State>
29
+ <Zip>#{@zip}</Zip>
30
+ <Country_non-US>#{@country_non_us}</Country_non-US>
31
+ </address>"
32
+ end
33
+
34
+ end
35
+ end
@@ -0,0 +1,49 @@
1
+ module Mandao
2
+
3
+ class AddressListCollection < ActiveResource::Collection
4
+ def initialize(parsed = {})
5
+ attrs = []
6
+ attrs << parsed['addressList'] if parsed['addressList'].is_a? Hash
7
+ @elements = attrs || parsed['addressList']
8
+ end
9
+ end
10
+
11
+ class AddressList < Base
12
+ self.collection_name = "addressLists"
13
+ self.collection_parser = Mandao::AddressListCollection
14
+
15
+ class << self
16
+
17
+ def create(list_name, addresses=[])
18
+ if list_name && addresses
19
+ xml = generate_xml(addresses)
20
+ begin
21
+ response = RestClient.post("#{self.site}#{ self.collection_name }", xml, content_type: :xml )
22
+ attrs = parse_response(response)
23
+ Mandao::AddressList.new(attrs)
24
+ rescue
25
+ end
26
+ else
27
+ raise StandardError, 'Address List must contain a name and at least one address.'
28
+ end
29
+ end
30
+
31
+ def generate_xml(addresses)
32
+ addresses_xml = ""
33
+ addresses.each do |address_attrs|
34
+ addresses_xml += Mandao::Address.new(address_attrs).to_xml
35
+ end
36
+ "<addressList>
37
+ <addressListName>Best Customers test</addressListName>
38
+ <addressMappingId>2</addressMappingId>
39
+ <addresses>
40
+ #{ addresses_xml }
41
+ </addresses>
42
+ </addressList>"
43
+ end
44
+
45
+ end
46
+
47
+ end
48
+
49
+ end
@@ -0,0 +1,19 @@
1
+ module Mandao
2
+ class Base < ActiveResource::Base
3
+ self.include_format_in_path = false
4
+
5
+ class << self
6
+
7
+ def activate
8
+ self.site = Mandao.config.api_endpoint
9
+ self.format = Mandao.config.format
10
+ end
11
+
12
+ def parse_response(xml)
13
+ ActiveResource::Formats::XmlFormat.decode(xml)
14
+ end
15
+
16
+ end
17
+
18
+ end
19
+ end
@@ -0,0 +1,24 @@
1
+ module Mandao
2
+
3
+ class DocumentCollection < ActiveResource::Collection
4
+ def initialize(parsed = {})
5
+ attrs = []
6
+ attrs << parsed['document'] if parsed['document'].is_a? Hash
7
+ @elements = attrs || parsed['document']
8
+ end
9
+ end
10
+
11
+ class Document < Base
12
+ self.collection_parser = Mandao::DocumentCollection
13
+
14
+ class << self
15
+ def create(file, content_type='application/pdf')
16
+ response = RestClient.post("#{self.site}documents", file, content_type: content_type, accept: 'application/xml')
17
+ attrs = parse_response(response)
18
+ Mandao::Document.new(attrs)
19
+ end
20
+ end
21
+
22
+ end
23
+
24
+ end
@@ -1,3 +1,3 @@
1
1
  module Mandao
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2pre"
3
3
  end
@@ -0,0 +1,16 @@
1
+ module ActiveResource
2
+ module Formats
3
+ module XmlFormat
4
+
5
+ def decode(xml)
6
+ data = Hash.from_xml(xml)
7
+ if data.is_a?(Hash) && data.keys.size == 1
8
+ data.values.first
9
+ else
10
+ data
11
+ end
12
+ end
13
+
14
+ end
15
+ end
16
+ end
data/lib/mandao.rb CHANGED
@@ -1,5 +1,2 @@
1
- require "mandao/version"
2
-
3
- module Mandao
4
- # Your code goes here...
5
- end
1
+ require 'mandao/version'
2
+ require 'mandao/configuration'
data/mandao.gemspec CHANGED
@@ -9,7 +9,7 @@ Gem::Specification.new do |spec|
9
9
  spec.authors = ["Marlon Mantilla"]
10
10
  spec.email = ["me@marlonmantilla.com"]
11
11
  spec.summary = %q{Mandao is a cool gem for accessing the Click2Mail REST API. https://developers.click2mail.com/rest-api/molpro/getting-started/main}
12
- spec.description = %q{Mandao gem allows developers to access Click2Mail REST services in a simple way.}
12
+ spec.description = %q{Mandao allows you to access Click2Mail REST services in a simple way.}
13
13
  spec.homepage = ""
14
14
  spec.license = "MIT"
15
15
 
@@ -18,7 +18,12 @@ Gem::Specification.new do |spec|
18
18
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
19
  spec.require_paths = ["lib"]
20
20
 
21
+ spec.add_runtime_dependency "activeresource", "~> 4.0"
22
+ spec.add_runtime_dependency "rest-client", "~> 1.7"
23
+
21
24
  spec.add_development_dependency "bundler", "~> 1.7"
22
25
  spec.add_development_dependency "rake", "~> 10.0"
23
- spec.add_development_dependency "rspec", "~> 3.1.0"
26
+ spec.add_development_dependency "rspec", "~> 3.1"
27
+ spec.add_development_dependency "webmock", "~> 1.19"
28
+
24
29
  end
@@ -0,0 +1,37 @@
1
+ require File.expand_path('../../spec_helper', __FILE__)
2
+
3
+ describe Mandao::AddressList do
4
+
5
+ before do
6
+ Mandao.configure do |config|
7
+ config.endpoint = Mandao::Configuration::DEFAULT_ENDPOINT
8
+ config.username = "username"
9
+ config.password = "password"
10
+ end
11
+ stub_address_lists
12
+ stub_address_lists_post
13
+ end
14
+
15
+ it 'should return all address lists' do
16
+ address_lists = Mandao::AddressList.all
17
+ expect(address_lists).to be_present
18
+ end
19
+
20
+ it 'should create an address list' do
21
+ address_lists = [{
22
+ first_name: 'John',
23
+ last_name: 'Doe',
24
+ organization: 'Umbrella',
25
+ address1: '742 Evergreen Terrace',
26
+ address2: '743 Evergreen Terrace',
27
+ address3: '',
28
+ city: 'Springfield',
29
+ state: 'MA',
30
+ zip: '12345',
31
+ country_non_us: ''
32
+ }]
33
+ address_list = Mandao::AddressList.create("My Address List", address_lists)
34
+ expect(address_list).to be_present
35
+ end
36
+
37
+ end
@@ -0,0 +1,26 @@
1
+ require File.expand_path('../../spec_helper', __FILE__)
2
+
3
+ describe Mandao::Document do
4
+
5
+ before do
6
+ Mandao.configure do |config|
7
+ config.endpoint = Mandao::Configuration::DEFAULT_ENDPOINT
8
+ config.username = "username"
9
+ config.password = "password"
10
+ end
11
+ stub_documents
12
+ stub_document_post
13
+ end
14
+
15
+ it 'should return all documents' do
16
+ documents = Mandao::Document.all
17
+ expect(documents).to be_present
18
+ end
19
+
20
+ it 'should create a document with a pdf' do
21
+ pdf = File.open(File.expand_path('../files/document.pdf', __FILE__), 'rb')
22
+ document = Mandao::Document.create(pdf)
23
+ expect(document).to be_present
24
+ end
25
+
26
+ end