highrise_mapper 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,3 @@
1
+ .DS_Store
2
+ pkg
3
+ tmp
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color --format documentation
data/.travis.yml ADDED
@@ -0,0 +1,3 @@
1
+ rvm:
2
+ - 2.0.0
3
+ - 1.9.3
data/Gemfile ADDED
@@ -0,0 +1,2 @@
1
+ source "https://rubygems.org"
2
+ gemspec
data/Rakefile ADDED
@@ -0,0 +1,7 @@
1
+ require "bundler"
2
+ Bundler::GemHelper.install_tasks
3
+
4
+ require "rspec/core/rake_task"
5
+ RSpec::Core::RakeTask.new
6
+
7
+ task :default => :build
data/Readme.md ADDED
@@ -0,0 +1,82 @@
1
+ [![Code Climate](https://codeclimate.com/github/lucasmartins/highrise-mapper.png)](https://codeclimate.com/github/lucasmartins/highrise-mapper) [![Build Status](https://secure.travis-ci.org/lucasmartins/highrise-mapper.png?branch=master)](https://travis-ci.org/lucasmartins/highrise-mapper) [![Dependency Status](https://gemnasium.com/lucasmartins/highrise-mapper.png)](https://gemnasium.com/lucasmartins/highrise-mapper)
2
+
3
+ Highrise Mapper
4
+ ===============
5
+
6
+ This is a **WORK IN PROGRESS**
7
+
8
+ This Gem depends heavily on the [Highrise](https://github.com/tapajos/highrise) Gem, and although you are encouraged to use it with Rails, I made it to be framework independent.
9
+
10
+ This Gem adds [Highrise](https://github.com/tapajos/highrise) behavior to your models so you can call `@person.save_to_highrise`
11
+
12
+ Install
13
+ =======
14
+
15
+ You can:
16
+ ```
17
+ $ gem install highrise-mapper
18
+ ```
19
+
20
+ Or just add it to your Gemfile
21
+ ```
22
+ gem 'highrise-mapper'
23
+ ```
24
+
25
+ Use
26
+ ===
27
+
28
+ ### Configuration file
29
+ If you are on Rails, run the following command to generate a config file:
30
+
31
+ `$ rails g highrise_mapper:config`
32
+
33
+ This file maps between the fields Highrise expects and the fields/methods your model has, like this.
34
+
35
+ ```yml
36
+ #highrise_key: Model key
37
+ person:
38
+ first_name: name
39
+ last_name: last_name
40
+ company_name: company_name
41
+ email_address: email
42
+ ```
43
+
44
+ In this example, you could have a Person class like this one:
45
+
46
+ ```ruby
47
+ class Person
48
+ def name
49
+ 'Albert'
50
+ end
51
+
52
+ def last_name
53
+ 'Einstein'
54
+ end
55
+
56
+ def company_name
57
+ self.company.name
58
+ end
59
+
60
+ def email
61
+ 'albert.einstein@princeton.edu'
62
+ end
63
+ end
64
+ ```
65
+ Notice that this implementation is database agnostic, there is no database reference whatsoever.
66
+
67
+ Check the specs to see the testing example.
68
+
69
+ Contribute
70
+ ==========
71
+
72
+ Just fork [HighriseMapper](https://github.com/lucasmartins/highrise-mapper), add your feature+spec, and make a pull request.
73
+
74
+ Support
75
+ =======
76
+
77
+ This is an opensource project so don't expect premium support, but don't be shy, post any troubles you're having in the [Issues](https://github.com/lucasmartins/highrise-mapper/issues) page and we'll do what we can to help.
78
+
79
+ License
80
+ =======
81
+
82
+ Tacape Tools is free software under the [MIT license](http://lucasmartins.mit-license.org).
@@ -0,0 +1 @@
1
+ en:
@@ -0,0 +1 @@
1
+ pt-BR:
@@ -0,0 +1,28 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "highrise_mapper/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "highrise_mapper"
7
+ s.version = HighriseMapper::Version::STRING
8
+ s.platform = Gem::Platform::RUBY
9
+ s.required_ruby_version = ">= 1.9.3"
10
+ s.authors = ["Lucas Martins"]
11
+ s.email = ["lucasmartins@railsnapraia.com"]
12
+ s.homepage = "http://rubygems.org/gems/highrise-mapper"
13
+ s.summary = "Experimental Gem"
14
+ s.description = s.summary
15
+ s.license = "MIT"
16
+
17
+ s.files = `git ls-files`.split("\n")
18
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
19
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
20
+ s.require_paths = ["lib"]
21
+
22
+ s.add_dependency "highrise-rails4"
23
+
24
+ s.add_development_dependency "rspec"
25
+ s.add_development_dependency "rake"
26
+ s.add_development_dependency "pry"
27
+ s.add_development_dependency "pry-nav"
28
+ end
@@ -0,0 +1,8 @@
1
+ Description:
2
+ Creates a sample highrise_mapper.yml
3
+
4
+ Example:
5
+ rails generate highrise_mapper:config
6
+
7
+ This will create:
8
+ config/highrise_mapper.yml
@@ -0,0 +1,10 @@
1
+ module HighriseMapper
2
+ class ConfigGenerator < Rails::Generators::Base
3
+ source_root File.expand_path('../../../../templates/config', __FILE__)
4
+
5
+ def create_config_file
6
+ config_yml = 'highrise_mapper.yml'
7
+ copy_file config_yml, "config/#{config_yml}"
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,32 @@
1
+ module HighriseMapper
2
+ module Context
3
+
4
+ module ClassMethods
5
+ end
6
+
7
+ module InstanceMethods
8
+ def setup_highrise
9
+ Highrise::Base.site = self.highrise_base_url
10
+ Highrise::Base.user = self.highrise_token
11
+ Highrise::Base.format = :xml
12
+ end
13
+ end
14
+
15
+ def self.included(receiver)
16
+ receiver.extend ClassMethods
17
+ receiver.send :include, InstanceMethods
18
+ check_expected_behavior(receiver)
19
+ end
20
+
21
+ private
22
+ def self.check_expected_behavior(receiver)
23
+ unless receiver.method_defined? 'highrise_base_url'
24
+ raise 'Your model must respond to "highrise_base_url"'
25
+ end
26
+ unless receiver.method_defined? 'highrise_token'
27
+ raise 'Your model must respond to "highrise_token"'
28
+ end
29
+ end
30
+ end
31
+
32
+ end
@@ -0,0 +1,69 @@
1
+ module HighriseMapper
2
+ module Person
3
+
4
+ module ClassMethods
5
+ end
6
+
7
+ module InstanceMethods
8
+ def save_to_highrise
9
+ unless HighriseMapper.config['person'].is_a? Hash
10
+ raise 'Your highrise_mapper.yml does not contain the "person" configuration key.'
11
+ end
12
+ self.highrise_context.setup_highrise
13
+ highrise_person = Highrise::Person.new(build_highrise_hash)
14
+
15
+ begin
16
+ highrise_person.save!
17
+ rescue Exception => e
18
+ #shoud get rails logger if available
19
+ puts highrise_person.errors
20
+ end
21
+ return highrise_person.attributes['id']
22
+ end
23
+
24
+ def delete_from_highrise(force_id=nil)
25
+ if force_id!=nil
26
+ highrise_id = force_id
27
+ else
28
+ highrise_id = self.highrise_id
29
+ end
30
+ self.highrise_context.setup_highrise
31
+ highrise_person = Highrise::Person.find(highrise_id)
32
+ highrise_person.destroy
33
+ end
34
+
35
+ def build_highrise_hash
36
+ new_hash = {}
37
+ config = HighriseMapper.config
38
+ config['person'].each do |k,v|
39
+ if v!=nil && v!=''
40
+ case k
41
+ when 'email_address'
42
+ new_hash['contact_data']={'email_addresses'=>[{'address'=> self.send(v),'location'=>''}]}
43
+ else
44
+ new_hash[k]=self.send(v)
45
+ end
46
+ end
47
+ end
48
+ return new_hash
49
+ end
50
+ end
51
+
52
+ def self.included(receiver)
53
+ receiver.extend ClassMethods
54
+ receiver.send :include, InstanceMethods
55
+ check_expected_behavior(receiver)
56
+ end
57
+
58
+ private
59
+ def self.check_expected_behavior(receiver)
60
+ unless receiver.method_defined? 'highrise_context'
61
+ raise 'Your model must respond to "highrise_context", where it returns the Instace of the HighriseMapper::Context model'
62
+ end
63
+ unless receiver.method_defined? 'highrise_id'
64
+ raise 'Your model must respond to "highrise_id", where it returns the id of its remote representation on Highrise (or nil if not existant)'
65
+ end
66
+ end
67
+ end
68
+
69
+ end
@@ -0,0 +1,8 @@
1
+ module HighriseMapper
2
+ module Version
3
+ MAJOR = 0
4
+ MINOR = 0
5
+ PATCH = 2
6
+ STRING = "#{MAJOR}.#{MINOR}.#{PATCH}"
7
+ end
8
+ end
@@ -0,0 +1,38 @@
1
+ require 'highrise'
2
+ require 'erb'
3
+ require 'logger'
4
+ require 'tempfile'
5
+ require 'pathname'
6
+ require 'yaml'
7
+
8
+ Encoding.default_internal = "utf-8"
9
+ Encoding.default_external = "utf-8"
10
+
11
+ #Adds a way to retrieve Submodules of a Module, used for CustomContentParsers
12
+ class Module
13
+ def submodules
14
+ constants.collect {|const_name| const_get(const_name)}.select {|const| const.class == Module}
15
+ end
16
+ end
17
+
18
+ module HighriseMapper
19
+
20
+ load 'highrise_mapper/context.rb'
21
+ load 'highrise_mapper/person.rb'
22
+
23
+ def self.config(root_dir=nil)
24
+ @config ||= load_config(root_dir)
25
+ end
26
+
27
+ def self.load_config(root_dir=nil)
28
+ root_dir ||= Pathname.new(Dir.pwd)
29
+
30
+ path = "#{root_dir}/config/highrise_mapper.yml"
31
+
32
+ raise "Couldn't find config yml at #{path}." unless File.file?(path)
33
+ content = File.read(path)
34
+ erb = ERB.new(content).result
35
+ YAML.load(erb).with_indifferent_access
36
+ end
37
+
38
+ end
@@ -0,0 +1,11 @@
1
+ require "spec_helper"
2
+ require 'pry'
3
+ require 'pry-nav'
4
+
5
+ describe HighriseMapper, "::config" do
6
+ it "returns a valid configuration Hash" do
7
+ config = HighriseMapper.config('templates')
8
+ config.should be_a_kind_of(Hash)
9
+ config.should have_key('person')
10
+ end
11
+ end
@@ -0,0 +1,26 @@
1
+ require "spec_helper"
2
+
3
+ describe HighriseMapper::Person, "#save_to_highrise" do
4
+ it "persists the Highrise::Person successfully" do
5
+ HighriseMapper.config('templates')
6
+ contact = Contact.new
7
+ begin
8
+ result = contact.save_to_highrise
9
+ result.should be_a_kind_of(Fixnum)
10
+ ensure
11
+ Highrise::Person.find(result).destroy
12
+ end
13
+ end
14
+ end
15
+
16
+ describe HighriseMapper::Person, "#delete_from_highrise" do
17
+ it "deletes the Highrise::Person successfully" do
18
+ HighriseMapper.config('templates')
19
+ contact = Contact.new
20
+ highrise_person_id = contact.save_to_highrise
21
+ result = contact.delete_from_highrise(highrise_person_id)
22
+ result.should be_a_kind_of(Net::HTTPOK)
23
+ end
24
+ end
25
+
26
+
@@ -0,0 +1,13 @@
1
+ require "highrise_mapper"
2
+ require "pathname"
3
+
4
+ SPECDIR = Pathname.new(File.dirname(__FILE__))
5
+ TMPDIR = SPECDIR.join("tmp")
6
+
7
+ Dir[File.dirname(__FILE__) + "/support/**/*.rb"].each {|r| require r}
8
+
9
+ RSpec.configure do |config|
10
+ config.include(SpecHelper)
11
+
12
+ config.before { FileUtils.mkdir_p(TMPDIR) }
13
+ end
@@ -0,0 +1,27 @@
1
+ class Contact
2
+ def name
3
+ 'Albert'
4
+ end
5
+
6
+ def last_name
7
+ 'Einstein'
8
+ end
9
+
10
+ def company_name
11
+ 'Princeton University'
12
+ end
13
+
14
+ def email
15
+ 'albert.einstein@princeton.edu'
16
+ end
17
+
18
+ def highrise_context
19
+ SaasCustomer.new
20
+ end
21
+
22
+ def highrise_id
23
+ 112233
24
+ end
25
+ #must be included after required behavior has been added to the Class
26
+ include HighriseMapper::Person
27
+ end
@@ -0,0 +1,3 @@
1
+ module SpecHelper
2
+
3
+ end
@@ -0,0 +1,15 @@
1
+ class SaasCustomer
2
+ def name
3
+ 'Rails na Praia'
4
+ end
5
+
6
+ def highrise_base_url
7
+ 'https://railsnapraia.highrisehq.com'
8
+ end
9
+
10
+ def highrise_token
11
+ 'a11386d68bdd94fe549b8498afafce56'
12
+ end
13
+ #must be included after required behavior has been added to the Class
14
+ include HighriseMapper::Context
15
+ end
@@ -0,0 +1,10 @@
1
+ # For a documentation on Highrise keys, refer to:
2
+ # https://github.com/37signals/highrise-api/blob/master/sections/data_reference.md#person
3
+ # NOTE: You should replace '-' with '_', like: first-name -> first_name
4
+ #
5
+ # On the left goes the Highrise key, on the right goes you model's field/method
6
+ person:
7
+ first_name: name
8
+ last_name: last_name
9
+ company_name: company_name
10
+ email_address: email
metadata ADDED
@@ -0,0 +1,155 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: highrise_mapper
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Lucas Martins
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-06-18 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: highrise-rails4
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rspec
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: rake
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: pry
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ - !ruby/object:Gem::Dependency
79
+ name: pry-nav
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ type: :development
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ description: Experimental Gem
95
+ email:
96
+ - lucasmartins@railsnapraia.com
97
+ executables: []
98
+ extensions: []
99
+ extra_rdoc_files: []
100
+ files:
101
+ - .gitignore
102
+ - .rspec
103
+ - .travis.yml
104
+ - Gemfile
105
+ - Rakefile
106
+ - Readme.md
107
+ - config/locales/en.yml
108
+ - config/locales/pt-BR.yml
109
+ - highrise_mapper.gemspec
110
+ - lib/generators/highrise_mapper/USAGE
111
+ - lib/generators/highrise_mapper/config_generator.rb
112
+ - lib/highrise_mapper.rb
113
+ - lib/highrise_mapper/context.rb
114
+ - lib/highrise_mapper/person.rb
115
+ - lib/highrise_mapper/version.rb
116
+ - spec/highrise_mapper_spec.rb
117
+ - spec/person_spec.rb
118
+ - spec/spec_helper.rb
119
+ - spec/support/contact.rb
120
+ - spec/support/helper.rb
121
+ - spec/support/saas_customer.rb
122
+ - templates/config/highrise_mapper.yml
123
+ homepage: http://rubygems.org/gems/highrise-mapper
124
+ licenses:
125
+ - MIT
126
+ post_install_message:
127
+ rdoc_options: []
128
+ require_paths:
129
+ - lib
130
+ required_ruby_version: !ruby/object:Gem::Requirement
131
+ none: false
132
+ requirements:
133
+ - - ! '>='
134
+ - !ruby/object:Gem::Version
135
+ version: 1.9.3
136
+ required_rubygems_version: !ruby/object:Gem::Requirement
137
+ none: false
138
+ requirements:
139
+ - - ! '>='
140
+ - !ruby/object:Gem::Version
141
+ version: '0'
142
+ requirements: []
143
+ rubyforge_project:
144
+ rubygems_version: 1.8.23
145
+ signing_key:
146
+ specification_version: 3
147
+ summary: Experimental Gem
148
+ test_files:
149
+ - spec/highrise_mapper_spec.rb
150
+ - spec/person_spec.rb
151
+ - spec/spec_helper.rb
152
+ - spec/support/contact.rb
153
+ - spec/support/helper.rb
154
+ - spec/support/saas_customer.rb
155
+ has_rdoc: