plamo 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -2,6 +2,7 @@
2
2
  #define RUBY_PLAMO_REQUEST_H
3
3
 
4
4
  extern VALUE rb_cPlamoRequest;
5
+ extern const rb_data_type_t rb_plamo_request_type;
5
6
 
6
7
  void Init_plamo_request(void);
7
8
 
@@ -1,55 +1,57 @@
1
1
  #include "plamo.h"
2
- #include "wrapper.h"
3
2
 
4
3
  VALUE rb_cPlamoResponse;
5
4
 
6
- static void deallocate(Wrapper *wrapper) {
7
- plamo_response_destroy(wrapper->inner);
8
- free(wrapper);
5
+ static void deallocate(void *plamo_response) {
6
+ plamo_response_destroy(plamo_response);
9
7
  }
10
8
 
9
+ const rb_data_type_t rb_plamo_response_type = {
10
+ "Response",
11
+ {
12
+ NULL,
13
+ deallocate,
14
+ NULL,
15
+ },
16
+ NULL,
17
+ NULL,
18
+ 0,
19
+ };
20
+
11
21
  static VALUE allocate(VALUE klass) {
12
- return Data_Wrap_Struct(klass, NULL, deallocate, malloc(sizeof(Wrapper)));
22
+ return TypedData_Wrap_Struct(klass, &rb_plamo_response_type, NULL);
13
23
  }
14
24
 
15
25
  static VALUE initialize(VALUE self) {
16
- Wrapper *wrapper;
17
- Data_Get_Struct(self, Wrapper, wrapper);
18
- wrapper->inner = plamo_response_new();
26
+ DATA_PTR(self) = plamo_response_new();
19
27
  return self;
20
28
  }
21
29
 
22
30
  static VALUE status_code(VALUE self) {
23
- Wrapper *wrapper;
24
- Data_Get_Struct(self, Wrapper, wrapper);
25
- return UINT2NUM(((PlamoResponse*)wrapper->inner)->status_code);
31
+ PlamoResponse *plamo_response;
32
+ TypedData_Get_Struct(self, PlamoResponse, &rb_plamo_response_type, plamo_response);
33
+ return UINT2NUM(plamo_response->status_code);
26
34
  }
27
35
 
28
36
  static VALUE set_status_code(VALUE self, VALUE code) {
29
- Wrapper *wrapper;
30
- Data_Get_Struct(self, Wrapper, wrapper);
31
- ((PlamoResponse*)wrapper->inner)->status_code = FIX2UINT(code);
37
+ PlamoResponse *plamo_response;
38
+ TypedData_Get_Struct(self, PlamoResponse, &rb_plamo_response_type, plamo_response);
39
+ plamo_response->status_code = FIX2UINT(code);
32
40
  return code;
33
41
  }
34
42
 
35
43
  static VALUE header(VALUE self) {
36
- Wrapper *wrapper;
37
- Data_Get_Struct(self, Wrapper, wrapper);
38
- VALUE rb_plamo_http_header = Data_Wrap_Struct(rb_cPlamoHttpHeader, NULL, free, malloc(sizeof(Wrapper)));
39
- Wrapper *plamo_http_header_wrapper;
40
- Data_Get_Struct(rb_plamo_http_header, Wrapper, plamo_http_header_wrapper);
41
- plamo_http_header_wrapper->inner = ((PlamoResponse*)wrapper->inner)->header;
44
+ PlamoResponse *plamo_response;
45
+ TypedData_Get_Struct(self, PlamoResponse, &rb_plamo_response_type, plamo_response);
46
+ VALUE rb_plamo_http_header = TypedData_Wrap_Struct(rb_cPlamoHttpHeader, &rb_plamo_http_header_type, plamo_response->header);
42
47
  return rb_plamo_http_header;
43
48
  }
44
49
 
45
50
  static VALUE body(VALUE self) {
46
- Wrapper *wrapper;
47
- Data_Get_Struct(self, Wrapper, wrapper);
48
- if (((PlamoResponse*)wrapper->inner)->body != NULL) {
49
- VALUE rb_plamo_byte_array = Data_Wrap_Struct(rb_cPlamoByteArray, NULL, free, malloc(sizeof(Wrapper)));
50
- Wrapper *plamo_byte_array_wrapper;
51
- Data_Get_Struct(rb_plamo_byte_array, Wrapper, plamo_byte_array_wrapper);
52
- plamo_byte_array_wrapper->inner = ((PlamoResponse*)wrapper->inner)->body;
51
+ PlamoResponse *plamo_response;
52
+ TypedData_Get_Struct(self, PlamoResponse, &rb_plamo_response_type, plamo_response);
53
+ if (plamo_response->body != NULL) {
54
+ VALUE rb_plamo_byte_array = TypedData_Wrap_Struct(rb_cPlamoByteArray, &rb_plamo_byte_array_type, plamo_response->body);
53
55
  return rb_plamo_byte_array;
54
56
  } else {
55
57
  return Qnil;
@@ -57,12 +59,11 @@ static VALUE body(VALUE self) {
57
59
  }
58
60
 
59
61
  static VALUE set_body(VALUE self, VALUE rb_body) {
60
- Wrapper *wrapper;
61
- Data_Get_Struct(self, Wrapper, wrapper);
62
-
62
+ PlamoResponse *plamo_response;
63
+ TypedData_Get_Struct(self, PlamoResponse, &rb_plamo_response_type, plamo_response);
63
64
  if (strcmp("String", rb_class2name(rb_obj_class(rb_body))) == 0) {
64
65
  struct RString *rstring = RSTRING(rb_body);
65
- ((PlamoResponse*)wrapper->inner)->body = plamo_byte_array_new(RSTRING_PTR(rstring), RSTRING_LEN(rstring));
66
+ plamo_response->body = plamo_byte_array_new(RSTRING_PTR(rstring), RSTRING_LEN(rstring));
66
67
  } else {
67
68
  VALUE *array = RARRAY_PTR(rb_body);
68
69
  size_t len = RARRAY_LEN(rb_body);
@@ -70,10 +71,9 @@ static VALUE set_body(VALUE self, VALUE rb_body) {
70
71
  for (size_t i = 0; i < len; i++) {
71
72
  buf[i] = NUM2CHR(array[i]);
72
73
  }
73
- ((PlamoResponse*)wrapper->inner)->body = plamo_byte_array_new(buf, len);
74
+ plamo_response->body = plamo_byte_array_new(buf, len);
74
75
  free(array);
75
76
  }
76
-
77
77
  return self;
78
78
  }
79
79
 
@@ -2,6 +2,7 @@
2
2
  #define RUBY_PLAMO_RESPONSE_H
3
3
 
4
4
  extern VALUE rb_cPlamoResponse;
5
+ extern const rb_data_type_t rb_plamo_response_type;
5
6
 
6
7
  void Init_plamo_response(void);
7
8
 
@@ -1,35 +1,53 @@
1
1
  #include "plamo.h"
2
- #include "wrapper.h"
3
2
 
4
3
  VALUE rb_cPlamoStringArray;
5
4
 
6
- static void deallocate(Wrapper *wrapper) {
7
- plamo_string_array_destroy(wrapper->inner);
8
- free(wrapper);
5
+ static void deallocate(void *plamo_string_array) {
6
+ plamo_string_array_destroy(plamo_string_array);
9
7
  }
10
8
 
9
+ const rb_data_type_t rb_plamo_string_array_type = {
10
+ "StringArray",
11
+ {
12
+ NULL,
13
+ deallocate,
14
+ NULL,
15
+ },
16
+ NULL,
17
+ NULL,
18
+ 0,
19
+ };
20
+
11
21
  static VALUE allocate(VALUE klass) {
12
- return Data_Wrap_Struct(klass, NULL, deallocate, malloc(sizeof(Wrapper)));
22
+ return TypedData_Wrap_Struct(klass, &rb_plamo_string_array_type, NULL);
13
23
  }
14
24
 
15
25
  static VALUE initialize(VALUE self) {
16
- Wrapper *wrapper;
17
- Data_Get_Struct(self, Wrapper, wrapper);
18
- wrapper->inner = plamo_string_array_new();
26
+ DATA_PTR(self) = plamo_string_array_new();
19
27
  return self;
20
28
  }
21
29
 
30
+ static VALUE length(VALUE self) {
31
+ PlamoStringArray *plamo_string_array;
32
+ TypedData_Get_Struct(self, PlamoStringArray, &rb_plamo_string_array_type, plamo_string_array);
33
+ return SIZET2NUM(plamo_string_array_length(plamo_string_array));
34
+ }
35
+
22
36
  static VALUE push(VALUE self, VALUE rb_string) {
23
- Wrapper *wrapper;
24
- Data_Get_Struct(self, Wrapper, wrapper);
25
- plamo_string_array_add(wrapper->inner, StringValueCStr(rb_string));
37
+ PlamoStringArray *plamo_string_array;
38
+ TypedData_Get_Struct(self, PlamoStringArray, &rb_plamo_string_array_type, plamo_string_array);
39
+ if (OBJ_FROZEN(self)) {
40
+ rb_exc_raise(rb_eFrozenError);
41
+ return Qnil;
42
+ }
43
+ plamo_string_array_add(plamo_string_array, StringValueCStr(rb_string));
26
44
  return Qnil;
27
45
  }
28
46
 
29
47
  static VALUE first(VALUE self) {
30
- Wrapper *wrapper;
31
- Data_Get_Struct(self, Wrapper, wrapper);
32
- const char *str = plamo_string_array_get_first(wrapper->inner);
48
+ PlamoStringArray *plamo_string_array;
49
+ TypedData_Get_Struct(self, PlamoStringArray, &rb_plamo_string_array_type, plamo_string_array);
50
+ const char *str = plamo_string_array_get_first(plamo_string_array);
33
51
  if (str != NULL) {
34
52
  return rb_str_new2(str);
35
53
  } else {
@@ -38,9 +56,9 @@ static VALUE first(VALUE self) {
38
56
  }
39
57
 
40
58
  static VALUE last(VALUE self) {
41
- Wrapper *wrapper;
42
- Data_Get_Struct(self, Wrapper, wrapper);
43
- const char *str = plamo_string_array_get_last(wrapper->inner);
59
+ PlamoStringArray *plamo_string_array;
60
+ TypedData_Get_Struct(self, PlamoStringArray, &rb_plamo_string_array_type, plamo_string_array);
61
+ const char *str = plamo_string_array_get_last(plamo_string_array);
44
62
  if (str != NULL) {
45
63
  return rb_str_new2(str);
46
64
  } else {
@@ -49,9 +67,9 @@ static VALUE last(VALUE self) {
49
67
  }
50
68
 
51
69
  static VALUE get_at(VALUE self, VALUE index) {
52
- Wrapper *wrapper;
53
- Data_Get_Struct(self, Wrapper, wrapper);
54
- const char *str = plamo_string_array_get_at(wrapper->inner, FIX2ULONG(index));
70
+ PlamoStringArray *plamo_string_array;
71
+ TypedData_Get_Struct(self, PlamoStringArray, &rb_plamo_string_array_type, plamo_string_array);
72
+ const char *str = plamo_string_array_get_at(plamo_string_array, FIX2ULONG(index));
55
73
  if (str != NULL) {
56
74
  return rb_str_new2(str);
57
75
  } else {
@@ -64,16 +82,16 @@ static void execute_each(const char *str) {
64
82
  }
65
83
 
66
84
  static VALUE each(VALUE self) {
67
- Wrapper *wrapper;
68
- Data_Get_Struct(self, Wrapper, wrapper);
69
- plamo_string_array_for_each(wrapper->inner, execute_each);
85
+ PlamoStringArray *plamo_string_array;
86
+ TypedData_Get_Struct(self, PlamoStringArray, &rb_plamo_string_array_type, plamo_string_array);
87
+ plamo_string_array_for_each(plamo_string_array, execute_each);
70
88
  return Qnil;
71
89
  }
72
90
 
73
91
  static VALUE delete_at(VALUE self, VALUE index) {
74
- Wrapper *wrapper;
75
- Data_Get_Struct(self, Wrapper, wrapper);
76
- if (plamo_string_array_remove_at(wrapper->inner, FIX2ULONG(index))) {
92
+ PlamoStringArray *plamo_string_array;
93
+ TypedData_Get_Struct(self, PlamoStringArray, &rb_plamo_string_array_type, plamo_string_array);
94
+ if (plamo_string_array_remove_at(plamo_string_array, FIX2ULONG(index))) {
77
95
  return Qtrue;
78
96
  } else {
79
97
  return Qfalse;
@@ -84,6 +102,8 @@ void Init_plamo_string_array(void) {
84
102
  rb_cPlamoStringArray = rb_define_class_under(rb_mPlamo, "StringArray", rb_cObject);
85
103
  rb_define_alloc_func(rb_cPlamoStringArray, allocate);
86
104
  rb_define_method(rb_cPlamoStringArray, "initialize", initialize, 0);
105
+ rb_define_method(rb_cPlamoStringArray, "length", length, 0);
106
+ rb_define_alias(rb_cPlamoStringArray, "size", "length");
87
107
  rb_define_method(rb_cPlamoStringArray, "push", push, 1);
88
108
  rb_define_method(rb_cPlamoStringArray, "first", first, 0);
89
109
  rb_define_method(rb_cPlamoStringArray, "last", last, 0);
@@ -91,4 +111,3 @@ void Init_plamo_string_array(void) {
91
111
  rb_define_method(rb_cPlamoStringArray, "each", each, 0);
92
112
  rb_define_method(rb_cPlamoStringArray, "delete_at", delete_at, 1);
93
113
  }
94
-
@@ -2,6 +2,7 @@
2
2
  #define RUBY_PLAMO_STRING_ARRAY_H
3
3
 
4
4
  extern VALUE rb_cPlamoStringArray;
5
+ extern const rb_data_type_t rb_plamo_string_array_type;
5
6
 
6
7
  void Init_plamo_string_array(void);
7
8
 
data/lib/plamo/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Plamo
2
- VERSION = "0.2.0"
2
+ VERSION = "0.3.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: plamo
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Shogo Otake
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-10-14 00:00:00.000000000 Z
11
+ date: 2019-11-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -60,6 +60,7 @@ extensions:
60
60
  - ext/plamo/extconf.rb
61
61
  extra_rdoc_files: []
62
62
  files:
63
+ - ".github/FUNDING.yml"
63
64
  - ".gitignore"
64
65
  - Gemfile
65
66
  - Gemfile.lock
@@ -75,6 +76,16 @@ files:
75
76
  - ext/plamo/plamo_app.h
76
77
  - ext/plamo/plamo_byte_array.c
77
78
  - ext/plamo/plamo_byte_array.h
79
+ - ext/plamo/plamo_form_data.c
80
+ - ext/plamo/plamo_form_data.h
81
+ - ext/plamo/plamo_form_data_field.c
82
+ - ext/plamo/plamo_form_data_field.h
83
+ - ext/plamo/plamo_form_data_field_array.c
84
+ - ext/plamo/plamo_form_data_field_array.h
85
+ - ext/plamo/plamo_form_data_file.c
86
+ - ext/plamo/plamo_form_data_file.h
87
+ - ext/plamo/plamo_form_urlencoded.c
88
+ - ext/plamo/plamo_form_urlencoded.h
78
89
  - ext/plamo/plamo_http_header.c
79
90
  - ext/plamo/plamo_http_header.h
80
91
  - ext/plamo/plamo_http_query.c
@@ -87,7 +98,6 @@ files:
87
98
  - ext/plamo/plamo_response.h
88
99
  - ext/plamo/plamo_string_array.c
89
100
  - ext/plamo/plamo_string_array.h
90
- - ext/plamo/wrapper.h
91
101
  - lib/plamo.rb
92
102
  - lib/plamo/version.rb
93
103
  - plamo.gemspec
data/ext/plamo/wrapper.h DELETED
@@ -1,8 +0,0 @@
1
- #ifndef RUBY_WRAPPER_H
2
- #define RUBY_WRAPPER_H
3
-
4
- typedef struct Wrapper {
5
- void *inner;
6
- } Wrapper;
7
-
8
- #endif /* RUBY_WRAPPER_H */