tpex 0.1.6

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.
@@ -0,0 +1,69 @@
1
+ require "json"
2
+
3
+ module TPEX
4
+ class Client
5
+ module Products
6
+
7
+ FIELDS = {
8
+ manufacturer: {machine_name: "manufacturer"},
9
+ certified: {machine_name: "field_certified", required: true},
10
+ tags: {machine_name: "field_tags", required: true, label:"tid"},
11
+ model_no: {machine_name: "field_model_num", required: true, enclosed: true},
12
+ brand: {machine_name: "field_product_brand", required: true, enclosed: true},
13
+ product_line: {machine_name: "field_product_line_family_name", enclosed: true},
14
+ femp: {machine_name: "field_femp", required: true},
15
+ upc: {machine_name: "field_universal_product_code_upc"},
16
+ energy_star_no: {machine_name: "field_energy_star_specification_", enclosed: true},
17
+ uuid: {machine_name: "uuid"}
18
+ }
19
+
20
+ Product = Struct.new(*FIELDS.keys)
21
+
22
+ # def get_product_by_uuid()
23
+
24
+ # end
25
+
26
+ def post_product(product)
27
+ data = jsonify_product(product)
28
+ response = post("content", data)
29
+ end
30
+
31
+ def build_product(options={})
32
+ required_fields = []
33
+ arguments = []
34
+
35
+ TPEX::Client::Products::FIELDS.each do |key, value|
36
+ required_fields << key if value[:required]
37
+ arguments << options[key]
38
+ end
39
+
40
+ required_fields.each do |key|
41
+ return nil if options[key].nil?
42
+ end
43
+
44
+ return Product.new(*arguments);
45
+ end
46
+
47
+ def jsonify_product(product)
48
+ if !product[:uuid].nil?
49
+ jsonobject = {"title" => "Json Object", "uuid" => product[:uuid]}
50
+ else
51
+ jsonobject = {"title" => "Json Object"}
52
+ end
53
+ fields = TPEX::Client::Products::FIELDS
54
+
55
+ product.each_pair do |name, value|
56
+ if name != :uuid
57
+ if !value.nil?
58
+ label = fields[name][:label] || "value"
59
+ contents = {label => value}
60
+ contents = [{label => value}] if fields[name][:enclosed]
61
+ jsonobject[fields[name][:machine_name]] = {"und" => contents}
62
+ end
63
+ end
64
+ end
65
+ JSON.generate({"product" => jsonobject})
66
+ end
67
+ end
68
+ end
69
+ end
@@ -0,0 +1,64 @@
1
+ require 'faraday'
2
+ require File.expand_path('../version', __FILE__)
3
+
4
+ module TPEX
5
+ module Configuration
6
+
7
+ VALID_OPTIONS_KEYS = [
8
+ :username,
9
+ :password,
10
+ :access_token,
11
+ :endpoint,
12
+ :access_endpoint,
13
+ :format,
14
+ :user_agent,
15
+ :cookie,
16
+ :logging
17
+ ].freeze
18
+
19
+ VALID_FORMATS = [
20
+ :json
21
+ ].freeze
22
+
23
+ # Default config values
24
+ DEFAULT_FORMAT = :json
25
+ DEFAULT_USERNAME = nil
26
+ DEFAULT_PASSWORD = nil
27
+ DEFAULT_ACCESS_TOKEN = nil
28
+ DEFAULT_ENDPOINT = 'http://aw.concept3ddev.com/tpex/test_endpoint'
29
+ DEFAULT_USER_AGENT = "TPEX Ruby Client #{TPEX::VERSION}".freeze
30
+ DEFAULT_COOKIE = nil
31
+ DEFAULT_LOGGING = nil
32
+
33
+ attr_accessor *VALID_OPTIONS_KEYS
34
+
35
+ # When this module is extended, set all configuration options to their default values
36
+ def self.extended(base)
37
+ base.reset
38
+ end
39
+
40
+ # Convenience method to allow configuration options to be set in a block
41
+ def configure
42
+ yield self
43
+ end
44
+
45
+ # Create a hash of options and their values
46
+ def options
47
+ VALID_OPTIONS_KEYS.inject({}) do |option, key|
48
+ option.merge!(key => send(key))
49
+ end
50
+ end
51
+
52
+ # Reset all configuration options to defaults
53
+ def reset
54
+ self.format = DEFAULT_FORMAT
55
+ self.username = DEFAULT_USERNAME
56
+ self.password = DEFAULT_PASSWORD
57
+ self.access_token = DEFAULT_ACCESS_TOKEN
58
+ self.endpoint = DEFAULT_ENDPOINT
59
+ self.user_agent = DEFAULT_USER_AGENT
60
+ self.cookie = DEFAULT_COOKIE
61
+ self.logging = DEFAULT_LOGGING
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,33 @@
1
+ require 'faraday_middleware'
2
+ Dir[File.expand_path('../../faraday/*.rb', __FILE__)].each{|f| require f}
3
+
4
+ module TPEX
5
+ # @private
6
+ module Connection
7
+
8
+ # private
9
+
10
+ def connection(raw=false)
11
+
12
+ options = {
13
+ :headers => {
14
+ 'Accept' => "application/#{format}; charset=utf-8",
15
+ 'User-Agent' => user_agent,
16
+ 'Cookie' => cookie,
17
+ 'X-CSRF-Token' => access_token
18
+ }.reject{ |k,v| v.nil? },
19
+ :ssl => {:verify => false},
20
+ :url => endpoint,
21
+ }
22
+
23
+ Faraday::Connection.new(options) do |c|
24
+ c.use FaradayMiddleware::Mashify unless raw
25
+ c.response :json, :content_type => /\bjson$/
26
+ c.response :logger if @logging
27
+ c.use FaradayMiddleware::RaiseHttpException
28
+ c.adapter Faraday.default_adapter
29
+
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,21 @@
1
+ module TPEX
2
+ class Error < StandardError; end
3
+
4
+ # For 400's
5
+ class BadRequest < Error; end
6
+
7
+ # For 401's
8
+ class Unauthorized < Error; end
9
+
10
+ # For 404's.
11
+ class NotFound < Error; end
12
+
13
+ # For 406's.
14
+ class NotAcceptable < Error; end
15
+
16
+ # For 500's
17
+ class InternalServerError < Error; end
18
+
19
+
20
+
21
+ end
@@ -0,0 +1,39 @@
1
+ module TPEX
2
+ module Request
3
+ # Perform an HTTP GET request
4
+ def get(path, options={}, raw=false, unformatted=false, no_response_wrapper=false)
5
+ request(:get, path, options, raw, unformatted, no_response_wrapper)
6
+ end
7
+
8
+ # Perform an HTTP POST request
9
+ def post(path, options={}, raw=false, unformatted=false, no_response_wrapper=false)
10
+ request(:post, path, options, raw, unformatted, no_response_wrapper)
11
+ end
12
+
13
+ private
14
+
15
+ # Perform an HTTP request
16
+ def request(method, path, options, raw=false, unformatted=false, no_response_wrapper=false)
17
+ response = connection(raw).send(method) do |request|
18
+ path = formatted_path(path) unless unformatted
19
+
20
+ case method
21
+ when :get, :delete
22
+ request.url(path, options)
23
+ when :post, :put
24
+ request.path = path
25
+ request.body = options unless options.empty?
26
+ end
27
+
28
+ request.headers['Content-Type'] = 'application/json'
29
+ end
30
+ return response if raw
31
+ return response.body if no_response_wrapper
32
+ return Response.create( response.body )
33
+ end
34
+
35
+ def formatted_path(path)
36
+ [path, format].compact.join('.')
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,9 @@
1
+ module TPEX
2
+ module Response
3
+ def self.create( response_hash )
4
+ data = response_hash.data.dup rescue response_hash
5
+ data.extend( self )
6
+ data
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,11 @@
1
+ module TPEX
2
+ module ServicesCSRF
3
+
4
+ # Returns a token used in the X-CSRF-Token header, to prevent
5
+ # cross-site request forgeries
6
+ def get_access_token(options={})
7
+ post("/services/session/token", options={}, raw=false, unformatted=true, no_response_wrapper=true)
8
+ end
9
+
10
+ end
11
+ end
@@ -0,0 +1,9 @@
1
+ module TPEX
2
+
3
+ VERSION = "0.1.6"
4
+
5
+ def self.version
6
+ VERSION
7
+ end
8
+
9
+ end
@@ -0,0 +1,17 @@
1
+ # This file was generated by the `rspec --init` command. Conventionally, all
2
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
3
+ # Require this file using `require "spec_helper"` to ensure that it is only
4
+ # loaded once.
5
+ #
6
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
7
+ RSpec.configure do |config|
8
+ config.treat_symbols_as_metadata_keys_with_true_values = true
9
+ config.run_all_when_everything_filtered = true
10
+ config.filter_run :focus
11
+
12
+ # Run specs in random order to surface order dependencies. If you find an
13
+ # order dependency and want to debug it, you can fix the order by providing
14
+ # the seed, which is printed after each run.
15
+ # --seed 1234
16
+ config.order = 'random'
17
+ end
metadata ADDED
@@ -0,0 +1,123 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tpex
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.6
5
+ platform: ruby
6
+ authors:
7
+ - Katherine Fleming
8
+ - Francisco Rodriguez
9
+ - Daniel Studer
10
+ - Nicholas Long
11
+ autorequire:
12
+ bindir: bin
13
+ cert_chain: []
14
+ date: 2013-12-10 00:00:00.000000000 Z
15
+ dependencies:
16
+ - !ruby/object:Gem::Dependency
17
+ name: faraday
18
+ requirement: !ruby/object:Gem::Requirement
19
+ requirements:
20
+ - - '>='
21
+ - !ruby/object:Gem::Version
22
+ version: '0'
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: faraday_middleware
32
+ requirement: !ruby/object:Gem::Requirement
33
+ requirements:
34
+ - - '>='
35
+ - !ruby/object:Gem::Version
36
+ version: '0'
37
+ type: :runtime
38
+ prerelease: false
39
+ version_requirements: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ - !ruby/object:Gem::Dependency
45
+ name: pry
46
+ requirement: !ruby/object:Gem::Requirement
47
+ requirements:
48
+ - - '>='
49
+ - !ruby/object:Gem::Version
50
+ version: '0'
51
+ type: :runtime
52
+ prerelease: false
53
+ version_requirements: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - '>='
56
+ - !ruby/object:Gem::Version
57
+ version: '0'
58
+ - !ruby/object:Gem::Dependency
59
+ name: json
60
+ requirement: !ruby/object:Gem::Requirement
61
+ requirements:
62
+ - - '>='
63
+ - !ruby/object:Gem::Version
64
+ version: '0'
65
+ type: :runtime
66
+ prerelease: false
67
+ version_requirements: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - '>='
70
+ - !ruby/object:Gem::Version
71
+ version: '0'
72
+ description: This gem allows for the creation of product and performance nodes on
73
+ the TPE website.
74
+ email: katherine.fleming@nrel.gov
75
+ executables: []
76
+ extensions: []
77
+ extra_rdoc_files: []
78
+ files:
79
+ - README.rdoc
80
+ - lib/faraday/raise_http_exception.rb
81
+ - lib/ponse = client.post_performance_node(perf)
82
+ - lib/tpex/api.rb
83
+ - lib/tpex/client/auth.rb
84
+ - lib/tpex/client/fields.txt
85
+ - lib/tpex/client/files.rb
86
+ - lib/tpex/client/performancenodes.rb
87
+ - lib/tpex/client/products.rb
88
+ - lib/tpex/client.rb
89
+ - lib/tpex/configuration.rb
90
+ - lib/tpex/connection.rb
91
+ - lib/tpex/error.rb
92
+ - lib/tpex/request.rb
93
+ - lib/tpex/response.rb
94
+ - lib/tpex/servicescsrf.rb
95
+ - lib/tpex/version.rb
96
+ - lib/tpex.rb
97
+ - spec/spec_helper.rb
98
+ homepage: http://performance.nrel.gov
99
+ licenses:
100
+ - LGPL
101
+ metadata: {}
102
+ post_install_message:
103
+ rdoc_options: []
104
+ require_paths:
105
+ - lib
106
+ required_ruby_version: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - '>='
109
+ - !ruby/object:Gem::Version
110
+ version: '1.9'
111
+ required_rubygems_version: !ruby/object:Gem::Requirement
112
+ requirements:
113
+ - - '>='
114
+ - !ruby/object:Gem::Version
115
+ version: 1.3.6
116
+ requirements: []
117
+ rubyforge_project:
118
+ rubygems_version: 2.0.3
119
+ signing_key:
120
+ specification_version: 4
121
+ summary: Client library for NREL's Technology Performance Exchange
122
+ test_files:
123
+ - spec/spec_helper.rb