hopo-ruby 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 181e5c6cf047a790539ca4cc364618d5d30bc6e2
4
+ data.tar.gz: 03d7ef77b7a55c5011d34644d5370d5a9cff3955
5
+ SHA512:
6
+ metadata.gz: cce29ce73cdfb6a520c58bd1cd95040c0bfd7727d5ce12dcf87bb141c41f5d389e729599d3ed7465bd827712910f5ce8fbcc1f72c2e3a9ecca2edf6f61a8fad5
7
+ data.tar.gz: 4ac570b7263262b466638b0b1de80270a42a7d030499c5b8c90933a88e9d6ebb38d1eb006477885fa95bb4c632829efe9b4de0fa2ba361687cdd359c0e0aa8b9
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in hopo-ruby.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Mike Reinmiller
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,37 @@
1
+
2
+ # Hopo::Ruby
3
+
4
+ A library for using the Honest Policy API, currently supports Auto and Home Raters.
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ ```ruby
11
+ gem 'hopo-ruby'
12
+ ```
13
+
14
+ And then execute:
15
+
16
+ $ bundle
17
+
18
+ Or install it yourself as:
19
+
20
+ $ gem install hopo-ruby
21
+
22
+
23
+ ## Setup
24
+
25
+ You need to set the **api_key** given by honestpolicy before you can use the raters. This can be set as simply as:
26
+
27
+ ```ruby
28
+ Hopo.api_key = 'your-unique-api-key'
29
+ ```
30
+
31
+ If you are using rails, you can run the generator to setup a config/initializer file for you. This file will require the hopo library and add a line to set your api_key. Make sure you do not commit your actual key to your repository.
32
+
33
+ $ rails g hopo:install
34
+
35
+
36
+ ## Usage
37
+
@@ -0,0 +1,11 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ # Will load PRY with GEM required. Could also run `pry --gem`
4
+ task :console do
5
+ require 'pry'
6
+ require 'hopo'
7
+ ARGV.clear
8
+ Pry.start
9
+ end
10
+
11
+ task :c => :console
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'hopo/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "hopo-ruby"
8
+ spec.version = Hopo::VERSION
9
+ spec.authors = ["Mike Reinmiller"]
10
+ spec.email = ["mike@honestpolicy.com"]
11
+ spec.summary = %q{A simple library for communicating with the Honest Policy API}
12
+ spec.description = %q{Easily connect to multiple Honest Policy APIs including the Rater, Leads, Vehicles and Carrier Quality}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.7"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+
24
+ spec.add_development_dependency 'pry', "~> 0.10.1"
25
+
26
+ end
@@ -0,0 +1,46 @@
1
+ # Borrowed from Rails source
2
+
3
+ class Object
4
+ # An object is blank if it's false, empty, or a whitespace string.
5
+ # For example, '', ' ', +nil+, [], and {} are all blank.
6
+ #
7
+ # This simplifies
8
+ #
9
+ # address.nil? || address.empty?
10
+ #
11
+ # to
12
+ #
13
+ # address.blank?
14
+ #
15
+ # @return [true, false]
16
+ def blank?
17
+ respond_to?(:empty?) ? !!empty? : !self
18
+ end
19
+
20
+ # An object is present if it's not blank.
21
+ #
22
+ # @return [true, false]
23
+ def present?
24
+ !blank?
25
+ end
26
+
27
+ # Returns the receiver if it's present otherwise returns +nil+.
28
+ # <tt>object.presence</tt> is equivalent to
29
+ #
30
+ # object.present? ? object : nil
31
+ #
32
+ # For example, something like
33
+ #
34
+ # state = params[:state] if params[:state].present?
35
+ # country = params[:country] if params[:country].present?
36
+ # region = state || country || 'US'
37
+ #
38
+ # becomes
39
+ #
40
+ # region = params[:state].presence || params[:country].presence || 'US'
41
+ #
42
+ # @return [Object]
43
+ def presence
44
+ self if present?
45
+ end
46
+ end
@@ -0,0 +1,13 @@
1
+ module Hopo
2
+ module Generators
3
+ class InstallGenerator < Rails::Generators::Base
4
+ desc "Creates Hopo Initializer File"
5
+ source_root File.expand_path('../templates', __FILE__)
6
+
7
+ def copy_initializer
8
+ template 'hopo_config.rb', 'config/initializers/hopo.rb'
9
+ end
10
+
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,6 @@
1
+ # Do not put the API_KEY directly in this file. It is recommended to set
2
+ # the appropriate environment variable.
3
+
4
+ require 'hopo'
5
+
6
+ Hopo.api_key = ENV['HOPO_API_KEY']
@@ -0,0 +1,46 @@
1
+ # Hopo Ruby libs
2
+ require 'net/http'
3
+ require 'uri'
4
+ require 'openssl'
5
+ require 'json'
6
+ require 'ostruct'
7
+
8
+ # Version
9
+ require "hopo/version"
10
+
11
+ # Extend Ruby Classes
12
+ require 'extend/object' unless defined?(::Rails)
13
+
14
+ # General
15
+ require 'hopo/errors'
16
+
17
+ # API Connections
18
+ require 'hopo/connections/request'
19
+ require 'hopo/connections/response'
20
+ require 'hopo/api'
21
+
22
+ # Rater - Auto, Home, Health, Life
23
+ require 'hopo/rater'
24
+ require 'hopo/rater/auto_rater'
25
+ require 'hopo/rater/home_rater'
26
+
27
+ # Base Hopo Module
28
+ module Hopo
29
+ class << self
30
+ attr_accessor :api_key
31
+
32
+ def new_request(line)
33
+ Rater.new(line)
34
+ end
35
+
36
+ def home_request
37
+ HomeRater.new
38
+ end
39
+
40
+ def auto_request
41
+ AutoRater.new
42
+ end
43
+
44
+ end
45
+ end
46
+
@@ -0,0 +1,66 @@
1
+ module Hopo
2
+ class API
3
+ include Hopo::Connections::Request
4
+ include Hopo::Connections::Response
5
+
6
+ attr_reader :url, :path, :ssl, :default_params
7
+ attr_accessor :uri, :http
8
+
9
+ def initialize(url, path=nil, ssl=false)
10
+ @default_params = {}
11
+ @params = {}
12
+ @url = url
13
+ @path = path
14
+ @ssl = ssl
15
+ setup_connections
16
+ end
17
+
18
+ def params=(parameters)
19
+ raise ArgumentError, 'parameters must be a hash {}' unless parameters.is_a?(Hash)
20
+ @params = parameters
21
+ end
22
+
23
+ def params
24
+ @params
25
+ end
26
+
27
+ def full_path
28
+ "#{url}#{path}"
29
+ end
30
+
31
+ private
32
+
33
+ def setup_connections
34
+ @uri = URI.parse(full_path)
35
+ @http = Net::HTTP.new(uri.host, uri.port)
36
+
37
+ if @ssl
38
+ @http.use_ssl = true
39
+ @http.verify_mode = OpenSSL::SSL::VERIFY_NONE
40
+ end
41
+
42
+ false
43
+ end
44
+
45
+ def perform_request(method, parameters, type)
46
+ request = method.new( encode_params( parameters ) )
47
+ response = http.request(request)
48
+ self.send("response_#{type}", response)
49
+ end
50
+
51
+ def encode_params(parameters)
52
+ uri.query = URI.encode_www_form( merge_parameters(parameters) )
53
+ uri
54
+ end
55
+
56
+ def merge_parameters(parameters)
57
+ raise ParameterError, 'parameters must be a hash {}' unless parameters.is_a?(Hash)
58
+ hash_merge(self.default_params, self.params, parameters)
59
+ end
60
+
61
+ def hash_merge(*hashes)
62
+ hashes.inject :merge
63
+ end
64
+
65
+ end
66
+ end
@@ -0,0 +1,38 @@
1
+ module Hopo
2
+ module Connections
3
+ module Request
4
+ module ClassMethods
5
+ end
6
+
7
+ def self.included(base)
8
+ base.extend(ClassMethods)
9
+ end
10
+
11
+ # Perform a GET request to a path
12
+ def get(parameters={}, type='json')
13
+ perform_request Net::HTTP::Get, parameters, type
14
+ end
15
+
16
+ # Perform a POST request to a path
17
+ def post(parameters={}, type='json')
18
+ perform_request Net::HTTP::Post, parameters, type
19
+ end
20
+
21
+ # Perform a PATCH request to a path
22
+ def patch(parameters={}, type='json')
23
+ perform_request Net::HTTP::Patch, parameters, type
24
+ end
25
+
26
+ # Perform a PUT request to a path
27
+ def put(parameters={}, type='json')
28
+ perform_request Net::HTTP::Put, parameters, type
29
+ end
30
+
31
+ # Perform a DELETE request to a path
32
+ def delete(parameters={}, type='json')
33
+ perform_request Net::HTTP::Delete, parameters, type
34
+ end
35
+
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,30 @@
1
+ module Hopo
2
+ module Connections
3
+ module Response
4
+ module ClassMethods
5
+ end
6
+
7
+ def self.included(base)
8
+ base.extend(ClassMethods)
9
+ end
10
+
11
+ private
12
+
13
+ # RAW response
14
+ def response_raw(response)
15
+ response.body
16
+ end
17
+
18
+ # Formatted JSON response
19
+ def response_json(response)
20
+ JSON.parse(response.body)
21
+ end
22
+
23
+ # Formatted OpenStruct response
24
+ def response_struct(response)
25
+ OpenStruct.new( request_json(response) )
26
+ end
27
+
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,8 @@
1
+ module Hopo
2
+ class Error < Exception
3
+ end
4
+ class AuthenticationError < Error
5
+ end
6
+ class ParameterError < Error
7
+ end
8
+ end
@@ -0,0 +1,73 @@
1
+ module Hopo
2
+ class Rater < API
3
+ attr_accessor :line, :premium_mode, :sorter
4
+
5
+ BASE_URL = 'https://integral.honestpolicy.com/api'
6
+
7
+ def initialize(line, premium_mode='annually', sorter='premiums-asc', version=1, path='rates')
8
+ unless Hopo.api_key
9
+ raise AuthenticationError, 'No API key provided. Set your API key using "Hopo.api_key = <API-KEY>".'
10
+ end
11
+
12
+ super(BASE_URL, "/v#{version}/#{path}", true)
13
+
14
+ @premium_mode = premium_mode
15
+ @sorter = sorter
16
+
17
+ @default_params = {
18
+ :key => Hopo.api_key,
19
+ :line => line
20
+ }
21
+
22
+ end
23
+
24
+ private
25
+
26
+ def response_json(response)
27
+ format_response( JSON.parse(response.body) )
28
+ end
29
+
30
+ def format_response(response)
31
+ # TEMP Hack Until Integral API is cleaned Up....
32
+ if response['status']
33
+ hash = { :errors => [response['message']] }
34
+
35
+ else
36
+ hash = {}
37
+ rates = response['results']['rates']
38
+ errors = response['results']['errors']
39
+ risk = response['risk']
40
+
41
+ hash.merge!( {:rates => factor_premiums(rates) } ) unless rates.blank?
42
+ hash.merge!( {:errors => errors} ) unless errors.blank?
43
+ hash.merge!( {:risk => risk} ) unless risk.blank?
44
+
45
+ end
46
+
47
+ hash
48
+ end
49
+
50
+ def factor_premiums(rates)
51
+ premiums = {:monthly => 12, :triannually => 3, :quarterly => 4, :biannually => 2, :annually => 1}
52
+ divisor = premiums[premium_mode.to_sym] || 1
53
+
54
+ rates.each do |k, v|
55
+ rates[k] = v / divisor
56
+ end
57
+
58
+ sort_rates(rates) unless sorter.blank?
59
+ end
60
+
61
+ def sort_rates(rates)
62
+ sort = sorter.split('-')
63
+
64
+ sorted_rates =
65
+ if sort[1] == 'desc'
66
+ Hash[rates.sort_by {|k, v| sort[0]=='companies' ? k : v }.reverse]
67
+ else
68
+ Hash[rates.sort_by {|k, v| sort[0]=='companies' ? k : v }]
69
+ end
70
+ end
71
+
72
+ end
73
+ end
@@ -0,0 +1,26 @@
1
+ module Hopo
2
+ class AutoRater < Rater
3
+
4
+ def initialize(premium_mode='annually', sorter='premiums-asc', version=1, path='rates')
5
+ super('auto', premium_mode, sorter, version, path)
6
+ end
7
+
8
+ def required_fields?
9
+ {
10
+ :zip => 'Five diging Zip Code for location',
11
+ :model_year => 'Not Required - defaults to 2012',
12
+ :make => 'Not Required - defaults to Toyota',
13
+ :model => 'Not Required - defaults to Camry'
14
+ }
15
+ end
16
+
17
+ def optional_fields?
18
+ {
19
+ :test1 => 'test 1',
20
+ :test2 => 'test 2',
21
+ :test3 => 'test 3'
22
+ }
23
+ end
24
+
25
+ end
26
+ end
@@ -0,0 +1,26 @@
1
+ module Hopo
2
+ class HomeRater < Rater
3
+
4
+ def initialize(premium_mode='annually', sorter='premiums-asc', version=1, path='rates')
5
+ super('home', premium_mode, sorter, version, path)
6
+ end
7
+
8
+ def required_fields?
9
+ {
10
+ :zip => 'Five diging Zip Code for location',
11
+ :cov_a => 'The Coverage Amount for the Home - Minimum 10000',
12
+ :year_built => 'Year Home was built - 1800 to 2014',
13
+ :construction_type => 'Material used for Home - [Frame, Masonry, Adobe, Log]'
14
+ }
15
+ end
16
+
17
+ def optional_fields?
18
+ {
19
+ :test1 => 'test 1',
20
+ :test2 => 'test 2',
21
+ :test3 => 'test 3'
22
+ }
23
+ end
24
+
25
+ end
26
+ end
@@ -0,0 +1,3 @@
1
+ module Hopo
2
+ VERSION = "0.0.2"
3
+ end
metadata ADDED
@@ -0,0 +1,105 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hopo-ruby
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Mike Reinmiller
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-10-01 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.7'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.7'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: pry
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 0.10.1
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 0.10.1
55
+ description: Easily connect to multiple Honest Policy APIs including the Rater, Leads,
56
+ Vehicles and Carrier Quality
57
+ email:
58
+ - mike@honestpolicy.com
59
+ executables: []
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - ".gitignore"
64
+ - Gemfile
65
+ - LICENSE.txt
66
+ - README.md
67
+ - Rakefile
68
+ - hopo-ruby.gemspec
69
+ - lib/extend/object.rb
70
+ - lib/generators/hopo/install_generator.rb
71
+ - lib/generators/hopo/templates/hopo_config.rb
72
+ - lib/hopo.rb
73
+ - lib/hopo/api.rb
74
+ - lib/hopo/connections/request.rb
75
+ - lib/hopo/connections/response.rb
76
+ - lib/hopo/errors.rb
77
+ - lib/hopo/rater.rb
78
+ - lib/hopo/rater/auto_rater.rb
79
+ - lib/hopo/rater/home_rater.rb
80
+ - lib/hopo/version.rb
81
+ homepage: ''
82
+ licenses:
83
+ - MIT
84
+ metadata: {}
85
+ post_install_message:
86
+ rdoc_options: []
87
+ require_paths:
88
+ - lib
89
+ required_ruby_version: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - ">="
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ required_rubygems_version: !ruby/object:Gem::Requirement
95
+ requirements:
96
+ - - ">="
97
+ - !ruby/object:Gem::Version
98
+ version: '0'
99
+ requirements: []
100
+ rubyforge_project:
101
+ rubygems_version: 2.2.2
102
+ signing_key:
103
+ specification_version: 4
104
+ summary: A simple library for communicating with the Honest Policy API
105
+ test_files: []