g5_updatable 0.0.1

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.
Files changed (86) hide show
  1. data/MIT-LICENSE +20 -0
  2. data/README.md +116 -0
  3. data/Rakefile +30 -0
  4. data/app/assets/javascripts/g5_updatable/application.js +13 -0
  5. data/app/assets/stylesheets/g5_updatable/application.css +15 -0
  6. data/app/controllers/g5_updatable/application_controller.rb +4 -0
  7. data/app/controllers/g5_updatable/feed_controller.rb +9 -0
  8. data/app/helpers/g5_updatable/application_helper.rb +4 -0
  9. data/app/views/layouts/g5_updatable/application.html.erb +14 -0
  10. data/config/routes.rb +3 -0
  11. data/lib/g5_updatable.rb +5 -0
  12. data/lib/g5_updatable/client_feed_processor.rb +22 -0
  13. data/lib/g5_updatable/client_updater.rb +25 -0
  14. data/lib/g5_updatable/engine.rb +28 -0
  15. data/lib/g5_updatable/feed_mapper.rb +57 -0
  16. data/lib/g5_updatable/g5_client.rb +10 -0
  17. data/lib/g5_updatable/g5_location.rb +21 -0
  18. data/lib/g5_updatable/locations_updater.rb +29 -0
  19. data/lib/g5_updatable/version.rb +3 -0
  20. data/lib/generators/g5_updatable/install/USAGE +11 -0
  21. data/lib/generators/g5_updatable/install/install_generator.rb +13 -0
  22. data/lib/generators/g5_updatable/install/templates/g5_updatable.rb +25 -0
  23. data/lib/tasks/g5_updatable_tasks.rake +4 -0
  24. data/spec/dummy/README.rdoc +28 -0
  25. data/spec/dummy/Rakefile +6 -0
  26. data/spec/dummy/app/assets/javascripts/application.js +13 -0
  27. data/spec/dummy/app/assets/stylesheets/application.css +15 -0
  28. data/spec/dummy/app/controllers/application_controller.rb +5 -0
  29. data/spec/dummy/app/helpers/application_helper.rb +2 -0
  30. data/spec/dummy/app/models/client.rb +2 -0
  31. data/spec/dummy/app/models/location.rb +2 -0
  32. data/spec/dummy/app/views/layouts/application.html.erb +14 -0
  33. data/spec/dummy/bin/bundle +3 -0
  34. data/spec/dummy/bin/rails +4 -0
  35. data/spec/dummy/bin/rake +4 -0
  36. data/spec/dummy/config.ru +4 -0
  37. data/spec/dummy/config/application.rb +23 -0
  38. data/spec/dummy/config/boot.rb +5 -0
  39. data/spec/dummy/config/database.yml +25 -0
  40. data/spec/dummy/config/environment.rb +5 -0
  41. data/spec/dummy/config/environments/development.rb +37 -0
  42. data/spec/dummy/config/environments/production.rb +82 -0
  43. data/spec/dummy/config/environments/test.rb +39 -0
  44. data/spec/dummy/config/initializers/assets.rb +8 -0
  45. data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
  46. data/spec/dummy/config/initializers/cookies_serializer.rb +3 -0
  47. data/spec/dummy/config/initializers/filter_parameter_logging.rb +4 -0
  48. data/spec/dummy/config/initializers/g5_updatable.rb +4 -0
  49. data/spec/dummy/config/initializers/inflections.rb +16 -0
  50. data/spec/dummy/config/initializers/mime_types.rb +4 -0
  51. data/spec/dummy/config/initializers/session_store.rb +3 -0
  52. data/spec/dummy/config/initializers/wrap_parameters.rb +14 -0
  53. data/spec/dummy/config/locales/en.yml +23 -0
  54. data/spec/dummy/config/routes.rb +3 -0
  55. data/spec/dummy/config/secrets.yml +22 -0
  56. data/spec/dummy/db/development.sqlite3 +0 -0
  57. data/spec/dummy/db/migrate/20140630175259_create_locations.rb +24 -0
  58. data/spec/dummy/db/migrate/20140630175330_create_clients.rb +13 -0
  59. data/spec/dummy/db/schema.rb +47 -0
  60. data/spec/dummy/db/test.sqlite3 +0 -0
  61. data/spec/dummy/log/development.log +6434 -0
  62. data/spec/dummy/log/test.log +6515 -0
  63. data/spec/dummy/public/404.html +67 -0
  64. data/spec/dummy/public/422.html +67 -0
  65. data/spec/dummy/public/500.html +66 -0
  66. data/spec/dummy/public/favicon.ico +0 -0
  67. data/spec/dummy/spec/fabricators/client_fabricator.rb +6 -0
  68. data/spec/dummy/spec/fabricators/location_fabricator.rb +9 -0
  69. data/spec/dummy/spec/models/client_spec.rb +0 -0
  70. data/spec/dummy/spec/models/location_spec.rb +0 -0
  71. data/spec/dummy/spec/support/client_feed.html +97 -0
  72. data/spec/dummy/spec/support/updated_client_feed.html +148 -0
  73. data/spec/dummy/tmp/pids/server.pid +1 -0
  74. data/spec/fabricators/client_fabricator.rb +6 -0
  75. data/spec/fabricators/location_fabricator.rb +9 -0
  76. data/spec/lib/g5_updatable/client_feed_processor_spec.rb +38 -0
  77. data/spec/lib/g5_updatable/client_updater_spec.rb +58 -0
  78. data/spec/lib/g5_updatable/feed_mapper_spec.rb +115 -0
  79. data/spec/lib/g5_updatable/locations_updater_spec.rb +71 -0
  80. data/spec/lib/g5_updatable/version.rb +7 -0
  81. data/spec/lib/generators/g5_updatable/install_generator_spec.rb +51 -0
  82. data/spec/lib/generators/tmp/config/routes.rb +7 -0
  83. data/spec/lib/tmp/config/initializers/g5_updatable.rb +25 -0
  84. data/spec/lib/tmp/config/routes.rb +8 -0
  85. data/spec/spec_helper.rb +44 -0
  86. metadata +279 -0
@@ -0,0 +1,20 @@
1
+ Copyright 2014 YOURNAME
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.
@@ -0,0 +1,116 @@
1
+ # G5 Updatable
2
+
3
+ [![Build Status](https://travis-ci.org/G5/g5_updatable.png?branch=master)](https://travis-ci.org/G5/g5_updatable)
4
+
5
+ G5 Updatable provides a solution for automatic updates of client and location
6
+ data when modified or created in the G5 Hub.
7
+
8
+ ## Current Version
9
+
10
+ NOT RELEASED
11
+
12
+ ## Installation
13
+
14
+ 1. Add this line to your application's Gemfile:
15
+
16
+ ```ruby
17
+ gem 'g5_updatable'
18
+ ```
19
+
20
+ 2. And then execute:
21
+
22
+ ```console
23
+ bundle
24
+ ```
25
+
26
+ 3. Run the generator.
27
+
28
+ ```ruby
29
+ rails g g5_updatable:install
30
+ ```
31
+
32
+ This creates an initilizer at config/initializers/g5_updatable.rb,
33
+ and mounts the engine at `/g5_updatable`.
34
+
35
+ ## Configuration
36
+
37
+ You can cofigure options within the generated initializer.
38
+
39
+ ```ruby
40
+ # config/initilizers/g5_updatable.rb
41
+ G5Updatable.setup do |config|
42
+ # base path to the G5 Hub
43
+ config.feed_endpoint = #string
44
+
45
+ # identifier of the client (urn)
46
+ config.client_identifier = #string
47
+
48
+ # default is true. When set to true, existing locations in your app will be
49
+ # updated with any changes made to the hub. If set to false, existing locations
50
+ # will be skipped and only newly added locations will be created.
51
+ config.update_locations = #boolean
52
+
53
+ # default is false. When set to true, client data will update.
54
+ config.update_client = #boolean
55
+
56
+ # default is [:name]. A whitlist of parameters to create/update on the model
57
+ config.location_parameters = #array of symbols
58
+
59
+ # default is [:name]. A whitlist of parameters to update on the model
60
+ config.client_parameters = #array of symbols
61
+ end
62
+ ```
63
+
64
+ ## Usage
65
+
66
+ G5 Updatable exposes a POST route at `/g5_updatable/update` that accepts a urn
67
+ parameter (client identifier within the hub). When the route is
68
+ hit, it will update/create Location and Client data based on the configuration.
69
+ See the [G5-Hub](https://github.com/G5/g5_hub) webhook logic for further info.
70
+
71
+ ## Authors
72
+
73
+ * Brian Bauer / [@bbauer](https://github.com/bbauer)
74
+
75
+ ## Contributing
76
+
77
+ 1. [Fork it](https://github.com/G5/g5_updatable/fork)
78
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
79
+ 3. Write your code and **specs**
80
+ 4. Commit your changes (`git commit -am 'Add some feature'`)
81
+ 5. Push to the branch (`git push origin my-new-feature`)
82
+ 6. Create a new Pull Request
83
+
84
+ If you find bugs, have feature requests or questions, please
85
+ [file an issue](https://github.com/G5/g5_updatable/issues).
86
+
87
+ ## Specs
88
+
89
+ ```bash
90
+ $ rspec spec
91
+ ```
92
+
93
+ ## License
94
+
95
+ Copyright (c) 2014 G5
96
+
97
+ MIT License
98
+
99
+ Permission is hereby granted, free of charge, to any person obtaining
100
+ a copy of this software and associated documentation files (the
101
+ "Software"), to deal in the Software without restriction, including
102
+ without limitation the rights to use, copy, modify, merge, publish,
103
+ distribute, sublicense, and/or sell copies of the Software, and to
104
+ permit persons to whom the Software is furnished to do so, subject to
105
+ the following conditions:
106
+
107
+ The above copyright notice and this permission notice shall be
108
+ included in all copies or substantial portions of the Software.
109
+
110
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
111
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
112
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
113
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
114
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
115
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
116
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,30 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+ require 'rdoc/task'
8
+
9
+ RDoc::Task.new(:rdoc) do |rdoc|
10
+ rdoc.rdoc_dir = 'rdoc'
11
+ rdoc.title = 'G5Updatable'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.rdoc')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+
17
+ APP_RAKEFILE = File.expand_path("../spec/dummy/Rakefile", __FILE__)
18
+ load 'rails/tasks/engine.rake'
19
+
20
+ Bundler::GemHelper.install_tasks
21
+
22
+ require 'rspec/core'
23
+ require 'rspec/core/rake_task'
24
+
25
+ Dir[File.join(File.dirname(__FILE__), 'tasks/**/*.rake')].each {|f| load f }
26
+
27
+ desc "Run all specs in spec directory (excluding plugin specs)"
28
+
29
+ RSpec::Core::RakeTask.new(:spec => 'app:db:test:prepare')
30
+ task :default => :spec
@@ -0,0 +1,13 @@
1
+ // This is a manifest file that'll be compiled into application.js, which will include all the files
2
+ // listed below.
3
+ //
4
+ // Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
5
+ // or vendor/assets/javascripts of plugins, if any, can be referenced here using a relative path.
6
+ //
7
+ // It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
8
+ // compiled file.
9
+ //
10
+ // Read Sprockets README (https://github.com/sstephenson/sprockets#sprockets-directives) for details
11
+ // about supported directives.
12
+ //
13
+ //= require_tree .
@@ -0,0 +1,15 @@
1
+ /*
2
+ * This is a manifest file that'll be compiled into application.css, which will include all the files
3
+ * listed below.
4
+ *
5
+ * Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
6
+ * or vendor/assets/stylesheets of plugins, if any, can be referenced here using a relative path.
7
+ *
8
+ * You're free to add application-wide styles to this file and they'll appear at the bottom of the
9
+ * compiled file so the styles you add here take precedence over styles defined in any styles
10
+ * defined in the other CSS/SCSS files in this directory. It is generally better to create a new
11
+ * file per style scope.
12
+ *
13
+ *= require_tree .
14
+ *= require_self
15
+ */
@@ -0,0 +1,4 @@
1
+ module G5Updatable
2
+ class ApplicationController < ActionController::Base
3
+ end
4
+ end
@@ -0,0 +1,9 @@
1
+ class G5Updatable::FeedController < ApplicationController
2
+ skip_before_filter :authenticate_user!
3
+ skip_before_filter :verify_authenticity_token
4
+
5
+ def update
6
+ G5Updatable::ClientFeedProcessor.new(params[:urn]).work
7
+ render json: {}, status: :ok
8
+ end
9
+ end
@@ -0,0 +1,4 @@
1
+ module G5Updatable
2
+ module ApplicationHelper
3
+ end
4
+ end
@@ -0,0 +1,14 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>G5Updatable</title>
5
+ <%= stylesheet_link_tag "g5_updatable/application", media: "all" %>
6
+ <%= javascript_include_tag "g5_updatable/application" %>
7
+ <%= csrf_meta_tags %>
8
+ </head>
9
+ <body>
10
+
11
+ <%= yield %>
12
+
13
+ </body>
14
+ </html>
@@ -0,0 +1,3 @@
1
+ G5Updatable::Engine.routes.draw do
2
+ post "/update" => "feed#update"
3
+ end
@@ -0,0 +1,5 @@
1
+ require "g5_updatable/engine"
2
+ require "g5_updatable/client_feed_processor"
3
+
4
+ module G5Updatable
5
+ end
@@ -0,0 +1,22 @@
1
+ require "g5_updatable/feed_mapper"
2
+ require "g5_updatable/client_updater"
3
+ require "g5_updatable/locations_updater"
4
+
5
+ class G5Updatable::ClientFeedProcessor
6
+ def initialize(urn=nil)
7
+ @urn = urn || G5Updatable.client_identifier
8
+ end
9
+
10
+ def work
11
+ if @urn
12
+ G5Updatable::ClientUpdater.new(feed_mapper.client).update
13
+ G5Updatable::LocationsUpdater.new(feed_mapper.locations).update
14
+ end
15
+ end
16
+
17
+ private
18
+
19
+ def feed_mapper
20
+ @feed_mapper ||= G5Updatable::FeedMapper.new(@urn)
21
+ end
22
+ end
@@ -0,0 +1,25 @@
1
+ class G5Updatable::ClientUpdater
2
+ def initialize(g5_client)
3
+ @g5_client = g5_client
4
+ end
5
+
6
+ def update
7
+ return unless G5Updatable.update_client && client
8
+
9
+ G5Updatable.client_parameters.each do |parameter|
10
+ client.send("#{parameter}=", @g5_client.send(parameter))
11
+ end
12
+
13
+ client.save
14
+ end
15
+
16
+ private
17
+
18
+ def client
19
+ @client ||= Client.find_by(uid: client_uid) if client_uid
20
+ end
21
+
22
+ def client_uid
23
+ "#{G5Updatable.feed_endpoint}#{G5Updatable.client_identifier}"
24
+ end
25
+ end
@@ -0,0 +1,28 @@
1
+ module G5Updatable
2
+ class Engine < ::Rails::Engine
3
+ isolate_namespace G5Updatable
4
+
5
+ config.generators do |g|
6
+ g.test_framework :rspec
7
+ g.fixture_replacement :fabrication, dir: 'spec/fabricators'
8
+ g.assets false
9
+ g.helper false
10
+ end
11
+ end
12
+
13
+ class << self
14
+ mattr_accessor :client_identifier, :feed_endpoint, :location_parameters,
15
+ :client_parameters, :update_client, :update_locations
16
+
17
+ self.client_identifier = nil
18
+ self.feed_endpoint = "http://g5-hub.herokuapp.com/clients/"
19
+ self.location_parameters = [:name]
20
+ self.client_parameters = [:name]
21
+ self.update_client = false
22
+ self.update_locations = true
23
+ end
24
+
25
+ def self.setup(&block)
26
+ yield self
27
+ end
28
+ end
@@ -0,0 +1,57 @@
1
+ require "g5_updatable/g5_client"
2
+ require "g5_updatable/g5_location"
3
+ require "microformats2"
4
+
5
+ class G5Updatable::FeedMapper
6
+ def initialize(urn)
7
+ @urn = urn
8
+ @feed = Microformats2.parse(client_uid).first if @urn
9
+ end
10
+
11
+ def client
12
+ G5Updatable::G5Client.new(client_parameters) if @feed
13
+ end
14
+
15
+ def locations
16
+ return [] if @feed.blank?
17
+
18
+ @feed.orgs.map do |location|
19
+ G5Updatable::G5Location.new(location_parameters(location.format))
20
+ end
21
+ end
22
+
23
+ private
24
+
25
+ def client_uid
26
+ "#{G5Updatable.feed_endpoint}#{@urn}" if @urn.present?
27
+ end
28
+
29
+ def client_parameters
30
+ {
31
+ name: @feed.name.to_s,
32
+ vertical: @feed.g5_vertical.to_s,
33
+ domain: @feed.g5_domain.to_s,
34
+ type: @feed.g5_domain_type.to_s
35
+ }
36
+ end
37
+
38
+ def location_parameters(location)
39
+ {
40
+ uid: location.uid.to_s,
41
+ urn: location.uid.to_s.split("/").last,
42
+ name: location.name.to_s,
43
+ domain: location.g5_domain.to_s,
44
+ street_address: location.adr.try(:format).try(:street_address).to_s,
45
+ state: location.adr.try(:format).try(:region).to_s,
46
+ city: location.adr.try(:format).try(:locality).to_s,
47
+ neighborhood: location.adr.try(:format).try(:g5_neighborhood).to_s,
48
+ postal_code: location.adr.try(:format).try(:postal_code).to_s,
49
+ phone_number: location.adr.try(:format).try(:tel).to_s,
50
+ corporate: location.g5_corporate.to_s,
51
+ floor_plans: location.g5_floorplan.to_s,
52
+ primary_amenity: location.g5_aparment_amenity_1.to_s,
53
+ qualifier: location.g5_aparment_feature_1.to_s,
54
+ primary_landmark: location.g5_landmark_1.to_s
55
+ }
56
+ end
57
+ end
@@ -0,0 +1,10 @@
1
+ require "virtus"
2
+
3
+ class G5Updatable::G5Client
4
+ include Virtus
5
+
6
+ attribute :name, String
7
+ attribute :vertical, String
8
+ attribute :domain, String
9
+ attribute :type, String
10
+ end
@@ -0,0 +1,21 @@
1
+ require "virtus"
2
+
3
+ class G5Updatable::G5Location
4
+ include Virtus
5
+
6
+ attribute :uid, String
7
+ attribute :urn, String
8
+ attribute :name, String
9
+ attribute :domain, String
10
+ attribute :street_address, String
11
+ attribute :state, String
12
+ attribute :city, String
13
+ attribute :neighborhood, String
14
+ attribute :postal_code, String
15
+ attribute :phone_number, String
16
+ attribute :corporate, String
17
+ attribute :floor_plans, String
18
+ attribute :primary_amenity, String
19
+ attribute :qualifier, String
20
+ attribute :primary_landmark, String
21
+ end
@@ -0,0 +1,29 @@
1
+ class G5Updatable::LocationsUpdater
2
+ def initialize(locations)
3
+ @locations = locations
4
+ end
5
+
6
+ def update
7
+ @locations.each { |location| process(location) unless skip?(location) }
8
+ end
9
+
10
+ private
11
+
12
+ def process(g5_location)
13
+ location = Location.find_or_initialize_by(urn: urn_for(g5_location))
14
+
15
+ G5Updatable.location_parameters.each do |parameter|
16
+ location.send("#{parameter}=", g5_location.send(parameter))
17
+ end
18
+
19
+ location.save
20
+ end
21
+
22
+ def skip?(location)
23
+ Location.exists?(urn: urn_for(location)) && !G5Updatable.update_locations
24
+ end
25
+
26
+ def urn_for(location)
27
+ location.uid.to_s.split("/").last
28
+ end
29
+ end