我是新手编写代码以支持我正在开发的应用程序的第三方(Transaction Express)API调用。我一直在关注这个教程:https://revs.runtime-revolution.com/integrating-a-third-party-api-with-rails-5-134f960ddbba它对我有意义。但是,在尝试根据教程代码查看订阅者时,我收到以下错误。
收到错误:
无法自动加载常量Base,预计 /Users/xxxxxxxxx/Documents/projects/phrayme/xxxxxxxxxxx/app/services/base.rb 定义它
从我的研究中,我认为它可能与目录结构以及Rails期望在那里有什么关系,但我还没能找到问题所在。请指教。
subscribers_controller.rb
class SubscribersController < ApplicationController
def index
@subscribers, @errors = Transexp::Subscriber.random(query)
end
def show
@subscriber = Transexp::Subscriber.find(params[:id])
end
private
def query
params.fetch(:query, {})
end
end
服务/ base.rb
module Transexp
class Base
attr_accessor :errors
def initialize(args = {})
args.each do |name, value|
attr_name = name.to_s.underscore
send("#{attr_name}=", value) if respond_to?("#{attr_name}=")
end
end
end
end
服务/ subscriber.rb
module Transexp
class Subscriber < Base
attr_accessor :firstName,
:lastName,
:fullName,
:coName,
:phone_type,
:phone_nr,
:addrLn1,
:addrLn2,
:city,
:state,
:zipCode,
:ctry,
:email,
:type,
:stat,
:note
MAX_LIMIT = 25
def self.find(id)
response = Request.get("subscribers/#{id}/information")
Subscriber.new(response)
end
def self.random(query = {})
response = Request.where('subscribers/random', query.merge({ number: MAX_LIMIT }))
subscribers = response.fetch('subscribers', []).map { |subscriber| Subscriber.new(subscriber) }
[ subscribers, response[:errors] ]
end
def initialize(args = {})
super(args)
end
end
end
查看/订户/ index.html.erb
<h1>Subscribers#index</h1>
<p>Find me in app/views/subscribers/index.html.erb</p>
<%= @subscribers %>
LIB / transexp / request.rb
class Request
class << self
def where(resource_path, query = {}, options = {})
response, status = get_json(resource_path, query)
status == 200 ? response : errors(response)
end
def get(id)
response, status = get_json(id)
status == 200 ? response : errors(response)
end
def errors(response)
error = { errors: { status: response["status"], message: response["message"] } }
response.merge(error)
end
def get_json(root_path, query = {})
query_string = query.map{|k,v| "#{k}=#{v}"}.join("&")
path = query.empty?? root_path : "#{root_path}?#{query_string}"
response = api.get(path)
[JSON.parse(response.body), response.status]
end
def api
Connection.api
end
end
end
LIB / transexp / connection.rb
require 'faraday'
require 'json'
module Transexp
class Connection
BASE = 'https://xxxxxxxxxxx'
def self.api
Faraday.new(url: BASE) do |faraday|
faraday.response :logger
faraday.adapter Faraday.default_adapter
faraday.headers['Content-Type'] = 'application/json'
faraday.headers['Gateway_ID'] = ENV['XXXXX']
faraday.headers['HostedKey'] = ENV['XXXXX']
faraday.headers['RURL'] = 'https://XXXXX.com/'
faraday.headers['CURL'] = 'https://XXXXX.com/'
end
end
end
端
感谢您提供的任何建议或提示。
DS