I'm trying to implement custom attributes for Intercom.io into my iOS app. I'm in XCode 7, Swift 2 and iOS 9.
Here's my code:
class func updateUser(user: User) {
Intercom.updateUserWithAttributes([
"name": user.name,
"email": user.email
])
let userAttributes = ([
"role": "customer",
"phone": user.phone,
"First Name": user.firstName,
"Last Name": user.lastName,
"Referral Code": user.referralCode,
"Avatar Pic": user.avatarURL,
"Profile Pic": user.profilePicURL
])
Intercom.updateUserWithAttributes(["custom_attributes": userAttributes])
}
I am successfully submitting "name" & "email" but my "custom_attributes" aren't working. I think my syntax is correct according to the Intercom's documentation:
https://docs.intercom.io/Install-on-your-mobile-product/configuring-intercom-for-ios
But I'm a Swift newbie and have no experience with Obj-C.
Also important to note that my events are reporting properly through.
Intercom.logEventWithName(eventName)
Is there anything wrong with my syntax?? Or anything else? Please help!
Turns out there were two issues:
1) Intercom documentation wrong and "custom_attributes" tag not needed for custom attributes
2) my URL formats were NSURL and not Strings and therefore the whole object was getting rejected
Fixed now! Thanks to Dale from Intercom for the support
the code is simply:
let userAttributes = [
"name": user.name,
"email": user.email,
"role": "customer",
"phone": user.phone,
"first_name": user.firstName,
"last_name": user.lastName,
"referral_code": user.referralCode,
"avatar_pic": user.avatarURLString,
"profile_pic": user.profilePicURLString
]
Intercom.updateUserWithAttributes(userAttributes)
Related
I believe this is going to be an easy fix but everything I'm trying isn't working.
I'm getting data from an external API and I'm looking at display the data after matching the ids.
The data is JSON and has some nested content.
I've tried the following
<li>Street: <%= #users.find{|u| u['id'] == #album['userId']}.try(:[],'address''street') || 'not available' %></li>
The data is structured like so
{
{
"id": 1,
"name": "Leanne Graham",
"username": "Bret",
"email": "Sincere#april.biz",
"address": {
"street": "Kulas Light",
"suite": "Apt. 556",
"city": "Gwenborough",
"zipcode": "92998-3874",
"geo": {
"lat": "-37.3159",
"lng": "81.1496"
}
},
}
You'll want that to be:
<li>Street: <%= #users.find{|u| u['id'] == #album['userId']}.try(:[],'address').try(:[],'street') || 'not available' %></li>
For a multi-level try, you might want to look into dig.
Also, if the iteration is as in your previous questions, then #album may need to be album. If this is in a different context than your previous questions, then kindly disregard.
This is where dig is useful:
https://ruby-doc.org/core-2.3.0_preview1/Hash.html
<li>Street: <%= #users.find{|u| u['id'] == #album['userId']}.dig(:address, :street) || 'not available' %></li>
I am using Rails as an API server, and I wonder why the data being sent to the server needs to be nested. This seems to be the preferred way of defining params:
def user_params
params.require(:user).permit(:first_name, :last_name, :password, :username, :email)
end
And this would be the corresponding JSON sent to the create route:
{
"user": {
"username": "lorem",
"first_name": "ipsum",
"last_name": "dolor",
"password": "sit",
"email": "amet"
}
}
Why is this the preferred way of posting data? Why could not the JSON be:
{
"username": "lorem",
"first_name": "ipsum",
"last_name": "dolor",
"password": "sit",
"email": "amet"
}
These aren't the only parameters that are sent while creating a resource for you, others are:
utf8 with value ✓
authenticity_token with a random string
commit with value either Save or Update
So the logic is pretty obvious: Rails groups all the user-belonging-parameters inside user key, and thus, it's easier to read, easier to interpret by the code, and easier to whitelist the related parameters.
Not only this, sometimes you will try to create multiple resources through one request, like a user has many books, so you would like to create a user, and at the same time, books - something called Nested Resources, and in that case, it will be like this:
{
"user":
{
"username": "john_don",
"books":
{
"0":
{
"author_id": 1
}
}
}
}
I hope you get the idea.
So I've been looking into the surveymonkey v3 api documentation for formatting questions types. What I want to do is create a multiple choice question that has an "other" option, which if selected has a text field that a user can fill in to be more specific. Is there a way to accomplish this with the api?
You should be able to do that when creating/updating a question.
Example:
POST /v3/surveys/<id>/pages/<id>/questions
{
"family": "single_choice",
"subtype": "vertical",
"answers": {
"other": [{
"text": "Other (please specify)",
"is_answer_choice": true,
"num_lines": 1,
"num_chars": 50
}],
"choices": [
{
"text": "Apples"
},
{
"text": "Oranges"
},
{
"text": "Bananas"
}
]
},
"headings": [
{
"heading": "What is your favourite fruit?"
}
]
}
This is_answer_choice field seems to not be accepted currently. That is a bug, you can watch the docs to potentially get notified on updates, or try it again later.
Edit: This method should work now, give it a try and let me know if it solves your problem!
I have two models. For example Person and Address.
Because I want to add or update addresses to the person within one request, the person model looks like:
has_many :addresses
accepts_nested_attributes_for :addresses
In the address controller is only one validation
validates :city, presence: true
When I now update the user via json api it works like a charm:
{
"user": {
"addresses_attributes": [
{"street": "bla", "zip": "12345", "city": "blubb"},
{"street": "blu", "zip": "98765", "city": "blebb"}
]
}
}
Now I delete the city of the second record:
{
"user": {
"addresses_attributes": [
{"street": "bla", "zip": "12345", "city": "blubb"},
{"street": "blu", "zip": "98765"}
]
}
}
and in the Users controller I can render a json response, something like:
render json: #user.errors
which gives me the correct error.
I am missing, that I don't know which of the addresses threw the error (In this example the second).
Any Ideas?
You can return the entire user object with its nested attributes and errors. I.e.
render json: #user.as_json(
include: [{addresses: {methods: [:errors]}],
methods: [:errors]
)
The result should look like this:
{
"user": {
"errors": {...},
"addresses_attributes": [
{"street": "bla", "zip": "12345", "city": "blubb", "errors": {...}},
{"street": "blu", "zip": "98765", "errors": {...}}
]
}
}
The #user that you tried to create will still contain the addresses that it tried to create with the nested attributes.
I don't know exactly how you want to render the fact that an address failed the validation but you can identify the one(s) that did fail by iterating over the #user.addresses.
For example, this will return all the invalid addresses:
#user.addresses.select { |address| !address.valid? }
You can still render these objects, or the json representation of them, even though they haven't been saved to the database.
hi friends i am working on grails using the web services i.e rest api is developed using the java and these are my request and response
requestBody:
{"color": "Select Color", "size": "Select Size", "fullName": "hg", "address": "g", "shape": "Select Shape", "finish": "Select Finish", "fileUpload": "new file","description": "gh", "telephone_Number": "9900887766", "email_Address": "email_Address", "line1": "ssap newas", "city": "gfdg", "state": "gf", "zip": "null", "ship_address": "g", "ship_line1": "abc xyz", "ship_city": "", "ship_state": "", "ship_zip": "null"};
response :
{"statusCode":"1","referenceNumber":"5","errorCode":null}
now i want the response status code should be printed on .gsp page
With JSON you need to do it via javascript, use jQuery to update that html element that would contain the status text. Such things are very easy to do with angularjs though.
You may use JsonSlurper to convert json to map
def slurper = new JsonSlurper()
def result = slurper.parseText('{"statusCode":"1","referenceNumber":"5","errorCode":null}`)
assert result.statusCode == "1"