wix-hive-ruby 0.9.0

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 (78) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +20 -0
  3. data/.rubocop.yml +487 -0
  4. data/.yardopts +6 -0
  5. data/Gemfile +4 -0
  6. data/Guardfile +11 -0
  7. data/LICENSE.txt +22 -0
  8. data/README.md +641 -0
  9. data/Rakefile +33 -0
  10. data/e2e/activities_api_spec.rb +334 -0
  11. data/e2e/contacts_api_spec.rb +364 -0
  12. data/e2e/e2e_helper.rb +35 -0
  13. data/e2e/insights_api_spec.rb +29 -0
  14. data/lib/hive/activities/contact/contact_create_activity.rb +109 -0
  15. data/lib/hive/activities/contact/contact_form_activity.rb +31 -0
  16. data/lib/hive/activities/conversion/conversion_complete_activity.rb +33 -0
  17. data/lib/hive/activities/e_commerce/purchase_activity.rb +150 -0
  18. data/lib/hive/activities/factory.rb +71 -0
  19. data/lib/hive/activities/hotels/hotels_cancel_activity.rb +52 -0
  20. data/lib/hive/activities/hotels/hotels_confirmation_activity.rb +133 -0
  21. data/lib/hive/activities/hotels/hotels_purchase_activity.rb +48 -0
  22. data/lib/hive/activities/hotels/hotels_purchase_failed_activity.rb +59 -0
  23. data/lib/hive/activities/messaging/send_activity.rb +75 -0
  24. data/lib/hive/activities/music/album_fan_activity.rb +36 -0
  25. data/lib/hive/activities/music/album_share_activity.rb +23 -0
  26. data/lib/hive/activities/music/track_lyrics_activity.rb +38 -0
  27. data/lib/hive/activities/music/track_play_activity.rb +31 -0
  28. data/lib/hive/activities/music/track_played_activity.rb +31 -0
  29. data/lib/hive/activities/music/track_share_activity.rb +32 -0
  30. data/lib/hive/activities/music/track_skipped_activity.rb +31 -0
  31. data/lib/hive/activities/scheduler/scheduler_appointment_activity.rb +73 -0
  32. data/lib/hive/activity.rb +60 -0
  33. data/lib/hive/activity_summary.rb +24 -0
  34. data/lib/hive/connect/request/wix_api_request.rb +92 -0
  35. data/lib/hive/connect/response/error.rb +88 -0
  36. data/lib/hive/connect/response/parse_json.rb +29 -0
  37. data/lib/hive/connect/response/raise_error.rb +17 -0
  38. data/lib/hive/connect/wix_client.rb +148 -0
  39. data/lib/hive/contact.rb +153 -0
  40. data/lib/hive/cursor.rb +48 -0
  41. data/lib/hive/errors.rb +5 -0
  42. data/lib/hive/extensions/hashie_hash.rb +16 -0
  43. data/lib/hive/extensions/hashie_validate_enum.rb +11 -0
  44. data/lib/hive/rest/activities.rb +55 -0
  45. data/lib/hive/rest/api.rb +13 -0
  46. data/lib/hive/rest/contacts.rb +114 -0
  47. data/lib/hive/rest/insights.rb +17 -0
  48. data/lib/hive/util.rb +20 -0
  49. data/lib/hive/version.rb +14 -0
  50. data/lib/wix-hive-ruby.rb +5 -0
  51. data/samples/quick_start.rb +49 -0
  52. data/spec/hive/activities/contact/contact_create_activity_spec.rb +25 -0
  53. data/spec/hive/activities/contact/contact_form_activity_spec.rb +9 -0
  54. data/spec/hive/activities/conversion/conversion_complete_activity_spec.rb +9 -0
  55. data/spec/hive/activities/e_commerce/purchase_activity_spec.rb +19 -0
  56. data/spec/hive/activities/factory_spec.rb +78 -0
  57. data/spec/hive/activities/hotels/hotels_cancel_activity_spec.rb +22 -0
  58. data/spec/hive/activities/hotels/hotels_confirmation_activity_spec.rb +34 -0
  59. data/spec/hive/activities/hotels/hotels_purchase_activity_spec.rb +22 -0
  60. data/spec/hive/activities/hotels/hotels_purchase_failed_activity_spec.rb +22 -0
  61. data/spec/hive/activities/messaging/send_activity_spec.rb +13 -0
  62. data/spec/hive/activities/scheduler/scheduler_appointment_activity_spec.rb +10 -0
  63. data/spec/hive/activity_spec.rb +18 -0
  64. data/spec/hive/connect/request/wix_api_request_spec.rb +54 -0
  65. data/spec/hive/connect/response/error_spec.rb +31 -0
  66. data/spec/hive/connect/response/parse_json_spec.rb +28 -0
  67. data/spec/hive/connect/response/raise_error_spec.rb +19 -0
  68. data/spec/hive/connect/wix_client_spec.rb +103 -0
  69. data/spec/hive/contact_spec.rb +148 -0
  70. data/spec/hive/cursor_spec.rb +75 -0
  71. data/spec/hive/hashie_hash_spec.rb +23 -0
  72. data/spec/hive/rest/activities_spec.rb +87 -0
  73. data/spec/hive/rest/contacts_spec.rb +225 -0
  74. data/spec/hive/rest/insights_spec.rb +17 -0
  75. data/spec/hive/util_spec.rb +36 -0
  76. data/spec/spec_helper.rb +59 -0
  77. data/wix-hive-ruby.gemspec +38 -0
  78. metadata +392 -0
@@ -0,0 +1,13 @@
1
+ require 'hive/rest/contacts'
2
+ require 'hive/rest/activities'
3
+ require 'hive/rest/insights'
4
+
5
+ module Hive
6
+ module REST
7
+ module API
8
+ include Hive::REST::Contacts
9
+ include Hive::REST::Activities
10
+ include Hive::REST::Insights
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,114 @@
1
+ require 'hive/contact'
2
+ require 'hive/util'
3
+ require 'time'
4
+ require 'hive/activities/factory'
5
+
6
+ module Hive
7
+ module REST
8
+ module Contacts
9
+ include Hive::Util
10
+
11
+ def contacts(query_options = {})
12
+ perform_with_cursor(:get, 'v1/contacts', Hive::Contact, params: query_options)
13
+ end
14
+
15
+ def contact(contact_id)
16
+ perform_with_object(:get, "v1/contacts/#{contact_id}", Hive::Contact)
17
+ end
18
+
19
+ def new_contact(contact)
20
+ perform_with_object(:post, 'v1/contacts', Hashie::Mash, body: contact.to_json)
21
+ end
22
+
23
+ def upsert_contact(args)
24
+ fail ArgumentError, 'Phone or Email are required!' unless args.key?(:phone) || args.key?(:email)
25
+
26
+ perform_with_object(:put, 'v1/contacts', Hashie::Mash, body: args.to_json)
27
+ end
28
+
29
+ def contacts_tags
30
+ perform_with_object(:get, 'v1/contacts/tags', Array)
31
+ end
32
+
33
+ def contacts_subscribers
34
+ perform_with_cursor(:get, 'v1/contacts/subscribers', Hive::ContactSubscriber)
35
+ end
36
+
37
+ def update_contact(_contact_id, _contact)
38
+ fail NotImplementedError, 'Update contacts is not available!'
39
+
40
+ # edit_contact_field("v1/contacts/#{contact_id}", contact)
41
+ end
42
+
43
+ def update_contact_name(id, name)
44
+ edit_contact_field("v1/contacts/#{id}/name", name)
45
+ end
46
+
47
+ def update_contact_company(id, company)
48
+ edit_contact_field("v1/contacts/#{id}/company", company)
49
+ end
50
+
51
+ def update_contact_picture(id, picture)
52
+ edit_contact_field("v1/contacts/#{id}/picture", picture)
53
+ end
54
+
55
+ def update_contact_address(id, address_id, address)
56
+ edit_contact_field("v1/contacts/#{id}/address/#{address_id}", address)
57
+ end
58
+
59
+ def update_contact_email(id, email_id, email)
60
+ edit_contact_field("v1/contacts/#{id}/email/#{email_id}", email)
61
+ end
62
+
63
+ def update_contact_phone(id, phone_id, phone)
64
+ edit_contact_field("v1/contacts/#{id}/phone/#{phone_id}", phone)
65
+ end
66
+
67
+ def update_contact_date(id, date_id, date)
68
+ edit_contact_field("v1/contacts/#{id}/date/#{date_id}", date)
69
+ end
70
+
71
+ def update_contact_note(id, note_id, note)
72
+ edit_contact_field("v1/contacts/#{id}/note/#{note_id}", note)
73
+ end
74
+
75
+ def update_contact_custom(id, custom_id, custom)
76
+ edit_contact_field("v1/contacts/#{id}/custom/#{custom_id}", custom)
77
+ end
78
+
79
+ def add_contact_address(id, address)
80
+ add_contact_field("v1/contacts/#{id}/address", address)
81
+ end
82
+
83
+ def add_contact_email(id, email)
84
+ add_contact_field("v1/contacts/#{id}/email", email)
85
+ end
86
+
87
+ def add_contact_phone(id, phone)
88
+ add_contact_field("v1/contacts/#{id}/phone", phone)
89
+ end
90
+
91
+ def add_contact_note(id, note)
92
+ add_contact_field("v1/contacts/#{id}/note", note)
93
+ end
94
+
95
+ def add_contact_custom(id, custom)
96
+ add_contact_field("v1/contacts/#{id}/custom", custom)
97
+ end
98
+
99
+ def add_contact_tags(id, tags)
100
+ add_contact_field("v1/contacts/#{id}/tags", tags)
101
+ end
102
+
103
+ private
104
+
105
+ def edit_contact_field(url, body)
106
+ perform_with_object(:put, url, Hive::Contact, body: body.to_json, params: { modifiedAt: Time.now.iso8601(3) })
107
+ end
108
+
109
+ def add_contact_field(url, body)
110
+ perform_with_object(:post, url, Hive::Contact, body: body.to_json, params: { modifiedAt: Time.now.iso8601(3) })
111
+ end
112
+ end
113
+ end
114
+ end
@@ -0,0 +1,17 @@
1
+ require 'hive/activity_summary'
2
+
3
+ module Hive
4
+ module REST
5
+ module Insights
6
+ include Hive::Util
7
+
8
+ def activities_summary(query_options = {})
9
+ perform_with_object(:get, 'v1/insights/activities/summary', Hive::ActivitySummary, params: query_options)
10
+ end
11
+
12
+ def contact_activities_summary(id, query_options = {})
13
+ perform_with_object(:get, "v1/insights/contacts/#{id}/activities/summary", Hive::ActivitySummary, params: query_options)
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,20 @@
1
+ require 'hive/connect/wix_client'
2
+
3
+ module Hive
4
+ module Util
5
+ def perform(request_method, path, options = {})
6
+ request = Hive::Request::WixAPIRequest.new(self, request_method, path, options)
7
+ request.perform
8
+ end
9
+
10
+ def perform_with_object(request_method, path, klass, options = {})
11
+ request = Hive::Request::WixAPIRequest.new(self, request_method, path, options)
12
+ request.perform_with_object(klass)
13
+ end
14
+
15
+ def perform_with_cursor(request_method, path, klass, options = {})
16
+ request = Hive::Request::WixAPIRequest.new(self, request_method, path, options)
17
+ request.perform_with_cursor(klass)
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,14 @@
1
+ module Hive
2
+ class Version
3
+ MAJOR = 0
4
+ MINOR = 9
5
+ PATCH = 0
6
+
7
+ class << self
8
+ # @return [String]
9
+ def to_s
10
+ [MAJOR, MINOR, PATCH].compact.join('.')
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,5 @@
1
+ # Client
2
+ require 'hive/connect/wix_client'
3
+
4
+ # Extensions
5
+ require 'hive/extensions/hashie_hash'
@@ -0,0 +1,49 @@
1
+ require 'sinatra'
2
+ require 'wix-hive-ruby'
3
+
4
+ SECRET_KEY = 'YOUR_SECRET_KEY'
5
+ APP_ID = 'YOUR_APP_ID'
6
+
7
+ # The route should match the app endpoint set during registration
8
+ get '/' do
9
+ # The GET request to your app endpoint will contain an instance parameter for you to parse
10
+ instance = params.delete('instance')
11
+
12
+ # Parse the instance parameter
13
+ wixInstance = Hive::Client.parse_instance_data(instance, SECRET_KEY)
14
+
15
+ # Create a Wix Hive Client
16
+ client = Hive::Client.new do |config|
17
+ config.secret_key = SECRET_KEY
18
+ config.app_id = APP_ID
19
+ config.instance_id = wixInstance.instanceId
20
+ end
21
+
22
+ contact = Hive::Contact.new
23
+ contact.name.first = 'Quick'
24
+ contact.name.last = 'Start'
25
+ contact.add_email(email: 'quick.start@example.com', tag: 'work')
26
+ contact.add_phone(phone: '123456789', tag: 'work')
27
+ contact.add_url(url: 'wix.com', tag: 'site')
28
+
29
+ # Create a new contact
30
+ contact_res = client.new_contact(contact)
31
+
32
+ FACTORY = Hive::Activities
33
+ activity = Hive::Activity.new(
34
+ type: FACTORY::MUSIC_ALBUM_FAN.type,
35
+ locationUrl: 'http://www.wix.com',
36
+ details: { summary: 'test', additionalInfoUrl: 'http://www.wix.com' },
37
+ info: { album: { name: 'Wix', id: '1234' } })
38
+
39
+ # Add an activity to the contact
40
+ activity_res = client.add_contact_activity(contact_res.contactId, activity)
41
+
42
+ body "Contact created: #{contact_res.contactId}.
43
+ Activity created: #{activity_res.activityId}
44
+ Thank you!"
45
+ end
46
+
47
+ after do
48
+ headers({ 'X-Frame-Options' => 'ALLOW-FROM wix.com' })
49
+ end
@@ -0,0 +1,25 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'CONTACTS_CREATE' do
4
+ subject(:create_activity) {ACTIVITIES_FACTORY::CONTACTS_CREATE.klass.new}
5
+
6
+ it '.add_email' do
7
+ create_activity.add_email( email: 'alex@example.com', tag: 'tag' )
8
+ end
9
+
10
+ it '.add_phone' do
11
+ create_activity.add_phone( phone: '123456789', tag: 'tag' )
12
+ end
13
+
14
+ it '.add_address' do
15
+ create_activity.add_address( tag: 'tag' )
16
+ end
17
+
18
+ it '.add_date' do
19
+ create_activity.add_date( date: 'date', tag: 'tag' )
20
+ end
21
+
22
+ it '.add_url' do
23
+ create_activity.add_url( url: 'wix.com', tag: 'tag' )
24
+ end
25
+ end
@@ -0,0 +1,9 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'CONTACT_CONTACT_FORM' do
4
+ subject(:create_activity) {ACTIVITIES_FACTORY::CONTACT_CONTACT_FORM.klass.new}
5
+
6
+ it '.add_field' do
7
+ create_activity.add_field( name: 'custom', value: 'value' )
8
+ end
9
+ end
@@ -0,0 +1,9 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'CONVERSION_COMPLETE' do
4
+ subject(:create_activity) {ACTIVITIES_FACTORY::CONVERSION_COMPLETE.klass.new( conversionType: 'PAGEVIEW' )}
5
+
6
+ it '.add_metadata' do
7
+ create_activity.add_metadata( property: 'custom', value: 'value' )
8
+ end
9
+ end
@@ -0,0 +1,19 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'E_COMMERCE_PURCHASE' do
4
+ coupon = {total: '1', title: 'Dis'}
5
+ payment = {total: '1', subtotal: '1', currency: 'EUR', coupon: coupon}
6
+
7
+ subject(:create_activity) { ACTIVITIES_FACTORY::E_COMMERCE_PURCHASE.klass.new( cartId: '11111', storeId: '11111', payment: payment ) }
8
+
9
+ it '.add_item' do
10
+ create_activity.add_item( id: 1, title: 'title', quantity: 1, currency: 'EUR' )
11
+ end
12
+
13
+ context 'E_COMMERCE_PURCHASE::Item' do
14
+ it '.add_variant' do
15
+ create_activity.add_item( id: 1, title: 'title', quantity: 1, currency: 'EUR' )
16
+ create_activity.items.first.add_variant( title: 'title' )
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,78 @@
1
+ require 'spec_helper'
2
+
3
+ describe Hive::Activities do
4
+
5
+ context 'factory methods' do
6
+ it '#CONTACT_CONTACT_FORM' do
7
+ expect(Hive::Activities::CONTACT_CONTACT_FORM.klass).to eq Hive::Activities.class_for_type(Hive::Activities::CONTACT_CONTACT_FORM.type)
8
+ end
9
+
10
+ it '#CONTACTS_CREATE' do
11
+ expect(Hive::Activities::CONTACTS_CREATE.klass).to eq Hive::Activities.class_for_type(Hive::Activities::CONTACTS_CREATE.type)
12
+ end
13
+
14
+ it '#CONVERSION_COMPLETE' do
15
+ expect(Hive::Activities::CONVERSION_COMPLETE.klass).to eq Hive::Activities.class_for_type(Hive::Activities::CONVERSION_COMPLETE.type)
16
+ end
17
+
18
+ it '#E_COMMERCE_PURCHASE' do
19
+ expect(Hive::Activities::E_COMMERCE_PURCHASE.klass).to eq Hive::Activities.class_for_type(Hive::Activities::E_COMMERCE_PURCHASE.type)
20
+ end
21
+
22
+ it '#MESSAGING_SEND' do
23
+ expect(Hive::Activities::MESSAGING_SEND.klass).to eq Hive::Activities.class_for_type(Hive::Activities::MESSAGING_SEND.type)
24
+ end
25
+
26
+ it '#MUSIC_ALBUM_FAN' do
27
+ expect(Hive::Activities::MUSIC_ALBUM_FAN.klass).to eq Hive::Activities.class_for_type(Hive::Activities::MUSIC_ALBUM_FAN.type)
28
+ end
29
+
30
+ it '#MUSIC_ALBUM_FAN' do
31
+ expect(Hive::Activities::MUSIC_ALBUM_SHARE.klass).to eq Hive::Activities.class_for_type(Hive::Activities::MUSIC_ALBUM_SHARE.type)
32
+ end
33
+
34
+ it '#MUSIC_TRACK_LYRICS' do
35
+ expect(Hive::Activities::MUSIC_TRACK_LYRICS.klass).to eq Hive::Activities.class_for_type(Hive::Activities::MUSIC_TRACK_LYRICS.type)
36
+ end
37
+
38
+ it '#MUSIC_TRACK_PLAY' do
39
+ expect(Hive::Activities::MUSIC_TRACK_PLAY.klass).to eq Hive::Activities.class_for_type(Hive::Activities::MUSIC_TRACK_PLAY.type)
40
+ end
41
+
42
+ it '#MUSIC_TRACK_PLAYED' do
43
+ expect(Hive::Activities::MUSIC_TRACK_PLAYED.klass).to eq Hive::Activities.class_for_type(Hive::Activities::MUSIC_TRACK_PLAYED.type)
44
+ end
45
+
46
+ it '#MUSIC_TRACK_SKIP' do
47
+ expect(Hive::Activities::MUSIC_TRACK_SKIP.klass).to eq Hive::Activities.class_for_type(Hive::Activities::MUSIC_TRACK_SKIP.type)
48
+ end
49
+
50
+ it '#MUSIC_TRACK_SHARE' do
51
+ expect(Hive::Activities::MUSIC_TRACK_SHARE.klass).to eq Hive::Activities.class_for_type(Hive::Activities::MUSIC_TRACK_SHARE.type)
52
+ end
53
+
54
+ # it '#HOTELS_CONFIRMATION' do
55
+ # expect(Hive::Activities::HOTELS_CONFIRMATION.klass).to eq Hive::Activities.class_for_type(Hive::Activities::HOTELS_CONFIRMATION.type)
56
+ # end
57
+
58
+ it '#HOTELS_CANCEL' do
59
+ expect(Hive::Activities::HOTELS_CANCEL.klass).to eq Hive::Activities.class_for_type(Hive::Activities::HOTELS_CANCEL.type)
60
+ end
61
+
62
+ it '#HOTELS_PURCHASE' do
63
+ expect(Hive::Activities::HOTELS_PURCHASE.klass).to eq Hive::Activities.class_for_type(Hive::Activities::HOTELS_PURCHASE.type)
64
+ end
65
+
66
+ it '#HOTELS_PURCHASE_FAILED' do
67
+ expect(Hive::Activities::HOTELS_PURCHASE_FAILED.klass).to eq Hive::Activities.class_for_type(Hive::Activities::HOTELS_PURCHASE_FAILED.type)
68
+ end
69
+
70
+ it '#SCHEDULER_APPOINTMENT' do
71
+ expect(Hive::Activities::SCHEDULER_APPOINTMENT.klass).to eq Hive::Activities.class_for_type(Hive::Activities::SCHEDULER_APPOINTMENT.type)
72
+ end
73
+
74
+ it 'Invalid type' do
75
+ expect(Hive::Activities.class_for_type('invalid')).to be_nil
76
+ end
77
+ end
78
+ end
@@ -0,0 +1,22 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'HOTELS_CANCEL' do
4
+ guest = { total: 1, adults: 1, children: 0 }
5
+
6
+ day_ago = (Time.now - (60 * 60 * 24)).iso8601(3)
7
+ stay = { checkin: day_ago, checkout: Time.now.iso8601(3) }
8
+
9
+ invoice = {total: '1', subtotal: '1', currency: 'EUR'}
10
+
11
+ refund = {kind: 'FULL', total: 1, currency: 'EUR', destination: 'NYC'}
12
+
13
+ subject(:create_activity) {ACTIVITIES_FACTORY::HOTELS_CANCEL.klass.new( cancelDate: day_ago, refund: refund, guests: guest, stay: stay, invoice: invoice ) }
14
+
15
+ it '.add_rate' do
16
+ create_activity.add_rate( date: 'date', subtotal: '1', total: '1', currency: 'EUR' )
17
+ end
18
+
19
+ it '.add_room' do
20
+ create_activity.add_room( maxOccupancy: 1 )
21
+ end
22
+ end
@@ -0,0 +1,34 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'HOTELS_CONFIRMATION' do
4
+ guest = { total: 1, adults: 1, children: 0 }
5
+
6
+ day_ago = (Time.now - (60 * 60 * 24)).iso8601(3)
7
+ stay = { checkin: day_ago, checkout: Time.now.iso8601(3) }
8
+
9
+ invoice = {total: '1', subtotal: '1', currency: 'EUR'}
10
+
11
+ subject(:create_activity) {ACTIVITIES_FACTORY::HOTELS_CONFIRMATION.klass.new( source: 'GUEST', guests: guest, stay: stay, invoice: invoice ) }
12
+
13
+ it '.add_rate' do
14
+ create_activity.add_rate( date: 'date', subtotal: '1', total: '1', currency: 'EUR' )
15
+ end
16
+
17
+ it '.add_room' do
18
+ create_activity.add_room( maxOccupancy: 1 )
19
+ end
20
+
21
+ context 'HOTELS_CONFIRMATION::Bed' do
22
+ it '.add_bed' do
23
+ create_activity.add_room( maxOccupancy: 1 )
24
+ create_activity.rooms.first.add_bed( kind: 'king' )
25
+ end
26
+ end
27
+
28
+ context 'HOTELS_CONFIRMATION::Rate' do
29
+ it '.add_tax' do
30
+ create_activity.add_rate( date: 'date', subtotal: '1', total: '1', currency: 'EUR' )
31
+ create_activity.rates.first.add_tax( name: 'VAT', total: '1', currency: 'EUR')
32
+ end
33
+ end
34
+ end