cuenote-api 0.0.4 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 0c05978f4eae76bf5e6551cc0ff22708660181b7
4
- data.tar.gz: 28dc1aa67c1699bea0ff69e8659221a7a811740a
3
+ metadata.gz: e13e1ff0ab47c0b200e4b8b142b694b47f70d0e2
4
+ data.tar.gz: f794e4f57ef44556995fb4d4a3e55b3382fc570e
5
5
  SHA512:
6
- metadata.gz: b40ce7a2f5c0ebe119b6deed77992f29e4d23871c4a77a939a72593f98084f621b32bc725d3d43a254aa2c0620e29a1f68994a8235d03e818fb1ce710ffd08e5
7
- data.tar.gz: 5e2167e668a65903b5a3e8a3061c6aa2741478b556cd668439c88c2dce8a0988937494804b64b89e0e98034677b82ad154787afcd0441e3c4a3a014dfeb063f0
6
+ metadata.gz: 96101fff168ed0d3f4a471eef16db2d6c7b6e3c84afe2bace5dc44bbb5ddd769c8a058162ac03e6786d6c69ff23ee32165e44033114697fa4fbc203879e71176
7
+ data.tar.gz: 361e5b1b967724b89cc18c83dc22325efb6d95795b379ca0652735161d5014590747f5628d56910a03fbc89adfffde38b98e388d09c2b4c46fcedb423bffc532
data/.gitignore CHANGED
@@ -15,3 +15,4 @@ spec/reports
15
15
  test/tmp
16
16
  test/version_tmp
17
17
  tmp
18
+ .env
@@ -24,4 +24,5 @@ Gem::Specification.new do |spec|
24
24
  spec.add_development_dependency "rake"
25
25
  spec.add_development_dependency "rspec"
26
26
  spec.add_development_dependency "webmock"
27
+ spec.add_development_dependency "dotenv"
27
28
  end
@@ -1,12 +1,14 @@
1
1
  require "cuenote/api/version"
2
+
3
+ # Config
2
4
  require "cuenote/api/config"
3
- require "cuenote/api/xml_builder"
4
- require "cuenote/api/request"
5
- require "cuenote/api/response"
6
- require "cuenote/api/import"
7
5
 
8
- module Cuenote
9
- module Api
10
- # Your code goes here...
11
- end
12
- end
6
+ # Utility
7
+ require "cuenote/api/command"
8
+ require "cuenote/api/connection"
9
+ require "cuenote/api/node"
10
+
11
+ # Api Object
12
+ require "cuenote/api/address_book"
13
+ require "cuenote/api/address"
14
+ require "cuenote/api/import"
@@ -0,0 +1,20 @@
1
+ require "cuenote-api"
2
+
3
+ module Cuenote::Api
4
+ class Address
5
+ def initialize(element=nil)
6
+ @element = element
7
+ # @adbook = element.attributes[:adbook]
8
+ end
9
+
10
+ # TODO
11
+ def update
12
+ run 'editAddress'
13
+ end
14
+
15
+ # TODO
16
+ def delete
17
+ run 'deleteAddress'
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,50 @@
1
+ require "cuenote/api/node"
2
+
3
+ module Cuenote::Api
4
+ class AddressBook < Node
5
+ def initialize(doc=nil)
6
+ super doc
7
+ @adbook = attributes[:adbook]
8
+ puts doc.class
9
+ end
10
+
11
+ def self.list
12
+ res = run 'getAdBookList'
13
+ res.elements.map do |element|
14
+ AddressBook.new(element)
15
+ end
16
+ end
17
+
18
+ # TODO
19
+ def self.create
20
+ end
21
+
22
+ # TODO
23
+ def update
24
+ end
25
+
26
+ # TODO
27
+ def delete
28
+ end
29
+
30
+ # TODO
31
+ def information
32
+ end
33
+
34
+ # TODO
35
+ def addresses
36
+ end
37
+
38
+ # TODO
39
+ def add
40
+ end
41
+
42
+ # TODO
43
+ def select
44
+ end
45
+
46
+ def import
47
+ @import ||= Import.new(adbook: @adbook)
48
+ end
49
+ end
50
+ end
@@ -1,11 +1,22 @@
1
- require "cuenote/api/request"
2
- require "cuenote/api/response"
1
+ require "cuenote/api/command"
3
2
  require "cuenote/api/connection"
4
3
 
5
4
  module Cuenote::Api
6
5
  class Base
7
- def connection
8
- Connection.new
6
+ def self.connection(command)
7
+ Connection.new(command)
8
+ end
9
+
10
+ def self.run(command, params={})
11
+ connection(Command.new(command, params)).response
12
+ end
13
+
14
+ def connection(command)
15
+ self.class.connection command
16
+ end
17
+
18
+ def run(command, params={})
19
+ self.class.run command, params
9
20
  end
10
21
  end
11
22
  end
@@ -0,0 +1,36 @@
1
+ require "builder"
2
+
3
+ module Cuenote::Api
4
+ class Command
5
+ def initialize(command, params={})
6
+ @command, @params = [command, params]
7
+ end
8
+
9
+ def builder
10
+ @builder ||= ::Builder::XmlMarkup.new
11
+ end
12
+
13
+ def build(id)
14
+ build_command @command, id, @params
15
+ end
16
+
17
+ private
18
+
19
+ def build_command command, id, params
20
+ options = {}
21
+ options[:id] = id if id
22
+ options[:command] = command
23
+ if params && params.size > 0
24
+ builder.execute options do |exec|
25
+ exec.parameter do |parameter|
26
+ params.each do |key, val|
27
+ eval "parameter.#{key} \"#{val}\""
28
+ end
29
+ end
30
+ end
31
+ else
32
+ builder.execute options
33
+ end
34
+ end
35
+ end
36
+ end
@@ -10,7 +10,7 @@ module Cuenote::Api
10
10
 
11
11
  def method_missing name, value=nil
12
12
  if value
13
- set name, value
13
+ set name.to_s.gsub(/=/, ''), value
14
14
  else
15
15
  get name
16
16
  end
@@ -1,11 +1,66 @@
1
+ require "net/http"
2
+ require "builder"
3
+
4
+ require "cuenote/api/command"
5
+ require "cuenote/api/result"
6
+
1
7
  module Cuenote::Api
2
8
  class Connection
3
- def send(req)
4
- uri = req.uri
5
- response = Net::HTTP.start uri.hostname, uri.port, use_ssl: (uri.scheme == 'https') do |http|
6
- http.request req
7
- end
8
- Response.new response
9
+ def initialize *commands
10
+ @commands = commands
11
+ end
12
+
13
+ def send
14
+ Net::HTTP.start uri.hostname, uri.port, use_ssl: (uri.scheme == 'https') do |http|
15
+ http.request request
16
+ end.tap {|res| puts res.body }
17
+ end
18
+
19
+ def response
20
+ @response ||= Result.new(send.body)
21
+ end
22
+
23
+ def request
24
+ req = Net::HTTP::Post.new(uri)
25
+ req.basic_auth username, password
26
+ req.content_type = 'form-data'
27
+ req.body = body.map{|k,v| "#{k}=#{v}" }.join("&")
28
+ req
29
+ end
30
+
31
+ def builder
32
+ @builder ||= ::Builder::XmlMarkup.new
33
+ end
34
+
35
+ def body
36
+ {
37
+ CCC: "愛",
38
+ xml: build
39
+ }
40
+ end
41
+
42
+ def build
43
+ builder.forcast do |forcast|
44
+ @commands.map.with_index do |command, id|
45
+ forcast << command.build(id)
46
+ end
47
+ end.tap{|xml| puts xml }
48
+ end
49
+
50
+ def uri
51
+ @uri ||= URI(config.endpoint)
52
+ end
53
+
54
+ def username
55
+ config.username
56
+ end
57
+
58
+ def password
59
+ config.password
60
+ end
61
+
62
+ def config
63
+ Cuenote::Api.config
9
64
  end
10
65
  end
11
66
  end
@@ -2,29 +2,25 @@ require "cuenote/api/base"
2
2
 
3
3
  module Cuenote::Api
4
4
  class Import < Base
5
- def initialize(options={})
6
- @options = options
5
+ def initialize(params={})
6
+ @params = params
7
7
  end
8
8
 
9
- def set
10
- req = Request.new
11
- req.add_command 'setImportEntry', 1, @options
12
- res = connection.send req
9
+ def set params={}
10
+ res = run 'setImportEntry', @params.merge(params)
13
11
  @impid = res.attributes[:impid]
14
12
  end
15
13
 
16
- def get
14
+ def get params={}
17
15
  @impid
18
16
  end
19
17
 
20
- def test
18
+ def test params={}
21
19
  @impid
22
20
  end
23
21
 
24
- def start
25
- req = Request.new
26
- req.add_command 'startImportEntry', 1, impid: @impid
27
- res = connection.send req
22
+ def start params={}
23
+ run 'startImportEntry', params.merge(impid: @impid)
28
24
  end
29
25
  end
30
26
  end
@@ -0,0 +1,30 @@
1
+ require 'rexml/document'
2
+
3
+ require "cuenote/api/base"
4
+
5
+ module Cuenote::Api
6
+ class Node < Base
7
+ def initialize(doc=nil)
8
+ @doc =
9
+ case doc
10
+ when REXML::Element
11
+ doc
12
+ when String
13
+ REXML::Document.new(doc)
14
+ else
15
+ REXML::Document.new('<a/>')
16
+ end
17
+ end
18
+
19
+ def attributes
20
+ @attributes ||= @doc.attributes.inject({}) do
21
+ |hash, attr| hash[attr[0].to_sym] = attr[1]
22
+ hash
23
+ end
24
+ end
25
+
26
+ def elements
27
+ @doc.elements
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,10 @@
1
+ require "cuenote/api/node"
2
+
3
+ module Cuenote::Api
4
+ class Result < Node
5
+ def initialize(doc=nil)
6
+ super
7
+ @doc = @doc.elements['forcast/result']
8
+ end
9
+ end
10
+ end
@@ -1,5 +1,5 @@
1
1
  module Cuenote
2
2
  module Api
3
- VERSION = "0.0.4"
3
+ VERSION = "0.1.0"
4
4
  end
5
5
  end
@@ -0,0 +1,24 @@
1
+ describe "get address" do
2
+ before do
3
+ Cuenote::Api.configure do |config|
4
+ config.endpoint = ENV['CUENOTE_ENDPOINT']
5
+ config.username = ENV['CUENOTE_USERNAME']
6
+ config.password = ENV['CUENOTE_PASSWORD']
7
+ end
8
+ WebMock.disable!
9
+ end
10
+
11
+ after do
12
+ WebMock.enable!
13
+ end
14
+
15
+ it "get address book" do
16
+ books = Cuenote::Api::AddressBook.list
17
+ book = books[0]
18
+
19
+ expect(book).to be_a_kind_of Cuenote::Api::AddressBook
20
+
21
+ import = book.import
22
+ expect(import).to be_a_kind_of Cuenote::Api::Import
23
+ end
24
+ end
@@ -0,0 +1,17 @@
1
+ require "spec_helper"
2
+
3
+ describe Cuenote::Api::Command do
4
+ it "build xml" do
5
+ @builder = Cuenote::Api::Command.new 'command', { key: 'val' }
6
+ expect(@builder.build(1)).to eq <<-XML.chomp
7
+ <execute id="1" command="command"><parameter><key>val</key></parameter></execute>
8
+ XML
9
+ end
10
+
11
+ it "build xml without params" do
12
+ @builder = Cuenote::Api::Command.new 'command'
13
+ expect(@builder.build(1)).to eq <<-XML.chomp
14
+ <execute id="1" command="command"/>
15
+ XML
16
+ end
17
+ end
@@ -1,10 +1,6 @@
1
1
  require "spec_helper"
2
2
 
3
3
  describe Cuenote::Api::Config do
4
- it "default config" do
5
- expect(Cuenote::Api.config.endpoint).to eq "http://asp.cuenote.jp/api/fcio.cgi"
6
- end
7
-
8
4
  it "changed config" do
9
5
  Cuenote::Api.config.endpoint "https://hoge.asp.cuenote.jp/api/fcio.cgi"
10
6
  expect(Cuenote::Api.config.endpoint).to eq "https://hoge.asp.cuenote.jp/api/fcio.cgi"
@@ -12,8 +8,20 @@ describe Cuenote::Api::Config do
12
8
 
13
9
  it "changed config with configure" do
14
10
  Cuenote::Api.configure do |config|
15
- config.endpoint "https://hoge.asp.cuenote.jp/api/fcio.cgi"
11
+ config.endpoint "https://moge.asp.cuenote.jp/api/fcio.cgi"
16
12
  end
17
- expect(Cuenote::Api.config.endpoint).to eq "https://hoge.asp.cuenote.jp/api/fcio.cgi"
13
+ expect(Cuenote::Api.config.endpoint).to eq "https://moge.asp.cuenote.jp/api/fcio.cgi"
14
+ end
15
+
16
+ it "changed config use equal" do
17
+ Cuenote::Api.config.endpoint = "https://fuga.asp.cuenote.jp/api/fcio.cgi"
18
+ expect(Cuenote::Api.config.endpoint).to eq "https://fuga.asp.cuenote.jp/api/fcio.cgi"
19
+ end
20
+
21
+ it "changed config with configure use equal" do
22
+ Cuenote::Api.configure do |config|
23
+ config.endpoint = "https://hage.asp.cuenote.jp/api/fcio.cgi"
24
+ end
25
+ expect(Cuenote::Api.config.endpoint).to eq "https://hage.asp.cuenote.jp/api/fcio.cgi"
18
26
  end
19
27
  end
@@ -1,3 +1,5 @@
1
1
  require "cuenote-api"
2
2
  require 'webmock/rspec'
3
+ require 'dotenv'
3
4
 
5
+ Dotenv.load
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cuenote-api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Eido NABESHIMA
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-04-07 00:00:00.000000000 Z
11
+ date: 2014-04-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: builder
@@ -80,6 +80,20 @@ dependencies:
80
80
  - - '>='
81
81
  - !ruby/object:Gem::Version
82
82
  version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: dotenv
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - '>='
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - '>='
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
83
97
  description: Cuenote API
84
98
  email:
85
99
  - closer009@gmail.com
@@ -95,18 +109,19 @@ files:
95
109
  - cuenote-api.gemspec
96
110
  - lib/cuenote-api.rb
97
111
  - lib/cuenote/api.rb
112
+ - lib/cuenote/api/address.rb
113
+ - lib/cuenote/api/address_book.rb
98
114
  - lib/cuenote/api/base.rb
115
+ - lib/cuenote/api/command.rb
99
116
  - lib/cuenote/api/config.rb
100
117
  - lib/cuenote/api/connection.rb
101
118
  - lib/cuenote/api/import.rb
102
- - lib/cuenote/api/request.rb
103
- - lib/cuenote/api/response.rb
119
+ - lib/cuenote/api/node.rb
120
+ - lib/cuenote/api/result.rb
104
121
  - lib/cuenote/api/version.rb
105
- - lib/cuenote/api/xml_builder.rb
122
+ - spec/integration/address_spec.rb
123
+ - spec/lib/cuenote/api/command_spec.rb
106
124
  - spec/lib/cuenote/api/config_spec.rb
107
- - spec/lib/cuenote/api/request_spec.rb
108
- - spec/lib/cuenote/api/response_spec.rb
109
- - spec/lib/cuenote/api/xml_builder_spec.rb
110
125
  - spec/spec_helper.rb
111
126
  homepage: ''
112
127
  licenses:
@@ -133,9 +148,8 @@ signing_key:
133
148
  specification_version: 4
134
149
  summary: Cuenote API
135
150
  test_files:
151
+ - spec/integration/address_spec.rb
152
+ - spec/lib/cuenote/api/command_spec.rb
136
153
  - spec/lib/cuenote/api/config_spec.rb
137
- - spec/lib/cuenote/api/request_spec.rb
138
- - spec/lib/cuenote/api/response_spec.rb
139
- - spec/lib/cuenote/api/xml_builder_spec.rb
140
154
  - spec/spec_helper.rb
141
155
  has_rdoc:
@@ -1,46 +0,0 @@
1
- require "cuenote/api/xml_builder"
2
- require "cuenote/api/config"
3
- require 'net/http'
4
-
5
- module Cuenote::Api
6
- class Request
7
- def req
8
- req = Net::HTTP::Post.new(uri)
9
- req.basic_auth user, password
10
- req.content_type = 'form-data'
11
- req.body = body
12
- req
13
- end
14
-
15
- def add_command command, id, params
16
- builder.add_command command, id, params
17
- end
18
-
19
- def builder
20
- @builder ||= Cuenote::Api::XmlBuilder.new
21
- end
22
-
23
- def body
24
- {
25
- CCC: "愛",
26
- xml: builder.build
27
- }
28
- end
29
-
30
- def uri
31
- @uri ||= URI(config.endpoint)
32
- end
33
-
34
- def user
35
- config.user
36
- end
37
-
38
- def password
39
- config.password
40
- end
41
-
42
- def config
43
- Cuenote::Api.config
44
- end
45
- end
46
- end
@@ -1,31 +0,0 @@
1
- require 'rexml/document'
2
-
3
- module Cuenote::Api
4
- class Response
5
- def initialize(response)
6
- @response = response
7
- end
8
-
9
- def attributes
10
- @attributes ||= result.attributes.inject({}) do
11
- |hash, attr| hash[attr[0].to_sym] = attr[1]
12
- hash
13
- end
14
- end
15
-
16
- def params
17
- @params ||= result.elements.map.inject({}) do |hash, element|
18
- hash[element.name.to_sym] = element.text
19
- hash
20
- end
21
- end
22
-
23
- def result
24
- @result ||= doc.elements['forecast/result']
25
- end
26
-
27
- def doc
28
- @doc ||= REXML::Document.new @response.body
29
- end
30
- end
31
- end
@@ -1,44 +0,0 @@
1
- require "builder"
2
-
3
- module Cuenote
4
- module Api
5
- class XmlBuilder
6
- def initialize
7
- @builder = ::Builder::XmlMarkup.new
8
- @commands = []
9
- end
10
-
11
- def add_command command, id=nil, params=nil
12
- id, params = [nil, id] if id && !params && Hash === id
13
- @commands << [command, id, params]
14
- end
15
-
16
- def build
17
- @builder.forecast do |forecast|
18
- @commands.each do |command, id, params|
19
- build_command command, id, params
20
- end
21
- end
22
- end
23
-
24
- private
25
-
26
- def build_command command, id, params
27
- options = {}
28
- options[:id] = id if id
29
- options[:command] = command
30
- if params
31
- @builder.execute options do |exec|
32
- exec.parameter do |parameter|
33
- params.each do |key, val|
34
- eval "parameter.#{key} #{val}"
35
- end
36
- end
37
- end
38
- else
39
- @builder.execute options
40
- end
41
- end
42
- end
43
- end
44
- end
@@ -1,11 +0,0 @@
1
- require "spec_helper"
2
-
3
- describe Cuenote::Api::Request do
4
- before do
5
- @request = Cuenote::Api::Request.new
6
- end
7
-
8
- it "send" do
9
- stub_request(:any, Cuenote::Api.config.endpoint)
10
- end
11
- end
@@ -1,14 +0,0 @@
1
- require "spec_helper"
2
-
3
- describe Cuenote::Api::Response do
4
- before do
5
- end
6
-
7
- it do
8
- stub_request(:any, Cuenote::Api.config.endpoint)
9
- .to_return(body: '<forecast><result command="setImportEntry" id="1"> <impid>1</impid><status>ok</status> <statuscode>1</statuscode></result></forecast>')
10
- request = Cuenote::Api::Request.new
11
- # expect(@response.attributes).to eq({ command: "setImportEntry", id: '1'})
12
- # expect(@response.params).to eq({ impid: '1', status: 'ok', statuscode: '1' })
13
- end
14
- end
@@ -1,35 +0,0 @@
1
- require "spec_helper"
2
-
3
- describe Cuenote::Api::XmlBuilder do
4
- before do
5
- @builder = Cuenote::Api::XmlBuilder.new
6
- end
7
-
8
- it "build xml" do
9
- @builder.add_command 'command', 1, { key: 'val' }
10
- expect(@builder.build).to eq <<-XML.chomp
11
- <forecast><execute id="1" command="command"><parameter><key>val</key></parameter></execute></forecast>
12
- XML
13
- end
14
-
15
- it "build xml without id" do
16
- @builder.add_command 'command', { key: 'val' }
17
- expect(@builder.build).to eq <<-XML.chomp
18
- <forecast><execute command="command"><parameter><key>val</key></parameter></execute></forecast>
19
- XML
20
- end
21
-
22
- it "build xml without params" do
23
- @builder.add_command 'command', 1
24
- expect(@builder.build).to eq <<-XML.chomp
25
- <forecast><execute id="1" command="command"/></forecast>
26
- XML
27
- end
28
-
29
- it "build xml only command name" do
30
- @builder.add_command 'command'
31
- expect(@builder.build).to eq <<-XML.chomp
32
- <forecast><execute command="command"/></forecast>
33
- XML
34
- end
35
- end