itrigga-admin_api_client 0.1.4 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.4
1
+ 0.2.0
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{itrigga-admin_api_client}
8
- s.version = "0.1.4"
8
+ s.version = "0.2.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Anson Kelly"]
@@ -27,6 +27,11 @@ Gem::Specification.new do |s|
27
27
  "VERSION",
28
28
  "configuration.yml",
29
29
  "itrigga-admin_api_client.gemspec",
30
+ "lib/itrigga/admin_api_client.rb",
31
+ "lib/itrigga/admin_api_client/admin_api_client.rb",
32
+ "lib/itrigga/admin_api_client/proxies/admin.rb",
33
+ "lib/itrigga/admin_api_client/proxies/api.rb",
34
+ "lib/itrigga/admin_api_client/proxies/search_tracker.rb",
30
35
  "spec/admin_api_client_spec.rb",
31
36
  "spec/proxies/admin_spec.rb",
32
37
  "spec/proxies/api_spec.rb",
@@ -0,0 +1,31 @@
1
+ # allow this to be used outside of full Rails init
2
+ this_dir = File.dirname(__FILE__)
3
+ $: << this_dir unless $:.include?(this_dir)
4
+
5
+ #require 'faster_csv'
6
+ require 'yaml'
7
+ require 'hashie'
8
+ require 'core_ext'
9
+ require 'trigga/param_fu'
10
+ require 'net_helper'
11
+
12
+ #require 'trigga/param_fu'
13
+ Dir[File.join(File.dirname(File.expand_path(__FILE__)),"admin_api_client","proxies","*.rb")].each {|f| require f }
14
+ Dir[File.join(File.dirname(File.expand_path(__FILE__)),"admin_api_client","*.rb")].each {|f| require f }
15
+
16
+ # load configuration
17
+ unless defined? RAILS_ENV
18
+ RAILS_ENV = ENV['RAILS_ENV'] || ENV['RACK_ENV'] || "development"
19
+ end
20
+
21
+ Itrigga.add_log_line("", true)
22
+ Itrigga.add_log_line(" iTrigga Admin Client API")
23
+ Itrigga.add_log_line("", true)
24
+
25
+ begin
26
+ plugin_configuration_file = ENV['TRIGGA_ADMIN_API_CLIENT_CONFIG_FILE'] || File.join(File.dirname(File.expand_path(__FILE__)),"..","..","configuration.yml")
27
+ TRIGGA_ADMIN_API_CLIENT_CONFIG = Hashie::Mash.new(YAML.load_file(plugin_configuration_file)[RAILS_ENV])
28
+ rescue Exception => e
29
+ Itrigga.add_log_line("...disabled => Unable to load Admin Client API configuration from #{plugin_configuration_file}.\n")
30
+ Itrigga.add_log_line("Exception: #{e.message}")
31
+ end
@@ -0,0 +1,86 @@
1
+ module Itrigga
2
+ module AdminApiClient
3
+ include Trigga::ParamFu
4
+ include Itrigga::AdminApiClient::Proxies::Admin
5
+ include Itrigga::AdminApiClient::Proxies::Api
6
+ include Itrigga::AdminApiClient::Proxies::SearchTracker
7
+
8
+ private
9
+
10
+ def self.make_call(module_name, endpoint, opts = {})
11
+ require_param(opts, :api_key)
12
+
13
+ host = make_absolute_url( opts[:host] || TRIGGA_ADMIN_API_CLIENT_CONFIG[module_name].base_url )
14
+
15
+ begin
16
+ url = build_url(host,endpoint,opts)
17
+ Itrigga.add_log_line("[API Client] - #{url}")
18
+
19
+ response = JSON.parse(Itrigga::NetHelper.do_get(url))
20
+ ::Hashie::Mash.new(response.kind_of?(Array) ? default_response : response.merge(default_response) )
21
+
22
+ rescue Exception => e
23
+ Itrigga.add_log_line "[API Client Error] - #{e.message}\n#{e.backtrace.join('\n')}" if RAILS_ENV == "development"
24
+ ::Hashie::Mash.new(:error => e.message, :status_code => status_code_from_error(e.message))
25
+ end
26
+ end
27
+
28
+ def self.default_response
29
+ {:status_code => 200}
30
+ end
31
+
32
+ def self.make_endpoint_absolute(endpoint)
33
+ endpoint.to_s.match(/^\/{1}.*/) ? endpoint : "/#{endpoint}"
34
+ end
35
+
36
+ def self.make_absolute_url(url)
37
+ url.to_s.match(/^https?:\/\//) ? url : "http://#{url}"
38
+ end
39
+
40
+ def self.build_url(url, endpoint, h = {})
41
+ # default format to json
42
+ format = h[:format] || "json"
43
+ h.delete :format
44
+
45
+ params = h.map { |k, v| "#{k}=#{v}" }.join("&")
46
+ "#{url}#{make_endpoint_absolute(endpoint)}.#{format}#{params.empty? ? '' : '?' + params}"
47
+ end
48
+
49
+ # get the status code from the error message
50
+ # eg "404 Not Found"
51
+ def self.status_code_from_error(error)
52
+ code = error.match(/^(\d{3} )/)
53
+ if code
54
+ code.to_s.to_i
55
+ else
56
+ 0
57
+ end
58
+ end
59
+
60
+ end
61
+
62
+ # ==============================================================================================================================
63
+ # Prints a debugging info line into the log...
64
+ # ==============================================================================================================================
65
+
66
+ def self.add_log_line(line, add_padding = false)
67
+ is_terminal = false
68
+ begin
69
+ @terminal_width ||= `stty size`.split.map { |x| x.to_i }.reverse[0]
70
+ is_terminal = true
71
+ rescue Exception => e
72
+ # not running in terminal``
73
+ end
74
+
75
+ if add_padding and is_terminal and !@terminal_width.nil?
76
+ puts "#{line.ljust(@terminal_width - line.length, "=")}\n"
77
+ else
78
+ if defined?(Rails.logger)
79
+ Rails.logger.info line
80
+ else
81
+ puts line
82
+ end
83
+ end
84
+ end
85
+
86
+ end
@@ -0,0 +1,26 @@
1
+ module Itrigga
2
+ module AdminApiClient
3
+ module Proxies
4
+ module Admin
5
+
6
+ def self.included(base)
7
+ base.send("extend",self)
8
+ end
9
+
10
+ def get_stats( opts = {})
11
+ Itrigga::AdminApiClient.make_call "admin", "stats", opts
12
+ end
13
+
14
+ def get_clients(opts = {})
15
+ Itrigga::AdminApiClient.make_call "admin", "clients", opts
16
+ end
17
+
18
+ def get_client(opts = {})
19
+ require_param(opts, :client_id)
20
+ Itrigga::AdminApiClient.make_call "admin", "clients/#{opts[:client_id]}", opts
21
+ end
22
+
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,52 @@
1
+ module Itrigga
2
+ module AdminApiClient
3
+ module Proxies
4
+ module Api
5
+
6
+ def self.included(base)
7
+ base.send("extend",self)
8
+ end
9
+
10
+ private
11
+
12
+ def common_params
13
+ [:page, :per_page, :max_tags, :site_key, :api_key]
14
+ end
15
+
16
+ def make_api_call(endpoint, opts = {})
17
+ require_one_of( opts, :site_key, :site_id )
18
+ Itrigga::AdminApiClient.make_call("api", endpoint, opts.reject{|k,v| !common_params.include?(k.to_sym) } )
19
+ end
20
+
21
+ public
22
+
23
+ def get_channels(opts)
24
+ make_api_call("channels", opts )
25
+ end
26
+
27
+ def get_channel_items( opts )
28
+ require_param( opts, :channel_id )
29
+ make_api_call("channels/#{opts[:channel_id]}", opts )
30
+ end
31
+
32
+ def get_sources( opts )
33
+ make_api_call('sources', opts)
34
+ end
35
+
36
+ def get_item_tags( opts )
37
+ make_api_call('item_tags', opts)
38
+ end
39
+
40
+ def get_latest_items( opts )
41
+ make_api_call('items', opts)
42
+ end
43
+
44
+ def get_item_details( opts )
45
+ require_param( opts, :item_id )
46
+ make_api_call("items/#{opts[:item_id]}", opts)
47
+ end
48
+
49
+ end
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,13 @@
1
+ module Itrigga
2
+ module AdminApiClient
3
+ module Proxies
4
+ module SearchTracker
5
+
6
+ def self.included(base)
7
+ base.send("extend",self)
8
+ end
9
+
10
+ end
11
+ end
12
+ end
13
+ end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: itrigga-admin_api_client
3
3
  version: !ruby/object:Gem::Version
4
- hash: 19
4
+ hash: 23
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
- - 1
9
- - 4
10
- version: 0.1.4
8
+ - 2
9
+ - 0
10
+ version: 0.2.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Anson Kelly
@@ -256,6 +256,11 @@ files:
256
256
  - VERSION
257
257
  - configuration.yml
258
258
  - itrigga-admin_api_client.gemspec
259
+ - lib/itrigga/admin_api_client.rb
260
+ - lib/itrigga/admin_api_client/admin_api_client.rb
261
+ - lib/itrigga/admin_api_client/proxies/admin.rb
262
+ - lib/itrigga/admin_api_client/proxies/api.rb
263
+ - lib/itrigga/admin_api_client/proxies/search_tracker.rb
259
264
  - spec/admin_api_client_spec.rb
260
265
  - spec/proxies/admin_spec.rb
261
266
  - spec/proxies/api_spec.rb