Flexirest to handle string array - ruby-on-rails

I'm using Flexirest to make a request to an endpoint that returns an array of strings with Content-Type: application\json.
This is the model:
class Neighborhood < BaseModel
get :by_location, "/neighborhoods/by_location"
end
And this is how I make the request:
def get_neighborhood_list
city_to_use = ApplicationHelper.user_location[:city] || Rails.configuration.default_location[:city]
#hoods = Neighborhood.by_location(city: city_to_use)
end
And this is called but it only reaches the super line since it breaks inside there:
def handle_response(response, cached = nil)
result = super(response, cached)
setup_paging_variables result, response
setup_paging_variables result.data, response if result.respond_to? 'data'
result
end
The endpoint returns an array of strings:
[
"Ahwatukee Foothills Village",
"Alhambra",
"Camelback East Village",
"Central City",
]
With this headers
My problem is that Flexirest is treating the strings as json objects instead due to the value on the Content-Type header.
I get this error
TypeError: no implicit conversion of Symbol into Integer
on this line on the if attributes[:_links].
It's getting there because hal_response? is true
Do I need to do something in particular in order for Flexirest to stop behaving like this?
Update
I guess I didn't explained it properly. The error is occurring on the gem side.
Here is the full error:
TypeError - no implicit conversion of Symbol into Integer:
flexirest (1.5.5) lib/flexirest/request.rb:612:in `handle_hal_links_embedded'
flexirest (1.5.5) lib/flexirest/request.rb:553:in `new_object'
flexirest (1.5.5) lib/flexirest/request.rb:704:in `block in generate_new_object'
flexirest (1.5.5) lib/flexirest/request.rb:703:in `generate_new_object'
flexirest (1.5.5) lib/flexirest/request.rb:501:in `handle_response'
config/initializers/01_flexirest.rb:7:in `handle_response'
flexirest (1.5.5) lib/flexirest/request.rb:219:in `block (2 levels) in call'
faraday (0.9.2) lib/faraday/response.rb:57:in `on_complete'
flexirest (1.5.5) lib/flexirest/request.rb:200:in `block in call'
activesupport (4.2.4) lib/active_support/notifications.rb:164:in `block in instrument'
activesupport (4.2.4) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
activesupport (4.2.4) lib/active_support/notifications.rb:164:in `instrument'
flexirest (1.5.5) lib/flexirest/request.rb:156:in `call'
flexirest (1.5.5) lib/flexirest/mapping.rb:46:in `_call'
flexirest (1.5.5) lib/flexirest/mapping.rb:28:in `block in _map_call'
app/controllers/application_controller.rb:93:in `get_neighborhood_list
The if attributes[:_links] line is part of the gem code, and it's here on this line
When it reaches this line the value of attributes is one of the strings inside the response, for instance: attributes = "Ahwatukee Foothills Village". That's why it gives the error.
What I'm trying to figure out if there is a way (maybe something in the configuration) that I can use Flexirest to handle this kind of responses.

This is a old question but thought I'd answer it. you need to add
plain: true
in your model like below.
class Neighborhood < BaseModel
get :by_location, "/neighborhoods/by_location", plain: true
end
It will then return the string array

Related

Reails remote form format exists but not recognized

The following form intends to generate an XHR response
<% articlediscounts_for_article = #articlediscounts.where(article_id: article.id).first %>
<%= form_with(scope: articlediscounts_for_article, url: user_discount_users_path, local: false, method: :post) do |form| %>
the action processes
Processing by UsersController#user_discount as JS
and generates expected results, but fails in the rendering process, complaining about (with initial stack)
ActionController::UnknownFormat (UsersController#user_discount is missing a template for this request format and variant.
request.formats: ["text/javascript", "*/*"]
request.variant: []):
actionpack (6.1.3.2) lib/action_controller/metal/implicit_render.rb:42:in `default_render'
actionpack (6.1.3.2) lib/action_controller/metal/basic_implicit_render.rb:6:in `block in send_action'
actionpack (6.1.3.2) lib/action_controller/metal/basic_implicit_render.rb:6:in `tap'
actionpack (6.1.3.2) lib/action_controller/metal/basic_implicit_render.rb:6:in `send_action'
Yet that template clearly exists in the proper path (its corresponding partial is also present in the directory) as shown here:
This is perplexing as the warning message is contrary to the reality. The only assumptions that come to mind are that of some handling of form_with cmobined with the scope or the question of variant
What is mistaken here?

Google Cloud Talent Solution Exception: Google::Cloud::PermissionDeniedError

Using Google Cloud Talent Solution (aka Google Cloud Job Discovery), I am getting the following stack trace even though all Google API credentials are in place and enabled. Error message seems to be generic ("Permission denied")and not sure what could be wrong. Any insight is much appreciated.
====================================================================
Error message
Google::Cloud::PermissionDeniedError: 7:User not authorized to perform this action.
Sample stack trace (hide Rails)
…cloud-pubsub-0.31.0/lib/google/cloud/pubsub/
service.rb: 459:in rescue in execute'
…cloud-pubsub-0.31.0/lib/google/cloud/pubsub/
service.rb: 455:inexecute'
…cloud-pubsub-0.31.0/lib/google/cloud/pubsub/
service.rb: 150:in publish'
…e-cloud-pubsub-0.31.0/lib/google/cloud/pubsub/
topic.rb: 573:inpublish_batch_messages'
…e-cloud-pubsub-0.31.0/lib/google/cloud/pubsub/
topic.rb: 314:in publish'
…emetry_job_google/lib/google_jobs/
google_jobs_event.rb: 22:inpublish_event'
…metry_careersites/app/mixins/
job_search_event_mixin.rb: 88:in record_google_search_impressions'
…metry_careersites/app/mixins/
job_search_event_mixin.rb: 17:inevent_job_search_impressions'
…ersites/app/controllers/public_site/
jobs_controller.rb: 59:in block (2 levels) in search'
…k-5.2.1.1/lib/action_controller/metal/
mime_responds.rb: 203:inrespond_to'
…ersites/app/controllers/public_site/
jobs_controller.rb: 53:in search'
…1/lib/action_controller/metal/
basic_implicit_render.rb: 6:insend_action'
…ems/actionpack-5.2.1.1/lib/abstract_controller/
base.rb: 194:in process_action'
…npack-5.2.1.1/lib/action_controller/metal/
rendering.rb: 30:inprocess_action'
…ctionpack-5.2.1.1/lib/abstract_controller/
callbacks.rb: 42:in block in process_action'
…/activesupport-5.2.1.1/lib/active_support/
callbacks.rb: 109:inblock in run_callbacks'
…try_careersites/app/controllers/
eventing_controller.rb: 23:in block in append_tracking_info_to_rails_log'
…vesupport-5.2.1.1/lib/active_support/
tagged_logging.rb: 71:inblock in tagged'
…vesupport-5.2.1.1/lib/active_support/
tagged_logging.rb: 28:in tagged'
…vesupport-5.2.1.1/lib/active_support/
tagged_logging.rb: 71:intagged'
…try_careersites/app/controllers/
eventing_controller.rb: 22:in append_tracking_info_to_rails_log'
…/activesupport-5.2.1.1/lib/active_support/
callbacks.rb: 118:inblock in run_callbacks'
…/activesupport-5.2.1.1/lib/active_support/
callbacks.rb: 136:in run_callbacks'
…ctionpack-5.2.1.1/lib/abstract_controller/
callbacks.rb: 41:inprocess_action'
…tionpack-5.2.1.1/lib/action_controller/metal/
rescue.rb: 22:in process_action'
<truncated 60 additional frames>
…2.5.0/gems/rack-timeout-0.5.1/lib/rack/timeout/
core.rb: 123:inblock in call'
…rack-timeout-0.5.1/lib/rack/timeout/support/
timeout.rb: 19:in timeout'
…2.5.0/gems/rack-timeout-0.5.1/lib/rack/timeout/
core.rb: 122:incall'
…k-5.2.1.1/lib/action_dispatch/middleware/
request_id.rb: 27:in call'
…ruby/2.5.0/gems/rack-2.0.6/lib/rack/
method_override.rb: 22:incall'
…/vendor/ruby/2.5.0/gems/rack-2.0.6/lib/rack/
runtime.rb: 22:in call'
…ctive_support/cache/strategy/
local_cache_middleware.rb: 29:incall'
…ack-5.2.1.1/lib/action_dispatch/middleware/
executor.rb: 14:in call'
…npack-5.2.1.1/lib/action_dispatch/middleware/
static.rb: 127:incall'
…vendor/ruby/2.5.0/gems/rack-2.0.6/lib/rack/
sendfile.rb: 111:in call'
…r/ruby/2.5.0/gems/railties-5.2.1.1/lib/rails/
engine.rb: 524:incall'
…/ruby/2.5.0/gems/puma-3.12.0/lib/puma/
configuration.rb: 225:in call'
…/vendor/ruby/2.5.0/gems/puma-3.12.0/lib/puma/
server.rb: 658:inhandle_request'
…/vendor/ruby/2.5.0/gems/puma-3.12.0/lib/puma/
server.rb: 472:in process_client'
…/vendor/ruby/2.5.0/gems/puma-3.12.0/lib/puma/
server.rb: 332:inblock in run'
…or/ruby/2.5.0/gems/puma-3.12.0/lib/puma/
thread_pool.rb: 133:in `block in spawn_thread
You will need to explicitly connect the service account to the Talent Solution api and then use that service account key for the auth. See here for details.

404 when sending JSON to RoR with curl

I'm very new to RoR, and I'm learning on the fly. With that being said, I'm trying to parse a POST request sent from Formstack to my script. I can view and run the script by going to it's URL and it works just fine, put when I point the Webhook in Formstack to the URL, nothing happens. I ran the below curl request from my command line, and it returned a 404 error.
curl -H "Content-Type: application/json" -d '{"FormID":"1234","UniqueID":"1234","Name":{"first":"John","last":"Smith"},"Email":"test#gmail.com","Phone":"(555) 555-555","Company":"Test Company"}' -X POST "http://scripturl.com"
I checked the data being sent with requestb.in, and everything is being sent properly. So why would it return a 404 error when JSON data is being sent, but run just fine when I view it in my browser? My sample code is below.
firstName = params[:Name]["first"]
lastName = params[:Name]["last"]
email = params["Email"]
company = params["Company"]
phone = params["Phone"]
client.create('contact', firstname: firstName, lastname: lastName, emailaddress1: email, subject: "Test", companyname: company, mobilephone: phone)
Here is the contents of log/production.log:
rack (1.6.4) lib/rack/sendfile.rb:113:in `call'
railties (4.2.3) lib/rails/engine.rb:518:in `call'
railties (4.2.3) lib/rails/application.rb:165:in `call'
passenger (5.0.13) lib/phusion_passenger/rack/thread_handler_extension.rb:94:in `process_request'
passenger (5.0.13) lib/phusion_passenger/request_handler/thread_handler.rb:157:in `accept_and_process_next_request'
passenger (5.0.13) lib/phusion_passenger/request_handler/thread_handler.rb:110:in `main_loop'
passenger (5.0.13) lib/phusion_passenger/request_handler.rb:415:in `block (3 levels) in start_threads'
passenger (5.0.13) lib/phusion_passenger/utils.rb:111:in `block in create_thread_and_abort_on_exception'
I found the problem. I had to add the following into config/routes.rb:
post '/add' => 'contacts#add'
And then in controllers/application_controller.rb, you need to replace this:
protect_from_forgery with: :exception
With this:
protect_from_forgery with: :null_session

production-only error ActionView::Template::Error (can't convert Symbol into Integer)

I am attempting to deploy a site onto engineyard and I keep getting an error that seems to be a data formatting error. This only happens when deployed as production onto the server cluster. using the same DB and codebase locally or on a VPS we have no issues.
The errors seem to me to show that suddenly there is an array / hash type mismatch with some of the user data. It always seems to trace back to the model where it is tasked to get 'options'. The errors are triggered by the templates calling for access of checks on user attributes.
production log:
Completed 500 Internal Server Error in 32ms
** [Airbrake] Failure: Net::HTTPClientError
ActionView::Template::Error (can't convert Symbol into Integer):
18: = admin_user.username
19: = username_helper(admin_user)
20: %td= country_image admin_user
21: %td= admin_user.account_name
22: %td
23: - if admin_user.is_a?(Creative)
24: - if admin_user.is_juror?
app/models/user.rb:79:in `[]'
app/models/user.rb:79:in `has_option?'
app/models/contest_holder.rb:77:in `is_client_type?'
app/models/contest_holder.rb:81:in `is_agent?'
app/models/contest_holder.rb:70:in `ao_client_name'
app/models/contest_holder.rb:115:in `account_name'
app/views/admin/users/_users.html.haml:21:in `block in _app_views_admin_users__users_html_haml__845309245401675393_70184787547360'
trace:
ActionView::Template::Error: can't convert Symbol into Integer
Sample stack trace (show Rails)
/app/models/user.rb: 79:in `[]'
/app/models/user.rb: 79:in `has_option?'
…P/releases/20130814153250/app/models/contest_holder.rb: 77:in `is_client_type?'
…P/releases/20130814153250/app/models/contest_holder.rb: 81:in `is_agent?'
…P/releases/20130814153250/app/models/contest_holder.rb: 70:in `ao_client_name'
…P/releases/20130814153250/app/models/contest_holder.rb: 115:in `account_name'
…1/gems/haml-4.0.3/lib/haml/helpers/action_view_mods.rb: 10:in `block in render_with_haml'
…ed_gems/ruby/1.9.1/gems/haml-4.0.3/lib/haml/helpers.rb: 89:in `non_haml'
…1/gems/haml-4.0.3/lib/haml/helpers/action_view_mods.rb: 10:in `render_with_haml'
…ms/ruby/1.9.1/gems/journey-1.0.4/lib/journey/router.rb: 68:in `block in call'
…ms/ruby/1.9.1/gems/journey-1.0.4/lib/journey/router.rb: 56:in `each'
…ms/ruby/1.9.1/gems/journey-1.0.4/lib/journey/router.rb: 56:in `call'
…uby/1.9.1/gems/omniauth-1.1.4/lib/omniauth/strategy.rb: 184:in `call!'
…uby/1.9.1/gems/omniauth-1.1.4/lib/omniauth/strategy.rb: 164:in `call'
…uby/1.9.1/gems/omniauth-1.1.4/lib/omniauth/strategy.rb: 184:in `call!'
…uby/1.9.1/gems/omniauth-1.1.4/lib/omniauth/strategy.rb: 164:in `call'
…uby/1.9.1/gems/omniauth-1.1.4/lib/omniauth/strategy.rb: 184:in `call!'
…uby/1.9.1/gems/omniauth-1.1.4/lib/omniauth/strategy.rb: 164:in `call'
…ruby/1.9.1/gems/omniauth-1.1.4/lib/omniauth/builder.rb: 49:in `call'
…VP/releases/20130814153250/lib/middleware/force_ssl.rb: 8:in `call'
…ems/ruby/1.9.1/gems/warden-1.2.1/lib/warden/manager.rb: 35:in `block in call'
…ems/ruby/1.9.1/gems/warden-1.2.1/lib/warden/manager.rb: 34:in `catch'
…ems/ruby/1.9.1/gems/warden-1.2.1/lib/warden/manager.rb: 34:in `call'
…ndled_gems/ruby/1.9.1/gems/rack-1.4.5/lib/rack/etag.rb: 23:in `call'
…/ruby/1.9.1/gems/rack-1.4.5/lib/rack/conditionalget.rb: 25:in `call'
…/1.9.1/gems/rack-1.4.5/lib/rack/session/abstract/id.rb: 210:in `context'
…/1.9.1/gems/rack-1.4.5/lib/rack/session/abstract/id.rb: 205:in `call'
…es/20130814153250/config/initializers/session_store.rb: 10:in `call'
code from user.rb which seems to be the point of contact for the error:
def has_option?(option)
self.options[option] && self.options[option].to_s == "1"
end
an example of an options value:
--- !map:ActiveSupport::HashWithIndifferentAccess is_agent: "1"
Came to find out that the issue was an additional line break within the database file. Specifically the application uses serialized yaml data a fair amount. An additional line break had been added between the yaml hash key and value causing an error within the object. We initially tried creating a script to cleanse the db and came to find it seemed to be simply corrupt from the original db file.
This was what wrong with my case
Error : ActionView::Template::Error (can't convert Symbol into Integer)
Reason:
In my haml.
I was trying to access a property from a json object.
object[:key]
But actually it was a json array(Array of entities from active record where clause). I just filtered the first element.
Hope someone will find useful.

ActiveModel and ActionMailer

Rails 3.1.2 with mail gem 2.3.0
I'm trying to use ActionMailer with an ActiveModel based form. I've defined my table less model like so:
class StprodApp
include ActiveModel::Validations
include ActiveModel::Conversion
extend ActiveModel::Naming
attr_accessor :email,:name
attr_reader :errors
validates_presence_of :email, :message => "Please enter your email address"
def initialize(attributes = {})
attributes.each do |name, value|
send("#{name}=", value)
end
#errors = ActiveModel::Errors.new(self)
end
def persisted?
false
end
end
So far so good, I can post a form based on stprod_app, and validations work
So now I want to send an email based off this model, so I do this in my create action:
#stprod_app = StprodApp.new(params[:stprod_app])
if #stprod_app.valid?
EmployeeMailer.stprod_app(#stprod_app).deliver
end
I get this error
undefined method `index' for #<StprodApp:0x007f95325a1ad0>
and this stack trace
mail (2.3.0) lib/mail/encodings.rb:117:in `value_decode'
mail (2.3.0) lib/mail/encodings.rb:101:in `decode_encode'
mail (2.3.0) lib/mail/fields/unstructured_field.rb:74:in `do_decode'
mail (2.3.0) lib/mail/fields/unstructured_field.rb:56:in `decoded'
mail (2.3.0) lib/mail/fields/unstructured_field.rb:107:in `wrapped_value'
mail (2.3.0) lib/mail/fields/unstructured_field.rb:70:in `do_encode'
mail (2.3.0) lib/mail/fields/unstructured_field.rb:52:in `encoded'
mail (2.3.0) lib/mail/field.rb:123:in `method_missing'
mail (2.3.0) lib/mail/header.rb:190:in `block in encoded'
mail (2.3.0) lib/mail/header.rb:189:in `each'
mail (2.3.0) lib/mail/header.rb:189:in `encoded'
mail (2.3.0) lib/mail/message.rb:1708:in `encoded'
actionmailer (3.1.2) lib/action_mailer/base.rb:451:in `set_payload_for_mail'
actionmailer (3.1.2) lib/action_mailer/base.rb:431:in `block in deliver_mail'
activesupport (3.1.2) lib/active_support/notifications.rb:53:in `block in
instrument'
activesupport (3.1.2) lib/active_support/notifications/instrumenter.rb:21:in
`instrument'
activesupport (3.1.2) lib/active_support/notifications.rb:53:in `instrument'
actionmailer (3.1.2) lib/action_mailer/base.rb:430:in `deliver_mail'
mail (2.3.0) lib/mail/message.rb:230:in `deliver'
app/controllers/stprod_app_controller.rb:20:in `create'
So I read the docs for the mail gem, and it says this:
---snip---
All objects that can render into an email, have an #encoded method. Encoded will return the object as a complete string ready to send in the mail system, that is, it will include the header field and value and CRLF at the end and wrapped as needed.
All objects that can render into an email, have a :decoded method. Decoded will return the object's "value" only as a string. This means it will not include the header fields (like 'To:' or 'Subject:').
By default, calling #to_s on a container object will call its encoded method, while #to_s on a field object will call it's decoded method. So calling #to_s on a Mail object will return the mail, all encoded ready to send, while calling #to_s on the From field or the body will return the decoded value of the object. The header object of Mail is considered a container. If you are in doubt, call #encoded, or #decoded explicitly, this is safer if you are not sure.
Structured fields that have parameter values that can be encoded (e.g. Content-Type) will provide decoded parameter values when you call the parameter names as methods against the object.
Structured fields that have parameter values that can be encoded (e.g. Content-Type) will provide encoded parameter values when you call the parameter names through the object.parameters[''] method call.
---snip---
It looks like the index error is Rail's last desperate attempt to decode/encode? the email. The only 'index' I found in rails that makes sense is in ActiveSupport::Multibyte::Chars.
It sounds like I have to implement an encode/decode method in my ActiveModel, but I can't figure out how!
Anybody know how to define an ActiveModel that works with ActionMailer?
Solved, left off a crucial detail, and therin lay the problem
I was passing the Stprod_app instance variable to the mail call:
def stprod_app(emp,app)
#app = app
#emp = emp
mail(:to=> #app.antibot,
:from=>#emp.email,
:app=>#app) # <<< DOH!!!!
Sorry!
I had a similar error from this code:
def report( request )
headers['environment'] = RAILS_ENV
headers['identifier'] = request.id
...
end
In this case, it could not encode request.id because it was numeric. Changing it to request.id.to_s solved the problem.

Resources