Skip to content
This repository has been archived by the owner on Jun 29, 2023. It is now read-only.

Enclose all classes under a Typeform namespace #66

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 14 additions & 12 deletions lib/create_api_gem/api_config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,22 @@
# specific language governing permissions and limitations
# under the License.

class APIConfig
def self.token
ENV['TYPEFORM_API_TOKEN']
end
module Typeform
class APIConfig
def self.token
ENV['TYPEFORM_API_TOKEN']
end

def self.image_api_request_url
'https://images.typeform.com'
end
def self.image_api_request_url
'https://images.typeform.com'
end

def self.workspaces_api_request_url
'https://api.typeform.com/workspaces'
end
def self.workspaces_api_request_url
'https://api.typeform.com/workspaces'
end

def self.api_request_url
'https://api.typeform.com'
def self.api_request_url
'https://api.typeform.com'
end
end
end
90 changes: 46 additions & 44 deletions lib/create_api_gem/api_request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,63 +15,65 @@
# specific language governing permissions and limitations
# under the License.

class APIRequest
def self.execute(*args)
new(*args).tap do |request|
raise StandardError, 'Failed asserting that the request succeeds' unless request.success?
module Typeform
class APIRequest
def self.execute(*args)
new(*args).tap do |request|
raise StandardError, 'Failed asserting that the request succeeds' unless request.success?
end
end
end

def headers
@response.headers
end
def headers
@response.headers
end

def inspect
"#{@response.code}\n#{@response}"
end
def inspect
"#{@response.code}\n#{@response}"
end

def bad_request?
@response.code == 400
end
def bad_request?
@response.code == 400
end

def unauthorized?
@response.code == 401
end
def unauthorized?
@response.code == 401
end

def payment_required?
@response.code == 402
end
def payment_required?
@response.code == 402
end

def forbidden?
@response.code == 403
end
def forbidden?
@response.code == 403
end

def not_found?
@response.code == 404
end
def not_found?
@response.code == 404
end

def service_unavailable?
@response.code == 502 || @response.code == 503
end
def service_unavailable?
@response.code == 502 || @response.code == 503
end

def error_code
json.fetch(:code)
end
def error_code
json.fetch(:code)
end

private
private

def request(args = {})
RestClient::Request.execute(args) { |r| @response = r }
end
def request(args = {})
RestClient::Request.execute(args) { |r| @response = r }
end

def json
JSON.parse(@response, symbolize_names: true)
end
def json
JSON.parse(@response, symbolize_names: true)
end

def json?
json
true
rescue JSON::ParserError
false
def json?
json
true
rescue JSON::ParserError
false
end
end
end
38 changes: 20 additions & 18 deletions lib/create_api_gem/data_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,28 +17,30 @@

require 'ffaker'

module DataGenerator
def self.title
FFaker::Movie.title
end
module Typeform
module DataGenerator
def self.title
FFaker::Movie.title
end

def self.description
FFaker::Lorem.paragraph
end
def self.description
FFaker::Lorem.paragraph
end

def self.email
FFaker::Internet.email
end
def self.email
FFaker::Internet.email
end

def self.color_code
'#' + SecureRandom.hex(3)
end
def self.color_code
'#' + SecureRandom.hex(3)
end

def self.field_ref
SecureRandom.hex(6)
end
def self.field_ref
SecureRandom.hex(6)
end

def self.uuid
SecureRandom.uuid
def self.uuid
SecureRandom.uuid
end
end
end
134 changes: 68 additions & 66 deletions lib/create_api_gem/forms/blocks/block.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,81 +15,83 @@
# specific language governing permissions and limitations
# under the License.

class Block
def self.all_types
{
'date block' => DateBlock,
'dropdown block' => DropdownBlock,
'email block' => EmailBlock,
'file upload block' => FileUploadBlock,
'group block' => GroupBlock,
'legal block' => LegalBlock,
'long text block' => LongTextBlock,
'multiple choice block' => MultipleChoiceBlock,
'number block' => NumberBlock,
'opinion scale block' => OpinionScaleBlock,
'payment block' => PaymentBlock,
'picture choice block' => PictureChoiceBlock,
'rating block' => RatingBlock,
'short text block' => ShortTextBlock,
'statement block' => StatementBlock,
'website block' => WebsiteBlock,
'yes no block' => YesNoBlock,
'phone number block' => PhoneNumberBlock
}
end
module Typeform
class Block
def self.all_types
{
'date block' => DateBlock,
'dropdown block' => DropdownBlock,
'email block' => EmailBlock,
'file upload block' => FileUploadBlock,
'group block' => GroupBlock,
'legal block' => LegalBlock,
'long text block' => LongTextBlock,
'multiple choice block' => MultipleChoiceBlock,
'number block' => NumberBlock,
'opinion scale block' => OpinionScaleBlock,
'payment block' => PaymentBlock,
'picture choice block' => PictureChoiceBlock,
'rating block' => RatingBlock,
'short text block' => ShortTextBlock,
'statement block' => StatementBlock,
'website block' => WebsiteBlock,
'yes no block' => YesNoBlock,
'phone number block' => PhoneNumberBlock
}
end

def self.from_response(response)
response[:type] = response[:type].to_sym
if response[:type] == :group
response[:properties][:fields].map! { |field| Block.from_response(field) } unless response[:properties][:fields].nil?
def self.from_response(response)
response[:type] = response[:type].to_sym
if response[:type] == :group
response[:properties][:fields].map! { |field| Block.from_response(field) } unless response[:properties][:fields].nil?
end
properties = response[:properties] || {}
validations = response[:validations] || {}
block_params = response.keep_if { |k, _| k != :properties && k != :validations } || {}
params = properties.merge(validations).merge(block_params)
all_types.fetch(block_symbol_to_string(response[:type])).new(params)
end
properties = response[:properties] || {}
validations = response[:validations] || {}
block_params = response.keep_if { |k, _| k != :properties && k != :validations } || {}
params = properties.merge(validations).merge(block_params)
all_types.fetch(block_symbol_to_string(response[:type])).new(params)
end

def same?(actual)
(id.nil? || id == actual.id) &&
type == actual.type &&
title == actual.title &&
(ref.nil? || ref == actual.ref) &&
(description.nil? || description == actual.description) &&
(respond_to?(:attachment) ? same_attachment?(actual.attachment) : true) &&
same_extra_attributes?(actual)
end
def same?(actual)
(id.nil? || id == actual.id) &&
type == actual.type &&
title == actual.title &&
(ref.nil? || ref == actual.ref) &&
(description.nil? || description == actual.description) &&
(respond_to?(:attachment) ? same_attachment?(actual.attachment) : true) &&
same_extra_attributes?(actual)
end

def same_attachment?(actual_attachment)
return true if attachment.nil?
def same_attachment?(actual_attachment)
return true if attachment.nil?

type = attachment[:type]
case type
when 'image'
return (attachment[:href].start_with?("#{APIConfig.image_api_request_url}/images/") && actual_attachment[:href].start_with?("#{APIConfig.image_api_request_url}/images/"))
when 'video'
return attachment == actual_attachment
else
return false
type = attachment[:type]
case type
when 'image'
return (attachment[:href].start_with?("#{APIConfig.image_api_request_url}/images/") && actual_attachment[:href].start_with?("#{APIConfig.image_api_request_url}/images/"))
when 'video'
return attachment == actual_attachment
else
return false
end
end
end

def self.attachment
[Block.image_attachment_payload, Block.video_attachment_payload].sample
end
def self.attachment
[Block.image_attachment_payload, Block.video_attachment_payload].sample
end

def self.image_attachment_payload(image_id: 'default')
{ type: 'image', href: "#{APIConfig.image_api_request_url}/images/#{image_id}" }
end
def self.image_attachment_payload(image_id: 'default')
{ type: 'image', href: "#{APIConfig.image_api_request_url}/images/#{image_id}" }
end

def self.video_attachment_payload(video_url: 'https://www.youtube.com/watch?v=Uui3oT-XBxs', scale: 0.6)
{ type: 'video', href: video_url, scale: scale }
end
def self.video_attachment_payload(video_url: 'https://www.youtube.com/watch?v=Uui3oT-XBxs', scale: 0.6)
{ type: 'video', href: video_url, scale: scale }
end

def self.block_symbol_to_string(symbol)
string = symbol.to_s
string.sub!('_', ' ')
string + ' block'
def self.block_symbol_to_string(symbol)
string = symbol.to_s
string.sub!('_', ' ')
string + ' block'
end
end
end
Loading