marketwatch 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: 8d756b2ca1ca8b362dd0bb2090c4a136a61e59f7
4
+ data.tar.gz: 26ba6dc80f6041cba030eafb0b5b46be7aba6970
5
+ SHA512:
6
+ metadata.gz: c2a62eecb6ba702b25bb1fc5833f198f9af17ebd0e085e2c37d396259723f69290c9314fd7535cb3fe19b187fc27a09ff726668e7bc8eaeaba6361a89504b8bc
7
+ data.tar.gz: 3716cf3637aac061a148b3074a0078bbba1c3c0d1353d589a1cf673a83fdd44d6b9d111701752bcbbf7cdd99726503a989d076ab8f34978e56cac317ce840f74
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (C) 2014 Benjamin Feng
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,31 @@
1
+ # Marketwatch
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'marketwatch'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install marketwatch
20
+
21
+ ## Usage
22
+
23
+ TODO: Write usage instructions here
24
+
25
+ ## Contributing
26
+
27
+ 1. Fork it ( https://github.com/fengb/marketwatch/fork )
28
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
29
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
30
+ 4. Push to the branch (`git push origin my-new-feature`)
31
+ 5. Create a new Pull Request
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,2 @@
1
+ require 'marketwatch/flashcharter'
2
+ require 'marketwatch/version'
@@ -0,0 +1,96 @@
1
+ require 'open-uri'
2
+ require 'uri'
3
+ require 'json'
4
+ require 'date'
5
+ require 'ostruct'
6
+
7
+ module Marketwatch
8
+ module Flashcharter
9
+ TYPES = {
10
+ historical_prices: 1,
11
+ info: 21,
12
+ }
13
+
14
+ def self.info(params={})
15
+ raise unless params[:ticker]
16
+
17
+ params[:type] = TYPES[:info]
18
+
19
+ raw = raw_request(params)
20
+ OpenStruct.new(
21
+ raw: raw,
22
+ range: time(raw['Range']['First'])..time(raw['Range']['Last']),
23
+ instrument: OpenStruct.new(
24
+ common_name: raw['instrument']['CommonName'],
25
+ cusip: raw['instrument']['Cusip'],
26
+ ticker: raw['instrument']['Ticker'],
27
+ )
28
+ )
29
+ end
30
+
31
+ def self.historical_prices(params={})
32
+ raise unless params[:ticker]
33
+
34
+ params[:type] = TYPES[:historical_prices]
35
+
36
+ params[:end_date] ||= Date.today
37
+ # 4 weeks of data just because
38
+ params[:begin_date] ||= params[:end_date] - 28
39
+ params[:country_code] ||= 'US' # TODO: this isn't necessary
40
+ params[:frequency] ||= 5 # TODO: magic number
41
+
42
+ raw = raw_request(params)
43
+ raw['TimeSeriesOhlcDataPoint'].map do |raw_data|
44
+ OpenStruct.new.tap do |o|
45
+ o.raw = raw_data
46
+ o.open = raw_data['Open']
47
+ o.high = raw_data['High']
48
+ o.low = raw_data['Low']
49
+ o.last = raw_data['Last']
50
+ o.volume = raw_data['Volume']
51
+ o.begin_time = time(raw_data['BeginDateUTime'])
52
+ o.end_time = time(raw_data['EndDateUTime'])
53
+ # Totally misnamed, but this adheres to the response
54
+ o.begin_date = o.begin_time
55
+ o.end_date = o.end_time
56
+ end
57
+ end
58
+ end
59
+
60
+ def self.raw_request(params)
61
+ params[:doc_set_uri] ||= [90,103,159,173,183,184,3126,436,2988] # TODO: voodoo numbers
62
+ encoded = encode_params(params)
63
+ url = "http://www.marketwatch.com/thunderball.flashcharter/JsonHandler.ashx?#{encoded}"
64
+ open url do |f|
65
+ JSON.parse f.read
66
+ end
67
+ end
68
+
69
+ def self.encode_params(params)
70
+ preprocessed = preprocess_params(params)
71
+ URI.encode_www_form(preprocessed)
72
+ end
73
+
74
+ def self.camel(s)
75
+ s.to_s.gsub /_(.)/ do |match|
76
+ match[1].upcase
77
+ end
78
+ end
79
+
80
+ def self.preprocess_params(params)
81
+ {}.tap do |ret|
82
+ params.each do |key, value|
83
+ if value.respond_to?(:strftime)
84
+ ret[camel(key)] = value.strftime('%m/%d/%y %H:%M:%S')
85
+ else
86
+ ret[camel(key)] = value
87
+ end
88
+ end
89
+ end
90
+ end
91
+
92
+ def self.time(num)
93
+ Time.at(num).utc
94
+ end
95
+ end
96
+ end
@@ -0,0 +1,3 @@
1
+ module Marketwatch
2
+ VERSION = "0.0.2"
3
+ end
metadata ADDED
@@ -0,0 +1,92 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: marketwatch
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - fengb
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-09-11 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.6'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.6'
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: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.1'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.1'
55
+ description: API for Marketwatch
56
+ email:
57
+ - contact@fengb.info
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - LICENSE
63
+ - README.md
64
+ - Rakefile
65
+ - lib/marketwatch.rb
66
+ - lib/marketwatch/flashcharter.rb
67
+ - lib/marketwatch/version.rb
68
+ homepage: https://github.com/fengb/marketwatch
69
+ licenses:
70
+ - MIT
71
+ metadata: {}
72
+ post_install_message:
73
+ rdoc_options: []
74
+ require_paths:
75
+ - lib
76
+ required_ruby_version: !ruby/object:Gem::Requirement
77
+ requirements:
78
+ - - ">="
79
+ - !ruby/object:Gem::Version
80
+ version: '0'
81
+ required_rubygems_version: !ruby/object:Gem::Requirement
82
+ requirements:
83
+ - - ">="
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ requirements: []
87
+ rubyforge_project:
88
+ rubygems_version: 2.2.2
89
+ signing_key:
90
+ specification_version: 4
91
+ summary: API for Marketwatch
92
+ test_files: []