How to refactor ruby rest-client get method - ruby-on-rails

I'm using Rubys rest-client gem to make a call to Google API and want to shorten the url part.
Current code:
class GoogleTimezoneGetter
def initialize(lat:, lon:)
#lat = lat
#lon = lon
#time_stamp = Time.new.to_i
end
def response
response = RestClient.get "https://maps.googleapis.com/maps/api/timezone/json?location=#{#lat},#{#lon}&timestamp=#{#time_stamp}&key=#{GOOGLE_TIME_ZONE_KEY}"
JSON.parse(response)
end
def time_zone
response["timeZoneId"]
end
end
I would like to be able to do something like:
def response
response = RestClient.get (uri, params)
JSON.parse(response)
end
But I'm struggling to find out how to do so.
To make the class a bit tidier, I'd like to break the url down into 'uri' and 'params'. I think the rest-client gem allows you to do this but I can't find specific examples.
I want to put the
{#lat},#{#lon}&timestamp=#{#time_stamp}&key=#{GOOGLE_TIME_ZONE_KEY}"
in to a 'params' method and pass that to the RestClient.get method.

Have you check the rest-client gem readme?
They did give a specific example on this (below example quoted from the readme)
RestClient.get 'http://example.com/resource', {params: {id: 50, 'foo' => 'bar'}}
In your case, it should be something like this
def url
"https://maps.googleapis.com/maps/api/timezone/json"
end
def params
{
locations: "#{#lat},#{#lon}",
timestamp: #time_stamp,
key: GOOGLE_TIME_ZONE_KEY
}
end
def response
response = RestClient.get(url, params: params)
JSON.parse(response)
end

rest-client already accepts a hash for params. If you prefer a bunch of little methods on your class, you can divide out each step to a method and keep everything readable.
class GoogleTimezoneGetter
def initialize(lat:, lon:)
#lat = lat
#lon = lon
#time_stamp = Time.new.to_i
end
def response
response = RestClient.get gtz_url, params: { gtz_params }
JSON.parse(response)
end
def time_zone
response["timeZoneId"]
end
def gtz_url
"https://maps.googleapis.com/maps/api/timezone/json"
end
def gtz_params
return {location: "#{#lat},#{#lon}", timestamp: #time_stamp, key: GOOGLE_TIME_ZONE_KEY }
end
end

Related

NoMethodError: undefined method `submission' for HTTParty

I'm currently working on a Rails application where I am trying to submit a form to the FormStack API. The request look as follows.
This is what the requests looks like:
POST /api/v2/form/12345/submission.json HTTP/1.1
Host: www.formstack.com
Authorization: Bearer YOUR_APP_OAUTH_TOKEN
Accept: application/json
Content-Type: application/json
field_12345=Example&field_12346=Answer
I'm trying to implement that using Httparty on the library I created to make the requests to this API service.
module FormStack
class Form
include HTTParty
attr_reader :form_id
base_uri "https://www.formstack.com/api/v2"
def initialize
#access_token = ENV.fetch('FORMSTACK_ACCESS_TOKEN')
#form_id = ENV.fetch('FORMSTACK_FORM_ID')
end
def create_form
self.class.get(relative_uri, headers: headers)
end
def submission
self.class.post(create_submission_uri, headers: headers, query: query)
end
private
def relative_uri
"/form/#{#form_id}/field.json"
end
def create_submission_uri
"form/#{#form_id}/submission.json"
end
def headers
{
"Accept" => "application/json",
"Content-Type" => "application/json",
"Authorization" => "Bearer #{#access_token}"
}
end
def query
{
"field_66563890" => "blah",
"field_66563757" => "something"
}
end
end
end
controller
class FormsController < ApplicationController
def display_form
#form = FormStack::Form.new().create_form
end
def create
#form.submission
redirect_to 'localhost:3000'
end
end
This are the routes
get '/forms/display_form', to: 'forms#display_form'
post '/forms/submit', to: "forms#create"
First of all, I've got a couple general ruby things for you:
When you call FormStack::Form.new().create_form you actually don't need the () after .new -- ruby knows to call the method with no arguments even if you exclude the parens.
I'm not quite sure how you're calling FormsController::display_form from FormsController::create, but for now I'll just assume that you're using magic.
Anyways, on to my answer. As your error message states, the error is related to you calling submission on something which does not have a submission method. With that knowledge, we can look at what Object you're calling submission on in this line:
#form.submission
It looks like you're calling submission on #form. Well, let's go and look at where you declare #form:
#form = FormStack::Form.new().create_form
Let's break that declaration down into its parts. First, with FormStack::Form.new(), you're creating a new instance of FormStack::Form. So far so good. FormStack::Form has a submission method defined on it. But then, you call create_form on it. So, let's look at what create_form does:
def create_form
self.class.get(relative_uri, headers: headers)
end
create_form calls a method provided by HTTParty, get. The get method returns a HTTParty::Response Object. So, let's parse through the line where you set #form again. Broken down, what you're doing is this:
#form = FormStack::Form # This line sets the variable to a constant
#form = #form.new # This line sets the variable to be an instance of FormStack::Form
#form = #form.create_form # This line sets #form to be an instance of HTTParty::Reponse
As you can see, at the end we've set #form to an instance of HTTParty::Reponse instead of FormStack::Form, and since there's not submission method for HTTParty::Response that's why you get the error.
Based on this exploration, we can see that the fix would be to set #form to a FormStack::Form object instead, which we can do by changing the display_form action to be:
def display_form
#form = FormStack::Form.new
#form.create_form
end

WebMock model's function when called via controller

I have existing functionality, where specs calls HTTP PUT method of a controller, which in-turn calls model's method to get some JSON data via. API.
class SomeModel
def GetData()
uri = URI.parse('http://api.mydomain.com/getmydata/123/ohmyurl')
Net::HTTP.start(uri.host, uri.port, :read_timeout => 200) do |http|
request = Net::HTTP::Get.new(uri.request_uri)
response = http.request(request)
unless response.body == "null"
return JSON.parse(response.body)
end
#......
end
end
end
class SomeController < ApplicationController
def update
#...
#model.GetData()
#...
end
end
#......specs............
put :update
I need to mock the API in SomeModel. So far I have tried:
#......specs............
before do
stub_request(:get, /api.mydomain.com/)
.with(headers: {'Accept'=>'application/json' })
.to_return(status: 200, body: {id: 1, secondParam: '324'}.to_json, headers: {})
end
#.......
put :update
result = JSON.load response.body
expect(result['secondParam']).to eq("324")
Which is not able to mock the API call and actual API is called.
kindly ignore syntax errors

How should I test this code with rspec rails?

In my rails application I have a module with an API call that I would like to write rspec tests for. What would you write as proper tests for this? I have read not to combine httparty with rspec because it is very slow and now I have no idea how to do this. Thanks for the advice!
module BandsInTown
class API
attr_reader :artist_name, :info, :events
def initialize(artist_name)
#artist_name = artist_name
end
def artist_info
#info = fetch_data
end
def artist_events
#events = fetch_data 'events'
end
private
def fetch_data(endpoint = nil)
request_url = "http://api.bandsintown.com/artists/#{[#artist_name, endpoint].compact.join('/')}.json?api_version=2.0&app_id=AUTH_KEY"
resp = HTTParty.get(request_url)
return false if resp.code != 200
resp
end
end
end
I would use webmock to get around making API requests.
Here's an example on how the code in yout spec_helper.rb could look like:
require 'webmock/rspec'
def stub_api_requests
response = File.read('spec/fixtures/payload.json')
stub_request(:get, %r{http:\/\/api.bandsintown.com\/artists/\/(.)*})
.to_return(
status: 200,
body: response
)
end
config.before(:each) do
stub_api_requests
end

If open-uri works, why does net/http return an empty string?

I am attempting to download a page from Wikipedia. For such a task, I am using gems. When using net/http, all I get is an empty string. So I tried with open-uri and it works fine.
Nevertheless, I prefer the first option because it gives me a much more explicit control; but why is it returning an empty string?
class Downloader
attr_accessor :entry, :url, :page
def initialize
# require 'net/http'
require 'open-uri'
end
def getEntry
print "Article name? "
#entry = gets.chomp
end
def getURL(entry)
if entry.include?(" ")
#url = "http://en.wikipedia.org/wiki/" + entry.gsub!(/\s/, "_")
else
#url = "http://en.wikipedia.org/wiki/" + entry
end
#url.downcase!
end
def getPage(url)
=begin THIS FAULTY SOLUTION RETURNS AN EMPTY STRING ???
connector = URI.parse(url)
connection = Net::HTTP.start(connector.host, connector.port) do |http|
http.get(connector.path)
end
puts "Body:"
#page = connection.body
=end
#page = open(url).read
end
end
test = Downloader.new
test.getEntry
test.getURL(test.entry)
test.getPage(test.url)
puts test.page
P.S.: I am an autodidact programmer so the code might not fit good practices. My apologies.
Because your request return 301 Redirect (check connection.code value), you should follow redirect manually if you are using net/http. Here is more details.

Checking user email calling Closio API in Rails - HTTParty and no method errors

I'm working with a team on checking a user's email input when they sign up for a web app. The user will not be allowed to sign up if their email is not found with the following API call using HTTParty. We are getting method_errors for whatever syntax is first within the function. For, example, in the method below, "include" comes up as an undefined method error.
def email_checker
include HTTParty
default_params :output => 'json'
format :json
base_uri 'app.close.io'
basic_auth 'insert_api_code_here', ' '
response = HTTParty.get('/api/v1/contact/')
#email_database = []
response['data'].each do |x|
x['emails'].each do |contact_info|
#email_database << contact_info['email']
end
end
unless #email_database.include? :email
errors.add :email, 'According to our records, your email has not been found!'
end
end
UPDATE: So we went with the inline version of using HTTParty and our registrations controller (working with devise) looks like this:
class RegistrationsController < Devise::RegistrationsController
def email_checker(email)
YAML.load(File.read('config/environments/local_env.yml')).each {|k, v| ENV[k.to_s] = v}
api_options = {
query: => {:output => 'json'},
format: :json,
base_uri: 'app.close.io',
basic_auth: ENV["API_KEY"], ' '
}
response = HTTParty.get('/api/v1/contact/', api_options)
#email_database = []
response['data'].each do |x|
x['emails'].each do |contact_info|
#email_database << contact_info['email']
end
end
unless #email_database.include? email
return false
else
return true
end
end
def create
super
if email_checker == false
direct_to 'users/sign_up'
#and return to signup with errors
else
User.save!
end
end
end
We're getting syntax error: "syntax error, unexpected =>" Did we screw up the format?
There are two different ways to use HTTParty, and you're trying to use both. Pick one :).
The class-based method would look something like this:
class CloseIo
include HTTParty
default_params :output => 'json'
format :json
base_uri 'app.close.io'
basic_auth 'insert_api_code_here', ' '
end
class UserController
def email_checker
response = CloseIo.get('/api/v1/contact/')
# ... the rest of your stuff
end
end
An inline version would look something like this
class UserController
def email_checker
api_options = {
query: :output => 'json',
format: :json,
base_uri: 'app.close.io',
basic_auth: 'insert_api_code_here'
}
response = HTTParty.get('/api/v1/contact/', api_options)
# ... do stuff
end
end

Resources