I am using Ruby on Rails. I am trying to implement https://www.kraken.com/help/api
Incase it isn't obvious, my knowledge of implementing API's and gems is very basic.
I go to the "example-api-code" paragraph and I get to https://github.com/leishman/kraken_ruby
I include the gem and do the bundle.
Now we get to "Usage" "Create A Client"
I assume I put the following in config/kraken.rb
API_KEY = '3bH+M/nLp......'
API_SECRET = 'wQG+7Lr9b.....'
kraken = Kraken::Client.new(API_KEY, API_SECRET)
time = kraken.server_time
time.unixtime #=> 1393056191
I wanted to test something simple, such as displaying the time.
I put the following code in my views/welcome/index.html.erb file, but then I get an error.
<p><%= kraken.server_time %></p>
-> ActionView::Template::Error (undefined local variable or method `kraken' for #<#<Class:0x007f95c6246ba8>:0x007f95c6245eb0>):
Trying this code in my html.erb file and it gives me another error
<p><%= #kraken.server_time %></p>
-> ActionView::Template::Error (undefined method `server_time' for nil:NilClass):
-------------------------------------------------------------------------------
EDIT: Solutions Attempts, TLDR NameError (uninitialized constant...
# Gladis
Using your solution I get this error
->! Unable to load application: SyntaxError: /app/app/controllers/welcome_controller.rb:5: dynamic constant assignment
-> API_KEY = '...'
So I tried
def index
#kraken = Kraken::Client.new('3bH+M/nLp...', 'wQG+7Lr9b...')
time = #kraken.server_time
time.unixtime #=> 1393056191
end
and got this new error
NameError (uninitialized constant WelcomeController::Kraken):
app/controllers/welcome_controller.rb:5:in `index'
# Pavel Tkackenko
Your first solution (wrapping in singleton-like class) gives me this error.
ActionView::Template::Error (uninitialized constant ActionView::CompiledTemplates::KrakenClient):
app/views/welcome/index.html.erb:1:in `_app_views_welcome_index_html_erb__3584347874708863751_70226442404480'
1: <p><%= KrakenClient.get.server_time %></p>
Your second solutions (monkey-patch) gives me the this error.
ActionView::Template::Error (uninitialized constant ActionView::CompiledTemplates::Kraken):
1: <p><%= Kraken.client.server_time %></p>
app/views/welcome/index.html.erb:1:in `_app_views_welcome_index_html_erb___3719740865851336982_69820265644620'
Moving /config/kraken.rb to /config/initializers/kraken.rb using Pavel's method
Wrapping it in singleton-like class gives me this error (infact, it doesn't even let me host the server)
->When I host it on my local computer with bin/rails server
/config/initializers/kraken.rb:6:in `<class:KrakenClient>': uninitialized constant KrakenClient::Kraken (NameError)
/config/initializers/kraken.rb:1:in `<top (required)>'
->When I host it on heroku
Running: rake assets:precompile
rake aborted!
NameError: uninitialized constant KrakenClient::Kraken
/config/initializers/kraken.rb:5:in `<class:KrakenClient>'
/config/initializers/kraken.rb:1:in `<top (required)>'
...
/config/environment.rb:5:in `<top (required)>'
With monkey-patch I get this error
ActionView::Template::Error (uninitialized constant Kraken::Client):
1: <p><%= Kraken.client.server_time %></p>
config/initializers/kraken.rb:8:in `client'
app/views/welcome/index.html.erb:1:in `_app_views_welcome_index_html_erb___577296263292451462_70097201303100'
If you got it to work on your computer, I would be happy to use your code as a skeleton (I am assuming this will be easier than figuring out what's wrong on my side).
Put this code in WelcomeController.rb
def index
API_KEY = '3bH+M/nLp......'
API_SECRET = 'wQG+7Lr9b.....'
#kraken = Kraken::Client.new(API_KEY, API_SECRET)
time = #kraken.server_time
time.unixtime #=> 1393056191
end
In view under this controller put:
<p><%= #kraken.server_time %></p>
kraken = Kraken::Client.new(API_KEY, API_SECRET)
kraken here is local variable. If you put it into config/kraken.rb, it will not be accessible outside.
There are different solutions. One is to wrap it in singleton-like class:
# config/initializers/kraken.rb
class KrakenClient
API_KEY = '3bH+M/nLp......'
API_SECRET = 'wQG+7Lr9b.....'
##config = Kraken::Client.new(API_KEY, API_SECRET)
def self.get
##config
end
end
# index.html.erb
<p><%= KrakenClient.get.server_time %></p>
Another one approach is to monkey-patch Kraken itself:
# config/initializers/kraken.rb
class Kraken
API_KEY = '3bH+M/nLp......'
API_SECRET = 'wQG+7Lr9b.....'
class << self
def client
#client ||= Kraken::Client.new(API_KEY, API_SECRET)
end
end
end
# index.html.erb
<p><%= Kraken.client.server_time %></p>
Related
I don't know that much about RUBY, just thought that you guys might help me with this. I'm using Storyblok as my headless CMS and JEKYLL when I'm building serve it this is the error that I got;
33: from C:/project/test/_plugins/storyblok_generator.rb:8:in `generate'
32: from C:/project/test/_plugins/storyblok_cms/generator.rb:12:in `generate!'
C:/project/test/vendor/cache/ruby/2.7.0/gems/storyblok-3.0.1/lib/storyblok/client.rb:354:in `block (2 levels) in find_and_fill_links': undefined method `[]' for nil:NilClass (NoMethodError)
the code below is from _plugins/storyblok_cms/generator.rb
def generate!
timestamp = Time.now.to_i
links = client.links(cv: timestamp)['data']['links']
stories = client.stories(per_page: 100, page: 1, cv: timestamp)['data']['stories'] #line 12
stories.each do |story|
# create all pages except global (header,footer,etc.)
content_type = story['content']['component']
if content_type != 'shared'
site.pages << create_page(site, story, links)
end
rescue UnknownContentTypeError => e
# for production, raise unknown content type error;
# for preview and other environments, issue an warning only since the content_type might be available
# but the code handling that content type might be in a different branch.
Jekyll.env == 'production' ? raise : Jekyll.logger.warn(e.message)
end
site.data['stories'] = stories
site.data['articles'] = stories.select { |story| story['full_slug'].start_with?('articles') }
site.data['shared'] = stories.select { |story| story['full_slug'].start_with?('shared') }
end
the code below is from _plugins/storyblok_generator.rb
require "storyblok"
module Jekyll
class StoryblokGenerator < Jekyll::Generator
safe true
def generate(site)
StoryblokCms::Generator.new(site).generate! #line 8
end
end
end
Additional Info:
ruby version: ruby 2.7.4p191 (2021-07-07 revision a21a3b7d23) [x64-mingw32]
jekyll version: jekyll 4.2.1
OS: Windows 10
I've actually found the solution to this, So this error occurs because I have created a page template in Block Library in Storyblok and then firing bundle exec jekyll serve command in terminal without creating the page template source/file in my project directory.
So I have an about_us block (content-type) in Block Library created and then when I fire bundle exec jekyll serve without creating an about_us.html first in _layouts folder, It would trigger the error.
Solution;
Make sure to create the source/file first in the _layouts folder if you have created a block (content-type) in block library.
I have defined actions and rules to reward users thanks to merit gem.
However, It does not work. After having execute the rewarded actions , the user does not take any points.
Here is my code:
first , here is my view:
<div class="col-md-3 text-center">
<p class="dark-blue"><%= #user.points %></p>
<p class="grey-light">Points</p>
</div>
Secondly, here is the point_rules model:
#Model/merit/point_rules.rb
module Merit
class PointRules include Merit::PointRulesMethods
def initialize
score 15, on: 'pages#create', to: [:user]
end
end
end
Thirdly, the method create in the pages Controller:
def create
#comment = Comment.new
#comment.title = params[:title]
#comment.content = params[:content]
#comment.created_at = params[:created_at]
#comment.score = 0
#comment.author = User.find(session[:user_id]).user_name
#comment.user = User.find(session[:user_id])
#comment.save
redirect_to "/feed"
end
Finally,the merit.rb:
#config/initializers/merit.rb
Merit.setup do |config|
# Check rules on each request or in background
config.checks_on_each_request = true
# Define ORM. Could be :active_record (default) and :mongoid
config.orm = :active_record
# Add application observers to get notifications when reputation changes.
# config.add_observer 'MyObserverClassName'
# Define :user_model_name. This model will be used to grand badge if no
# `:to` option is given. Default is 'User'.
config.user_model_name = 'User'
# Define :current_user_method. Similar to previous option. It will be used
# to retrieve :user_model_name object if no `:to` option is given. Default
# is "current_#{user_model_name.downcase}".
config.current_user_method = 'current_user'
# Add application observers to get notifications when reputation changes.
# config.add_observer 'MyObserverClassName'
end
Furthermore, Forest tells my in the CLI that "The association "sash" does not seem to exist for model "Merit::ActivityLog".
How to solve that?
thanks you in advance.
EDIT :
here are the stack traces of the possible error[merit] no target found: uninitialized constant Page.
/Users/charbe/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/merit-3.0.1/lib/merit/base_target_finder.rb:13:in `find'
[merit] no target found: uninitialized constant Page. /Users/charbe/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/merit-3.0.1/lib/merit/base_target_finder.rb:13:in `find'
[merit] no target found: uninitialized constant Page. /Users/charbe/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/merit-3.0.1/lib/merit/base_target_finder.rb:13:in `find'
[merit] no target found: uninitialized constant Page. /Users/charbe/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/merit-3.0.1/lib/merit/base_target_finder.rb:13:in `find'
[merit] no target found: uninitialized constant Page. /Users/charbe/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/merit-3.0.1/lib/merit/base_target_finder.rb:13:in `find'
[merit] no target found: uninitialized constant Page. /Users/charbe/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/merit-3.0.1/lib/merit/base_target_finder.rb:13:in `find'
[merit] NoMethodError on `TrueClass#user` (called from Merit::TargetFinder#other_target)
[merit] NoMethodError on `TrueClass#user` (called from Merit::TargetFinder#other_target)
Merit doesn't work with Rails 4.2.
https://github.com/merit-gem/merit/issues/301
You have to use
gem 'merit', '~>2.4'
Also the error NoMethodError on 'TrueClass#user' means that something is returning true when it is expecting an object that responds to #user.
How to make rake-offline work in rails 3.2.11 ?
I added initializer
offline = Rack::Offline.configure do
#cache "images/masthead.png"
public_path = Rails.public_path
Dir[public_path.join("javascripts/*.js")].each do |file|
cache file.relative_path_from(public_path)
end
network "/"
end
I added in routes
match "/application.manifest" => Rails::Offline
Rack::Offline.configure do
cache "assets/application.js"
cache "assets/application.css"
network "/"
end
and added manifest in html tag.
It throws error
/initializers/offline.rb:5:in `block in <top (required)>': undefined method `join' for "/Sites/Ruby/project/public":String (NoMethodError)
In Rails 3.2.11, Rails.public_path returns a String, not a Pathname object. (It looks like Rails master has it returning a Pathname object which is why rack-offline's documentation might say to use it that way).
Try this instead:
public_path = Pathname.new(Rails.public_path)
See https://github.com/wycats/rack-offline/issues/7
I just added a new model to my app named Collector.
I'm trying to make some data changes using the console on my server:
GsCollector.all.each do |gsc|
coll = Collector.new
coll.project_id = gsc.project_id
coll.kind = 'GsCollector'
coll.optional = gsc.optional
coll.included = gsc.included
coll.save
gsc.collector_id = coll.id
gsc.save
gsc.custom_descriptions.each do |cd|
cd.collector_id = coll.id
cd.save
end
gsc.order_notes.each do |note|
note.collector_id = coll.id
note.save
end
end
But this fails with:
GsCollector Load (0.3ms) SELECT `gs_collectors`.* FROM `gs_collectors`
NameError: uninitialized constant Collector
from (irb):2:in `block in irb_binding'
from (irb):1:in `each'
from (irb):1
What's wrong here? These commands in the console on my dev machine worked fine. I did migrate the database on the server.
Run the The command
reload!
Or restart your terminals to get good result
I know this has to be something stupid, but I keep getting the following error in one of my examples:
undefined method `new' for #<Class:0x211d274>
I have created a simple example to show the error:
describe LateCharge do
before :each do
#membership = Membership.new
#location = mock_model(Location, :late_payment_rate => 10)
end
it "should initialize" do
LateCharge.respond_to?('new').should == true
#charge = LateCharge.new(#membership, #location)
end
end
The strange part is, when I run the example by itself, it passes. When I run it with all my examples, it fails with the following error:
NoMethodError in 'LateCharge should initialize'
undefined method `new' for #<Class:0x211d274>
/Library/Ruby/Gems/1.8/gems/activerecord-2.3.2/lib/active_record/base.rb:1964:in `method_missing_without_paginate'
/Users/l33/.gem/ruby/1.8/gems/mislav-will_paginate-2.3.11/lib/will_paginate/finder.rb:170:in `method_missing'
./spec/models/late_charge_spec.rb:15:
It is failing on the line: #charge = LateCharge.new(#membership, #location)
I do not have any problems instantiating the LateCharge object at run time or from the console.
Anyone have any ideas?
Seems to me the following information is key to your issue:
will_paginate/finder.rb:170:in `method_missing'
Hey Lee - not sure if you're still having this problem, but I had the exact same thing and it's because another spec I had was unstubbing the function.