Skip to content

Commit

Permalink
REST Resources mapped
Browse files Browse the repository at this point in the history
  • Loading branch information
kopz9999 committed Jul 28, 2016
1 parent d24255e commit 7fe146e
Show file tree
Hide file tree
Showing 35 changed files with 366 additions and 12 deletions.
8 changes: 8 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,11 @@
/pkg/
/spec/reports/
/tmp/
# C9
.c9
# RubyMine
.idea
# Env
.env
# VCR
/spec/cassettes
1 change: 1 addition & 0 deletions .ruby-gemset
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
fub_client
1 change: 1 addition & 0 deletions .ruby-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ruby-2.3.1
21 changes: 17 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
# FubClient

Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/fub_client`. To experiment with that code, run `bin/console` for an interactive prompt.

TODO: Delete this and the text above, and describe your gem
This gem is a Ruby Client for [Follow Up Boss API](https://api.followupboss.com/api-documentation/)
For more information about Follow Up Boss go to [www.followupboss.com](www.followupboss.com)

## Installation

Expand All @@ -22,7 +21,21 @@ Or install it yourself as:

## Usage

TODO: Write usage instructions here
After installing the gem, you can start consuming FUB resources as Rails like
models with shorthand methods:

```ruby
# Get one event
event = FubClient::Event.find 12

# Paginate (offset calculated)
person = FubClient::Person.by_page 2, 10
# => Her::Model::Relation<Person>

# Total (from all records)
total = FubClient::EmailTemplate.total
# => 323
```

## Development

Expand Down
15 changes: 12 additions & 3 deletions fub_client.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ Gem::Specification.new do |spec|
spec.authors = ["Kyoto Kopz"]
spec.email = ["[email protected]"]

spec.summary = %q{TODO: Write a short summary, because Rubygems requires one.}
spec.description = %q{TODO: Write a longer description or delete this line.}
spec.homepage = "TODO: Put your gem's website or public repo URL here."
spec.summary = 'Ruby client for Follow Up Boss API http://www.followupboss.com'
# spec.description = %q{TODO: Write a longer description or delete this line.}
spec.homepage = "https://github.com/kopz9999/fub_client"
spec.license = "MIT"

# Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
Expand All @@ -27,7 +27,16 @@ Gem::Specification.new do |spec|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
spec.require_paths = ["lib"]

spec.add_dependency "her", "~> 0.8.1"
spec.add_dependency "faraday", "~> 0.8.11"
spec.add_dependency "facets", "~> 3.0.0"

# Developemnt
spec.add_development_dependency "bundler", "~> 1.12"
spec.add_development_dependency "rake", "~> 10.0"
spec.add_development_dependency "rspec", "~> 3.0"
spec.add_development_dependency "dotenv", "~> 2.1.1"
spec.add_development_dependency "vcr", "~> 3.0.3"
spec.add_development_dependency "webmock", "~> 2.1.0"
spec.add_development_dependency "pry", "~> 0.10.3"
end
26 changes: 25 additions & 1 deletion lib/fub_client.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,29 @@
# Libs
require 'base64'
require 'singleton'
require 'faraday'
require 'her'
require 'facets/string/snakecase'

# App
require "fub_client/version"
require "fub_client/client"
require "fub_client/middleware"
require "fub_client/resource"
# App Models
require "fub_client/event"
require "fub_client/person"
require "fub_client/note"
require "fub_client/call"
require "fub_client/user"
require "fub_client/smart_list"
require "fub_client/email_template"
require "fub_client/action_plan"
require "fub_client/em_event"
require "fub_client/custom_field"

module FubClient
# Your code goes here...
def self.root
File.expand_path '../..', __FILE__
end
end
5 changes: 5 additions & 0 deletions lib/fub_client/action_plan.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module FubClient
class ActionPlan < Resource
collection_path 'actionPlans'
end
end
4 changes: 4 additions & 0 deletions lib/fub_client/call.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module FubClient
class Call < Resource
end
end
40 changes: 40 additions & 0 deletions lib/fub_client/client.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
module FubClient
class Client
API_URL = 'api.followupboss.com'
API_VERSION = 'v1'

include Singleton

attr_writer :api_key
attr_reader :her_api

def initialize
init_her_api
end

def api_key
@api_key ||= ENV['FUB_API_KEY']
end

def api_uri
@api_uri ||= URI::HTTPS.build(host: API_URL, path: "/#{API_VERSION}")
end

private

def init_her_api
@her_api = Her::API.new
@her_api.setup url: self.api_uri.to_s do |c|
# Request
c.use FubClient::Middleware::Authentication
c.use Faraday::Request::UrlEncoded

# Response
c.use FubClient::Middleware::Parser

# Adapter
c.use Faraday::Adapter::NetHttp
end
end
end
end
5 changes: 5 additions & 0 deletions lib/fub_client/custom_field.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module FubClient
class CustomField < Resource
collection_path 'customFields'
end
end
5 changes: 5 additions & 0 deletions lib/fub_client/em_event.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module FubClient
class EmEvent < Resource
collection_path 'emEvents'
end
end
5 changes: 5 additions & 0 deletions lib/fub_client/email_template.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module FubClient
class EmailTemplate < Resource
collection_path 'templates'
end
end
8 changes: 8 additions & 0 deletions lib/fub_client/event.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module FubClient
class Event < Resource
attributes :created, :updated, :person_id, :message, :description,
:note_id, :source, :type, :property

belongs_to :person
end
end
7 changes: 7 additions & 0 deletions lib/fub_client/middleware.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require "fub_client/middleware/authentication"
require "fub_client/middleware/parser"

module FubClient
module Middleware
end
end
11 changes: 11 additions & 0 deletions lib/fub_client/middleware/authentication.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module FubClient
module Middleware
class Authentication < Faraday::Middleware
def call(env)
env[:request_headers]["Authorization"] = "Basic " +
Base64.strict_encode64("#{FubClient::Client.instance.api_key}:")
@app.call(env)
end
end
end
end
22 changes: 22 additions & 0 deletions lib/fub_client/middleware/parser.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
module FubClient
module Middleware
class Parser < Faraday::Response::Middleware

def on_complete(env)
original_json = MultiJson.load(env[:body])
json = original_json.deep_transform_keys{ |k| k.to_s.snakecase.to_sym }
metadata = json[:_metadata]
if metadata.nil?
result = json
else
result = json[metadata[:collection].snakecase.to_sym]
end
env[:body] = {
data: result,
errors: json[:errors],
metadata: metadata
}
end
end
end
end
4 changes: 4 additions & 0 deletions lib/fub_client/note.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module FubClient
class Note < Resource
end
end
5 changes: 5 additions & 0 deletions lib/fub_client/person.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module FubClient
class Person < Resource
collection_path 'people'
end
end
14 changes: 14 additions & 0 deletions lib/fub_client/resource.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module FubClient
class Resource
include Her::Model
use_api FubClient::Client.instance.her_api

scope :by_page, -> (page, per_page) {
where(offset: (page - 1)*per_page, limit: per_page)
}

def self.total
by_page(1, 1).metadata[:total]
end
end
end
5 changes: 5 additions & 0 deletions lib/fub_client/smart_list.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module FubClient
class SmartList < Resource
collection_path 'smartLists'
end
end
4 changes: 4 additions & 0 deletions lib/fub_client/user.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module FubClient
class User < Resource
end
end
11 changes: 11 additions & 0 deletions spec/fub_client/action_plan_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
require 'spec_helper'

describe FubClient::ActionPlan, :vcr do
describe '.all' do
it 'pulls down resource' do
objects = described_class.all
expect(objects.length).to be > 0
expect(objects.first).to be_kind_of(described_class)
end
end
end
11 changes: 11 additions & 0 deletions spec/fub_client/call_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
require 'spec_helper'

describe FubClient::Call, :vcr do
describe '.all' do
it 'pulls down resource' do
objects = described_class.all
expect(objects.length).to be > 0
expect(objects.first).to be_kind_of(described_class)
end
end
end
11 changes: 11 additions & 0 deletions spec/fub_client/custom_field_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
require 'spec_helper'

describe FubClient::CustomField, :vcr do
describe '.all' do
it 'pulls down resource' do
objects = described_class.all
expect(objects.length).to be > 0
expect(objects.first).to be_kind_of(described_class)
end
end
end
11 changes: 11 additions & 0 deletions spec/fub_client/em_event_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
require 'spec_helper'

describe FubClient::EmEvent, :vcr do
describe '.all' do
it 'pulls down resource' do
objects = described_class.all
expect(objects.length).to be > 0
expect(objects.first).to be_kind_of(described_class)
end
end
end
11 changes: 11 additions & 0 deletions spec/fub_client/email_template_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
require 'spec_helper'

describe FubClient::EmailTemplate, :vcr do
describe '.all' do
it 'brings the correct event' do
objects = described_class.all
expect(objects.length).to be > 0
expect(objects.first).to be_kind_of(described_class)
end
end
end
47 changes: 47 additions & 0 deletions spec/fub_client/event_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
require 'spec_helper'

describe FubClient::Event, :vcr do
describe '.find' do
it 'brings the correct event' do
evt = described_class.find 11195
expect(evt).not_to be_nil
expect(evt).to have_attributes(person_id: 24477, message: '',
description: "Best Time to Call: Today; Email: "+
"[email protected]; Phone Number: 407-705-3915", note_id: nil,
source: 'Curaytor Appointment', type: 'Registration')
end
end

describe '.all' do
it 'brings the correct event' do
events = described_class.all
expect(events.length).to be > 0
expect(events.first).to be_kind_of(described_class)
end
end

describe '.by_page' do
it 'brings the correct person' do
events = described_class.by_page(3, 25)
expect(events.metadata[:offset]).to eq 50
expect(events.metadata[:limit]).to eq 25
end
end

describe '.total' do
it 'gets the number of records' do
expect(described_class.total).to eq 11105
end
end

describe '#person' do
it 'brings the correct person' do
evt = described_class.find 11195
expect(evt).not_to be_nil
expect(evt.person).not_to be_nil
expect(evt.person).to be_kind_of(FubClient::Person)
expect(evt.person).to have_attributes(name: 'Dan Lopez',
source_url: "http://curaytor.com/schedule")
end
end
end
11 changes: 11 additions & 0 deletions spec/fub_client/note_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
require 'spec_helper'

describe FubClient::Note, :vcr do
describe '.all' do
it 'pulls down resource' do
objects = described_class.all
expect(objects.length).to be > 0
expect(objects.first).to be_kind_of(described_class)
end
end
end
Loading

0 comments on commit 7fe146e

Please sign in to comment.