driftrock-service 0.1.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/.gitignore ADDED
@@ -0,0 +1,20 @@
1
+ .idea
2
+ *.gem
3
+ *.rbc
4
+ .bundle
5
+ .config
6
+ .yardoc
7
+ Gemfile.lock
8
+ InstalledFiles
9
+ _yardoc
10
+ coverage
11
+ doc/
12
+ lib/bundler/man
13
+ pkg
14
+ rdoc
15
+ spec/reports
16
+ test/tmp
17
+ test/version_tmp
18
+ tmp
19
+ *.swo
20
+ *.swp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in driftrock-service.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Max
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.
data/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # Driftrock::Service
2
+
3
+ Gem to make it easier to talk to the driftrock api from your app.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'driftrock-service'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install driftrock-service
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,15 @@
1
+ require "bundler/gem_tasks"
2
+ require 'driftrock-service'
3
+ require 'rspec/core/rake_task'
4
+ require 'rspec'
5
+
6
+ RSpec::Core::RakeTask.new('spec')
7
+
8
+ #require 'rake/testtask'
9
+ #
10
+ #Rake::TestTask.new do |t|
11
+ # t.libs << 'test'
12
+ #end
13
+ #
14
+ #desc "Run tests"
15
+ #task :default => :test
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'driftrock-service/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "driftrock-service"
8
+ gem.version = Driftrock::Service::VERSION
9
+ gem.authors = ["Max"]
10
+ gem.email = ["max.dupenois@forward.co.uk"]
11
+ gem.description = %q{Gem for talking to the driftrock apu}
12
+ gem.summary = %q{Maintains the api endpoints and the calls to them with authorisation, etc.}
13
+ gem.homepage = "https://github.com/forward/driftrock-service"
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+ gem.add_runtime_dependency 'httparty'
20
+ gem.add_development_dependency 'rspec'
21
+ end
@@ -0,0 +1,87 @@
1
+ module Driftrock
2
+ module Service
3
+ module Api
4
+ module Administrative
5
+ include Driftrock::Service::Api
6
+
7
+ module URLs
8
+ BASE = '/dashboard_api'
9
+ USERS = BASE+'/user'
10
+ CHANNELS = BASE+'/channel'
11
+ APPS = BASE+'/driftrock_app'
12
+ end
13
+
14
+ def list_apps
15
+ get(URLs::APPS+"/")
16
+ end
17
+
18
+ def get_app(app_id)
19
+ get(URLs::APPS+"/#{app_id}")
20
+ end
21
+
22
+ def add_twitter_details(username, password)
23
+ post(URLs::CHANNELS+"/temp/connect_twitter", {username: username, password: password})
24
+ end
25
+
26
+ def auth_token(app_id, timestamp, company_id)
27
+ response = post(URLs::APPS+"/#{app_id}/auth_token",
28
+ {company_id: company_id, timestamp: timestamp})
29
+ response['token']
30
+ end
31
+
32
+ def activate_app(app_id)
33
+ post(URLs::APPS+"/#{app_id}/activate")
34
+ end
35
+
36
+ def channel_types
37
+ get(URLs::CHANNELS+"/types")
38
+ end
39
+
40
+ def add_channel(channel_name, channel_data)
41
+ post(URLs::CHANNELS+"/#{channel_name}", channel_data)
42
+ end
43
+
44
+ def get_channel(channel_name)
45
+ get(URLs::CHANNELS+"/#{channel_name}")
46
+ end
47
+
48
+ def add_profile_for_channel(channel_name, profile_id)
49
+ post(URLs::CHANNELS+"/#{channel_name}/profile", {profile_id: profile_id})
50
+ end
51
+
52
+ def new_token_for_channel(channel_name, token)
53
+ put(URLs::CHANNELS+"/#{channel_name}/profile", token)
54
+ end
55
+
56
+ def driftrock_login(validation_data)
57
+ post(URLs::USERS+"/login", validation_data)
58
+ end
59
+
60
+ def new_account(user_data)
61
+ post(URLs::USERS+"/new", user_data)
62
+ end
63
+
64
+ def get_user(user_session_id)
65
+ get(URLs::USERS+"/#{user_session_id}")
66
+ end
67
+
68
+ def get_company(company_id)
69
+ get(URLs::USERS+"/company/#{company_id}")
70
+ end
71
+
72
+ def get_companies
73
+ get(URLs::USERS+"/companies")
74
+ end
75
+
76
+ def user_admin?(user_session_id)
77
+ response = get(URLs::USERS+"/#{user_session_id}/is_admin")
78
+ response['is_admin'] =~ /true/
79
+ end
80
+
81
+ def new_company(company_data)
82
+ post(URLs::COMPANIES+"/new", company_data)
83
+ end
84
+ end
85
+ end
86
+ end
87
+ end
@@ -0,0 +1,16 @@
1
+ module Driftrock
2
+ module Service
3
+ module Api
4
+ module Data
5
+ include Driftrock::Service::Api
6
+ ENDPOINT = "/api"
7
+
8
+ def driftrock_data(channel, opts ={})
9
+ get(ENDPOINT+'/'+channel.to_s, opts)
10
+ end
11
+
12
+ end
13
+ end
14
+ end
15
+ end
16
+
@@ -0,0 +1,66 @@
1
+ module Driftrock
2
+ module Service
3
+ module Api
4
+ module Puller
5
+ include Driftrock::Service::Api
6
+ module URLs
7
+ BASE = "/puller_api"
8
+ TOKENS= BASE+"/client_tokens"
9
+ ADVERT_STATES = BASE+"/advert_states"
10
+ CONVERSION_STATES = BASE+"/conversion_states"
11
+ end
12
+
13
+ def tokens(channel)
14
+ get(URLs::TOKENS+"/#{channel}")
15
+ end
16
+
17
+ def temp_auth_tokens(channel)
18
+ get(URLs::TOKENS+"/#{channel}/temp")
19
+ end
20
+
21
+ def refresh_token(channel, company_id, profile_id,
22
+ access_token, refresh_token,
23
+ expires_in, expires_at
24
+ )
25
+ post(URLs::TOKENS+"/refreshed/#{channel}",
26
+ {
27
+ company_id: company_id,
28
+ profile_id: profile_id,
29
+ access_token: access_token,
30
+ refresh_token: refresh_token,
31
+ expires_in: expires_in,
32
+ expires_at: expires_at.strftime(datetime_format)
33
+ })
34
+ end
35
+
36
+ def advert_states(channel, date, company_id, profile_id, states)
37
+ post(URLs::ADVERT_STATES+"/#{channel}",
38
+ {
39
+ date: date.strftime(datetime_format),
40
+ company_id: company_id,
41
+ profile_id: profile_id,
42
+ advert_states: states
43
+ }
44
+ )
45
+ end
46
+
47
+ def conversion_states(channel, date, company_id, profile_id, states)
48
+ post(URLs::CONVERSION_STATES+"/#{channel}",
49
+ {
50
+ date: date.strftime(datetime_format),
51
+ company_id: company_id,
52
+ profile_id: profile_id,
53
+ conversion_states: states
54
+ }
55
+ )
56
+
57
+ end
58
+
59
+ def datetime_format
60
+ "%Y-%m-%d %H:%M:%S %z"
61
+ end
62
+ end
63
+ end
64
+ end
65
+ end
66
+
@@ -0,0 +1,46 @@
1
+ module Driftrock
2
+ module Service
3
+ module Api
4
+ def get(url, opts={})
5
+ api_request do |user_id, company_id|
6
+ Driftrock::Service::Connector.get_from_api(
7
+ url, user_id, company_id, opts
8
+ )
9
+ end
10
+ end
11
+
12
+ def put(url, opts={})
13
+ api_request do |user_id, company_id|
14
+ Driftrock::Service::Connector.put_to_api(
15
+ url, user_id, company_id, opts
16
+ )
17
+ end
18
+ end
19
+
20
+ def post(url, opts={})
21
+ api_request do |user_id, company_id|
22
+ Driftrock::Service::Connector.post_to_api(
23
+ url, user_id, company_id, opts
24
+ )
25
+ end
26
+ end
27
+
28
+ private
29
+ def api_request
30
+ user_id, company_id = nil
31
+ begin
32
+ sess = session.reduce({}){|h, (k, v)| h[k.to_sym] = v; h}
33
+ user_id = sess[:user_id]
34
+ company_id = sess[:company_id]
35
+ rescue => e
36
+ end
37
+ response = yield(user_id, company_id)
38
+ if response['errors']
39
+ raise Errors::DriftrockError.new(response['errors'])
40
+ else
41
+ response['data']
42
+ end
43
+ end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,36 @@
1
+ module Driftrock
2
+ module Service
3
+ class Config < Struct.new(:salt,:base_url,:app_id,:api_token,
4
+ :unauthorised_path)
5
+ private_class_method :new
6
+
7
+ def self.base_url
8
+ @config.base_url
9
+ end
10
+
11
+ def self.salt
12
+ @config.salt
13
+ end
14
+
15
+ def self.api_token
16
+ @config.api_token
17
+ end
18
+
19
+ def self.unauthorised_path
20
+ @config.unauthorised_path
21
+ end
22
+
23
+ def self.app_id
24
+ @config.app_id
25
+ end
26
+
27
+ def self.init
28
+ config = new
29
+ yield(config)
30
+ config.base_url.gsub(/\/$/, "")
31
+ @config = config
32
+ end
33
+
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,49 @@
1
+ require 'httparty'
2
+ module Driftrock
3
+ module Service
4
+ module Connector
5
+
6
+ def build_url_for_driftrock(url)
7
+ url = "/"+url if url !~ /^\/.*/
8
+ Driftrock::Service::Config.base_url+url
9
+ end
10
+
11
+ def post_to_api(url, user_id, company_id, data)
12
+ response = HTTParty.post(build_url_for_driftrock(url), :body => body_for(user_id, company_id, data))
13
+ puts "[MD => CALLING #{build_url_for_driftrock(url)} WITH:"
14
+ puts body_for(user_id, company_id, data)
15
+ puts "[MD] => RESPONSE IS:"
16
+ puts response
17
+ puts "========="
18
+ response
19
+ end
20
+ def get_from_api(url, user_id, company_id, data={})
21
+ response = HTTParty.get(build_url_for_driftrock(url), :body => body_for(user_id, company_id, data))
22
+ puts "[MD => CALLING #{build_url_for_driftrock(url)} WITH:"
23
+ puts body_for(user_id, company_id, data)
24
+ puts "[MD] => RESPONSE IS:"
25
+ puts response
26
+ puts "========="
27
+ response
28
+ end
29
+
30
+ def put_to_api(url, user_id, company_id, data={})
31
+ HTTParty.put(build_url_for_driftrock(url), :body => body_for(user_id, company_id, data))
32
+ end
33
+
34
+
35
+ def body_for(user_id, company_id, data)
36
+
37
+ data = {:company_id => company_id}.merge(data)
38
+ { :data => data,
39
+ :auth => {
40
+ :session_id => user_id,
41
+ :app_id => Driftrock::Service::Config.app_id,
42
+ :api_token => Driftrock::Service::Config.api_token
43
+ }
44
+ }
45
+ end
46
+ module_function :get_from_api, :post_to_api, :body_for, :build_url_for_driftrock
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,17 @@
1
+ module Driftrock
2
+ module Errors
3
+
4
+ class DriftrockError < StandardError
5
+
6
+ attr_reader :errors
7
+
8
+ def initialize(list_of_errors)
9
+ result = super()
10
+ @errors = list_of_errors
11
+ result
12
+ end
13
+
14
+ end
15
+
16
+ end
17
+ end
@@ -0,0 +1,5 @@
1
+ module Driftrock
2
+ module Service
3
+ VERSION = "0.1.8"
4
+ end
5
+ end
@@ -0,0 +1,60 @@
1
+ require "driftrock-service/version"
2
+ require "driftrock-service/config"
3
+ require "driftrock-service/connector"
4
+ require "driftrock-service/api"
5
+ require "driftrock-service/api/administrative"
6
+ require "driftrock-service/api/data"
7
+ require "driftrock-service/api/puller"
8
+ require "driftrock-service/errors/driftrock_error"
9
+
10
+
11
+ module Driftrock
12
+ module Service
13
+ module ClassMethods
14
+ def act_as_driftrock_app
15
+ if respond_to?(:before_filter)
16
+ before_filter :authorise
17
+ end
18
+ include Driftrock::Service::Api::Data
19
+ end
20
+
21
+ def act_as_driftrock_administrator
22
+ include Driftrock::Service::Api::Administrative
23
+ end
24
+
25
+ def act_as_driftrock_puller
26
+ include Driftrock::Service::Api::Puller
27
+ end
28
+ end
29
+
30
+ def self.included(base)
31
+ base.extend ClassMethods
32
+ end
33
+
34
+
35
+ def authorise
36
+ return if session && session[:user_id] && session[:company_id]
37
+ timestamp = params[:timestamp]
38
+ company_id= params[:company_id]
39
+ token = params[:token]
40
+
41
+ expected_token = token_for(timestamp, company_id)
42
+ puts "expected_token: #{expected_token}"
43
+
44
+ if token != expected_token
45
+ redirect_to Driftrock::Service::Config.unauthorised_path
46
+ else
47
+ session[:user_id] = params[:user_id]
48
+ session[:company_id] = params[:company_id]
49
+ end
50
+ end
51
+
52
+ def token_for(timestamp, company_id)
53
+ OpenSSL::Digest::SHA1.hexdigest(
54
+ [timestamp,Driftrock::Service::Config.salt,company_id].join(":")
55
+ )
56
+ end
57
+
58
+
59
+ end
60
+ end
@@ -0,0 +1,9 @@
1
+ require 'spec_helper'
2
+
3
+ describe AdministrativeApiTest do
4
+ it "should be able to make a call to the api" do
5
+ #Assume for the sake of testing there's at least one app
6
+ AdministrativeApiTest.new.apps.count.should be > 0
7
+ end
8
+
9
+ end
@@ -0,0 +1,8 @@
1
+ class AdministrativeApiTest
2
+ include Driftrock::Service::Api::Administrative
3
+
4
+ def test
5
+ apps
6
+ end
7
+
8
+ end
@@ -0,0 +1,10 @@
1
+ require 'driftrock-service'
2
+ require File.join(File.dirname(__FILE__), *%w[models administrative_api_test.rb])
3
+
4
+ Driftrock::Service::Config.init do |config|
5
+ config.base_url = "http://localhost:9393"
6
+ config.salt = "3kllki3weid9n3rljcw932oknc2ico23nlckno2i3f2on2o4ilknc2il3jp23mn"
7
+ config.api_token = "45asf42cwewfwf234/sfw34"
8
+ config.app_id = "23df434v34gdfnrnrt/regergw4"
9
+ config.unauthorised_path = "/unauthorised"
10
+ end
metadata ADDED
@@ -0,0 +1,98 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: driftrock-service
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.8
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Max
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-05-08 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: httparty
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rspec
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ description: Gem for talking to the driftrock apu
47
+ email:
48
+ - max.dupenois@forward.co.uk
49
+ executables: []
50
+ extensions: []
51
+ extra_rdoc_files: []
52
+ files:
53
+ - .gitignore
54
+ - Gemfile
55
+ - LICENSE.txt
56
+ - README.md
57
+ - Rakefile
58
+ - driftrock-service.gemspec
59
+ - lib/driftrock-service.rb
60
+ - lib/driftrock-service/api.rb
61
+ - lib/driftrock-service/api/administrative.rb
62
+ - lib/driftrock-service/api/data.rb
63
+ - lib/driftrock-service/api/puller.rb
64
+ - lib/driftrock-service/config.rb
65
+ - lib/driftrock-service/connector.rb
66
+ - lib/driftrock-service/errors/driftrock_error.rb
67
+ - lib/driftrock-service/version.rb
68
+ - spec/administrative_api_spec.rb
69
+ - spec/models/administrative_api_test.rb
70
+ - spec/spec_helper.rb
71
+ homepage: https://github.com/forward/driftrock-service
72
+ licenses: []
73
+ post_install_message:
74
+ rdoc_options: []
75
+ require_paths:
76
+ - lib
77
+ required_ruby_version: !ruby/object:Gem::Requirement
78
+ none: false
79
+ requirements:
80
+ - - ! '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ required_rubygems_version: !ruby/object:Gem::Requirement
84
+ none: false
85
+ requirements:
86
+ - - ! '>='
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ requirements: []
90
+ rubyforge_project:
91
+ rubygems_version: 1.8.24
92
+ signing_key:
93
+ specification_version: 3
94
+ summary: Maintains the api endpoints and the calls to them with authorisation, etc.
95
+ test_files:
96
+ - spec/administrative_api_spec.rb
97
+ - spec/models/administrative_api_test.rb
98
+ - spec/spec_helper.rb