Class: InterakthqRails::Import
- Inherits:
-
Object
- Object
- InterakthqRails::Import
- Includes:
- UserValidator
- Defined in:
- lib/interakthq_rails/import.rb
Constant Summary
Constants included from UserValidator
UserValidator::EXPECTED_ATTRIBUTES
Instance Attribute Summary collapse
-
#http ⇒ Object
readonly
Returns the value of attribute http.
-
#max_batch_size ⇒ Object
readonly
Returns the value of attribute max_batch_size.
-
#members_sent ⇒ Object
Returns the value of attribute members_sent.
-
#response_verified ⇒ Object
Returns the value of attribute response_verified.
-
#uri ⇒ Object
readonly
Returns the value of attribute uri.
Class Method Summary collapse
Instance Method Summary collapse
- #custom_data_attributes_for_member(member) ⇒ Object
-
#initialize ⇒ Import
constructor
A new instance of Import.
- #json_collection_of_members(members) ⇒ Object
- #send_members(members) ⇒ Object
- #start ⇒ Object
- #start_sending_in_batches ⇒ Object
Methods included from UserValidator
#availabe_model_attributes, #check_basic_validation, #model_name, #verify_reponse
Constructor Details
Instance Attribute Details
#http ⇒ Object (readonly)
Returns the value of attribute http.
7 8 9 |
# File 'lib/interakthq_rails/import.rb', line 7 def http @http end |
#max_batch_size ⇒ Object (readonly)
Returns the value of attribute max_batch_size.
7 8 9 |
# File 'lib/interakthq_rails/import.rb', line 7 def max_batch_size @max_batch_size end |
#members_sent ⇒ Object
Returns the value of attribute members_sent.
8 9 10 |
# File 'lib/interakthq_rails/import.rb', line 8 def members_sent @members_sent end |
#response_verified ⇒ Object
Returns the value of attribute response_verified.
8 9 10 |
# File 'lib/interakthq_rails/import.rb', line 8 def response_verified @response_verified end |
#uri ⇒ Object (readonly)
Returns the value of attribute uri.
7 8 9 |
# File 'lib/interakthq_rails/import.rb', line 7 def uri @uri end |
Class Method Details
.start ⇒ Object
10 11 12 |
# File 'lib/interakthq_rails/import.rb', line 10 def self.start new.start end |
.uri ⇒ Object
14 15 16 17 |
# File 'lib/interakthq_rails/import.rb', line 14 def self.uri host = (Rails.env.production? ? "https://interakthq.com" : "http://localhost:3000") URI.parse(host + "/api/v1/members/import_data") end |
Instance Method Details
#custom_data_attributes_for_member(member) ⇒ Object
62 63 64 65 66 67 68 69 70 71 |
# File 'lib/interakthq_rails/import.rb', line 62 def custom_data_attributes_for_member(member) if( (custom_data = InterakthqRails.config.user.custom_data).present?) custom_data.inject({}) do |hash, (key, value)| hash[key] = ( value.kind_of?(Proc) ? value.call(member) : member.send( value)) hash end else {} end end |
#json_collection_of_members(members) ⇒ Object
51 52 53 54 55 56 57 58 59 |
# File 'lib/interakthq_rails/import.rb', line 51 def json_collection_of_members( members) members.map do |member| member_hash = {} availabe_model_attributes.each do |attribute| member_hash[attribute] = member.send( attribute) end member_hash.merge( custom_data_attributes_for_member(member)) end.compact end |
#send_members(members) ⇒ Object
41 42 43 44 45 46 47 48 49 |
# File 'lib/interakthq_rails/import.rb', line 41 def send_members( members) request = Net::HTTP::Post.new(uri.request_uri) request.basic_auth(InterakthqRails.config.app_id, InterakthqRails.config.api_key) request["Content-Type"] = "application/json" request.body = {members: members}.to_json response = http.request(request) verify_reponse( response) unless response_verified end |
#start ⇒ Object
26 27 28 29 |
# File 'lib/interakthq_rails/import.rb', line 26 def start check_basic_validation start_sending_in_batches end |
#start_sending_in_batches ⇒ Object
31 32 33 34 35 36 37 38 39 |
# File 'lib/interakthq_rails/import.rb', line 31 def start_sending_in_batches model_name.find_in_batches batch_size: max_batch_size do |members| members_json = json_collection_of_members( members) send_members( members_json) @members_sent += members_json.length puts "#{members_sent} users sent." end end |