I am trying to connect a Rails application with the Bigcommerce API but I can't and I don't know why.
I made the next:
Add 'gem bigcommerce' to my gem file
Execute 'bundle install'
Put the code behind in the ApplicationController index
def index
api = Bigcommerce::Api.new({
:store_url => "http://mystore.mybigcommerce.com",
:username => "user",
:api_key => "3a0ce...[my api key]"
})
puts api.time
end
Stop and Start Rails server
I received an error saying 'undefined method `time' for #'
Does any body could tell me what I am doing wrong and how to configure Bigcommerce in a Rails app? I found the official documentation, but it's not very clear for me.
Thanks in advance
Have you added require at the beggining of your ApplicationController file?
require 'bigcommerce'
Related
I am new to ruby on rails. I apologize early if this question might be dumb... but I can't figure this out and the tutorials I've seen is making this simple task confusing. Or I am just not understanding what I am missing or what to do... if a step by step tutorial may help, thanks!
My task to make a rails api that you can pass JSON to via postman/DH.
The payload will be {"value": "foo"}. A '200 (OK)' Response with payload of {"value":"bar"}
No database needed, just a controller that takes in a payload and gives back a response.
This is what I do know...
What I do know is how to create a rails api by "rails new testrails --api"
Then going to my 'gemfile' and installing some kind of json to gems.
Then... I mess with the 'route.rb' file in config and the controller in'apps'.
I get lost here on what I need to do with the route file and controller...
What ever steps come next.
Step 1: install Rails 5
$ gem install rails
Step 2: create an API-only Rails application
$ rails new my_app --api
Step 3: integrate rack-cors
Add gem 'rack-cors' at the bottom of your $RAILS_ROOT/Gemfile ($RAILS_ROOT stands for the root directory of your rails app), then
$ bundle install
Then add these lines to your $RAILS_ROOT/config/application.rb, inside the class definition
config.middleware.insert_before 0, Rack::Cors do
allow do
origins '*'
resource '*', :headers => :any, :methods => [:get, :post, :patch, :put, :delete, :options]
end
end
Step 4: generate a controller
$ rails g controller foo
Add an action to the FooController (in file $RAILS_ROOT/controllers/foo_controller.rb)
def create
foo = params[:foo]
# Do whatever you want with foo
render json: {value: 'bar'}
end
Step 5: add a route
Modify the file $RAILS_ROOT/config/routes.rb, add
post '/foo' => 'foo#index'
in the block
Step 6: start rails server
$ rails s
That's all. Now you can send POST requests to http://localhost:3000/foo and see what happens.
I have been learning Ruby on Rails, but I still have issues when it comes to Ruby gems with examples that are irb based and not Rails or Sinatra framework based. I am trying to implement the Block.io Bitcoin API functionality. But the code I find is Ruby only, so I am not sure where to create a config file for the API Key and also whether I need to create a controller to make this work in the views for Rails.
The gem and examples are on: https://github.com/BlockIo/gem-block-io
I installed this gem via bundle install on Rails
gem install block_io -v=1.0.6
The Ruby example show the following:
>> require 'block_io'
>> BlockIo.set_options :api_key=> 'API KEY', :pin => 'SECRET PIN', :version => 2
In Rails which config file would I enter the above api_key and pin?
In the example they show the code to get your address as follows:
BlockIo.get_my_address
Do I need to create a function in a controller such as:
def address
#my_address = BlockIo.get_my_addresses
end
and in the view use:
<%= #my_address %>
I need some guidance with regards to the above, any comment or assistance will be greatly appreciated.
require 'block_io' can go into Gemfile like gem 'block_io'. Rails/bundler will require it automaticaly for you as long as the gem name is also the file name you want to require from this gem.
BlockIo.set_options :api_key=> 'API KEY', :pin => 'SECRET PIN', :version => 2 can be put into an initilizer like config/initializers/block_io.rb. This way set_options is called only once when Rails starts a server or console or runner.
Put it like this into the file config/initializers/block_io.rb
BlockIo.set_options :api_key=> ENV['BLOCK_IO_API_KEY'], :pin => ENV['BLOCK_IO_PIN'], :version => 2
With the environment variables in use you don't commit any secret into your repo.
Now you should be able to call BlockIo.get_my_address within any action.
There is the following problem: I'm developing some Rails application on my local machine, and all is good, app works, but after uploading on Heroku there would be the following error (I saw it using 'heroku logs'):
NameError (uninitialized constant Api::V1::ApiV1Controller::UndefinedTokenTypeError)
My code:
def require_token
begin
Some code which generates UndefinedTokenTypeError
rescue UndefinedTokenTypeError => e
render json: e.to_json
end
end
UndefinedTokenTypeError is in lib/errors.rb file:
class EmptyCookieParamsError < StandardError
def to_json
{ result_code: 1 }
end
end
class UndefinedTokenTypeError < StandardError
def to_json
{ result_code: 2 }
end
end
I've got the same version for Rails/Ruby on my local machine (2.0). How can I fix it? Thanks.
From what I can see, you may be experiencing either a CORS-related issue or you're not authenticating properly
Cross Origin Resource Sharing
CORS is a standard HTML protocol, which basically governs which websites can "ping" your site. Facebook & Twitter's third-party widgets only work because they allow any site to send them data
For Rails to work with CORS, it's recommended to install the Rack-CORS gem. This will allow you to put this code in your config/application.rb file:
#CORS
config.middleware.use Rack::Cors do
allow do
origins '*'
resource '/data*', :headers => :any, :methods => :post
end
end
Because you're experiencing these issues on Heroku, it could be the problem you're experiencing. Even if it isn't, it's definitely useful to appreciate how CORS works
Authentication
Unless your API is public, you'll likely be authenticating the requests
The way we do this is with the authenticate_or_request_with_http_token function, which can be seen here:
#Check Token
def restrict_access
authenticate_or_request_with_http_token do |token, options|
user = User.exists?(public_key: token)
#token = token if user
end
end
We learnt how to do this with this Railscast, which discusses how to protect an API. The reason I asked about your code was because the above works for us on Heroku, and you could gain something from it!
Running on Heroku will be using the production environment. Check to see what is different between environments/development.rb and environments/production.rb
You can try running your app in production mode on your local machine, rails server -e production
I am guessing your config.autoload_paths isn't set correctly. Should be in config/application.rb
I've been at this for awhile. I've tried using net http to connect to a https url with no luck. I've tried http party and getting uninitialized constant httpparty. I am running rails 3.2.7. I have httpparty add to gem file.
Can someone please provide an example with either problem sending params to a https url and receiving a response.
code:
options = {:id => params[:id], :code => params[:code]}
response = HTTPParty.post('https://test.com', options)
gem file:
gem "httparty", "~> 0.9.0"
HTTParty.get('https://google.com', :query => {:q => 'stack overflow'})
If this doesn't work for you, please show the code you're using. It you're getting an uninitialized constant error, you've done something wrong. Did you run bundle after adding to your Gemfile?
Also, it's HTTParty, not HTTPParty.
I'm trying to use oauth-plugin on a Rails application I'm developing, but I keep running into problems.
To make sure I'm not making any mistake, I started an application from scratch (using Rails 3.0.3). Here are the steps I followed:
Create da new rails application (rails.test)
Edited its Gemfile to include:
gem "oauth-plugin", ">=0.4.0.pre1"
gem "oauth", "0.4.4"
Generated oauth-consumer, by running script/rails g oauth_consumer
Edited oauth_consumers.rb to include my keys for Google integration:
:google=>{
:key=>"anonymous",
:secret=>"anonymous",
:scope=>"https://www.google.com/calendar/feeds/",
:options => {
:site => "http://www.google.com",
:request_token_path => "/accounts/OAuthGetRequestToken",
:access_token_path => "/accounts/OAuthGetAccessToken",
:authorize_path=> "/accounts/OAuthAuthorizeToken"
},
}
Edited routes.rb to add the route for oauth_consumer:
resources :oauth_consumers
Edited application_controller.rb to implement the logged_in? method as follows:
def logged_in?
true
end
Now when I access http://localhost:3000/oauth_consumers/google I get the following error:
uninitialized constant GoogleToken
Does anyone know what causes this error and how can I fix it? GoogleToken is a class that should have been auto generated by oauth-plugin, so I can't tell why I'm getting this uninitialized constant error.
The GoogleToken class doesn't get auto-generated unless you pass "google" to the generator like so:
script/rails g oauth_consumer google
or for rails 3:
rails g oauth_consumer google
Also check to ensure the relationship is set up in the user model like so:
has_one :google, :class_name => "GoogleToken", :dependent => :destroy
Did you remember to run bundle install from terminal after editing your Gemfile? Sounds like your Rails app doesn't know about these gems yet.
I have the same problem, i think a solution could be in this:
https://github.com/pelle/oauth-plugin/blob/master/lib/generators/oauth_consumer/USAGE
You need some sort of authentication like restful-authentication plugin, if you uncomment line 27..29 in your oauth_consumers_controller.rb file, you'll jump to next step!